检查多个变量是否不为空 -PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8273928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if multiple variables are not empty -PHP
提问by Helena
This is the code:
这是代码:
<?php if($variable !== '') { ?>
<?php echo $variable; ?>
<?php } ?>
What I want is that it checks if multiple variables are empty.
我想要的是它检查多个变量是否为空。
Thank you.
谢谢你。
回答by jeroen
As you are comparing to an empty string (!==''
), you can also use concatenation:
当您与空字符串 ( !==''
)进行比较时,您还可以使用串联:
if (!empty($a . $b . $c))
By the way:
顺便一提:
if($variable !== '') {
echo $variable;
}
Is the same as just:
是一样的:
echo $variable;
回答by Adrian Cornish
Use the empty() call in PHP.
在 PHP 中使用 empty() 调用。
http://www.php.net/manual/en/function.empty.php
http://www.php.net/manual/en/function.empty.php
<?php if(!empty($variable)) { ?>
<?php echo $variable; ?>
<?php } ?>
回答by drdwilcox
Use the logical and operator: &&
:
使用逻辑和运算符&&
::
<?php if ($var1 !== '' && $var2 !== '') { ?>
Also, you can place all of this script in one tag.
此外,您可以将所有这些脚本放在一个标签中。