Dug*_*ugi 11 php variables if-statement boolean-expression negation
我已经使用PHP了很长一段时间了,但这对我来说总是一个谜,在变量前正确使用感叹号(负号).
有什么!$var表明?是var false,空,没有设置等?
以下是我需要学习的一些例子......
例1:
$string = 'hello';
$hello = (!empty($string)) ? $string : '';
if (!$hello)
{
die('Variable hello is empty');
}
Run Code Online (Sandbox Code Playgroud)
这个例子有效吗?如果$string是空的话,if语句真的可以工作吗?
例2:
$int = 5;
$count = (!empty($int)) ? $int : 0;
// Note the positive check here
if ($count)
{
die('Variable count was not empty');
}
Run Code Online (Sandbox Code Playgroud)
这个例子有效吗?
我从不使用上述任何示例,我将这些限制if ($var)为仅具有布尔值的变量.我只需要知道这些例子是否有效,这样我就可以扩大对if ($var)语句的使用范围.他们看起来很干净.
谢谢.
| 归档时间: |
|
| 查看次数: |
25732 次 |
| 最近记录: |