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
?>