我已经使用PHP了很长一段时间了,但这对我来说总是一个谜,在变量前正确使用感叹号(负号).
什么!$var表示? var为false,为空,未设置等?
以下是我需要学习的一些例子……
例1:
$string = 'hello';
$hello = (!empty($string)) ? $string : '';
if (!$hello)
{
die('Variable hello is empty');
}
这个例子有效吗?如果$string为空,if语句是否真的有效?
例2:
$int = 5;
$count = (!empty($int)) ? $int : 0;
// Note the positive check here
if ($count)
{
die('Variable count was not empty');
}
这个例子有效吗?
我从不使用上面的任何一个例子,我将这些if($var)限制为只有布尔值的变量.我只需要知道这些示例是否有效,这样我就可以扩大if($var)语句的使用范围.他们看起来很干净.
谢谢.