Which of the following two methods is better if () to judge whether a variable is null? Is there a difference?

Source: Internet
Author: User
Which of the following two methods can be used to determine whether a variable is null? Is there a difference? When studying wordpress, IF () is used to determine whether the custom domain is empty (). I don't know what the difference between the two methods is. which one is more efficient? // & Nbsp; show & nbsp; the & nbsp; first & nbsp; value & nbsp if () two methods to judge whether the variable is null are better? Is there a difference?
When studying wordpress, IF () is used to determine whether the custom domain is empty (). I don't know what the difference between the two methods is. which one 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 --------------------
Reference:
Thank you. According to the if ($ image) method, isn't if (key_1_value) more concise? If ($ image) is a method that only uses variables as parameters. Is it advisable?


If ($ image) and if (! $ Image) is the opposite 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 is, $ find and! In $ find, if is the opposite of the content in else.



------ Solution --------------------
If ($ key_1_value! = '') {// This indicates that $ key_1_value can be used as long as it is not empty. $ key_1_value = 0 is also not empty. all $ key_1_value = 0 is also allowed.
Echo $ key_1_value;
}
If ($ key_1_value) {// Here $ key_1_value is true. The so-called true is that $ key_1_value cannot be blank or 0, and the condition $ key_1_value cannot be 0
Echo $ key_1_value;
}
// The functions of the above two methods are slightly different, depending on 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.