if () two ways to determine if a variable is empty, which is better? Is there a difference?
When learning to study WordPress, there are two ways to determine whether a custom field is empty, using the if (). Beginner PHP, I do not know what the difference between the two methods, which is more efficient?
Show the first value of the specified key inside a loop
$key _1_value = Get_post_meta ($post->id, ' key_1 ', true);
Check if the custom field has a value
if ($key _1_value! = "){
echo $key _1_value;
}
?>
Display an image based on Custom-field value, if it exists
ID, ' url ', true);
if ($image):?>
"Alt=" "/>
------Solution--------------------
Citation:
Thank you. According to the IF ($image) method, if (Key_1_value) is not more concise? if ($image) is this the only way to take variables as parameters, is it advisable?
if ($image) and if (! $image) is the exact opposite of the concept
For example
$find = 1;
if ($find) {
echo "Find is not empty";
}else{
echo "Find is empty";
}
Equivalent
if (! $find) {
echo "Find is empty";
}else{
echo "Find is not empty";
}
That's $find and! The IF in the $find is exactly the opposite of what is in else
------Solution--------------------
if ($key _1_value!= ") {//here refers to $key_1_value as long as it is not empty, $key _1_value=0 is not empty all $key_1_value=0 can also be passed
echo $key _1_value;
}
if ($key _1_value) {//here refers to $key_1_value as true, the so-called Real is $key_1_value can not be empty also can not be 0, this condition is more than the above a $key_1_value can not be 0
echo $key _1_value;
}
The above two methods function a little different, look at your needs
?>