Home > Article > Backend Development > if()判断变量是不是为空的两种方法,哪种更好?有区别吗
if()判断变量是否为空的两种方法,哪种更好?有区别吗?
学习研究wordpress时,有以下两种判断自定义域是否为空的方法,都用到了IF()。初学php,不知这两种方法有何区别,哪种效率更高一些?
// 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="" />
$find=1;
if($find){
echo "find不为空";
}else{
echo "find为空";
}
//相当于
if(!$find){
echo "find为空";
}else{
echo "find不为空";
}