if () determine if the variable is not NULL for both methods, which is better? Is there a difference?

Source: Internet
Author: User
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
?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.