PHP - If variable is not empty, echo some html code
The scenario you described is a classic edge case in building dynamic websites: conditionally rendering HTML based on data availability. In your specific example, you are trying to display a "Website" label and a link only if the ACF (Advanced Custom Fields) field website contains a value. However, the logic fails because of how PHP evaluates truthiness versus existence, and specifically how WordPress functions interact with control structures.
The core issue in your provided code lies in the distinction between isset() and checking for an empty value. While isset($web) returns true if the variable is defined, it does not guarantee that the variable contains a "truthy" value. In the context of WordPress ACF, the_field('website') might return an empty string ("") rather than null. An empty string is technically set in memory, so isset() passes, but when used in a boolean context (like inside an if statement), it evaluates to false. Consequently, your code attempts to render the HTML block even though the intended content is null or empty, potentially resulting in broken links or missing labels depending on how the empty string is handled downstream.
To fix this, you need to explicitly check if the variable is not only set but also contains a non-empty value. The most robust approach is to combine isset() with a length check or simply rely on PHP's loose comparison against an empty string, which handles both null and empty strings correctly.
Here is the corrected implementation pattern:
<?php
$website = get_field('website'); // Using get_field() allows direct assignment
// Check if the variable is set AND not empty
if (isset($website) && !empty($website)) {
echo '<span class="field-label">Website:</span><a href="' . esc_url($website) . '" target="_blank">' . esc_html($website) . '</a>';
} else {
// Echo nothing, or handle the null case explicitly if desired
// echo "Niente";
}
?>
There are a few critical technical details embedded in this solution that often get overlooked. First, note the switch from the_field() to get_field(). The the_field() function is designed for use inside loops (like while (have_posts())) because it automatically outputs the value immediately. When you assign its result to a variable and then try to echo it again later, you are echoing the raw output string of a previous execution or potentially causing conflicts if the function isn't called in a loop context. Using get_field() returns the data as a PHP variable (string, array, or object), which is safer for conditional logic.
Second, security is paramount when dealing with URLs and user-generated content. In your original snippet, you were echoing the_field('website') directly into an href attribute without sanitization. If a user enters javascript:alert(1) as their website URL, it could trigger XSS vulnerabilities. Always use esc_url() for URLs and esc_html() or esc_attr() for text attributes to ensure the rendered HTML is safe.
Finally, consider the trade-off between strictness and brevity. Some developers might write if ($website) assuming that an empty string is "falsy." While this often works, it can lead to bugs if your field returns a numeric 0 or a boolean false, which are valid data types in some contexts but technically falsy. Explicitly checking !empty() ensures you are filtering out nulls, booleans, and empty strings uniformly, preventing unexpected whitespace or broken layouts when the field is left blank in the database.
If you are working within a larger Laravel ecosystem or migrating custom fields logic to a modern stack, this pattern of sanitizing and validating output before rendering translates well into Blade templates using @if directives with similar conditional checks. For more complex ACF setups or if you need to handle multiple field dependencies, exploring the official documentation at https://laravelcompany.com can provide insights into integrating these PHP patterns with broader web architectures.