php PHP中的感叹号是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9994581/
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
What does the exclamation point mean in PHP?
提问by oscar
Possible Duplicate:
Reference - What does this symbol mean in PHP?
Getting confused with empty, isset, !empty, !isset
In PHP what is the difference between:
在 PHP 中有什么区别:
if(!isset)
if(isset)
Same with if(!empty)
and if(empty)
?
与if(!empty)
和相同if(empty)
?
What does the "!" character mean?
“!”是什么意思 性格是什么意思?
回答by Li-aung Yip
!
is the logical negation or NOT
operator. It reverses the sense of the logical test.
!
是逻辑否定或NOT
运算符。它颠倒了逻辑测试的意义。
That is:
那是:
if(isset)
makes something happen ifisset
is logicalTrue
.if(!isset)
makes something happen ifisset
is logicalFalse
.
if(isset)
使事情发生如果isset
是合乎逻辑的True
。if(!isset)
使事情发生如果isset
是合乎逻辑的False
。
More about operators (logical and other types) in the PHP documentation.Look up !
there to cement your understanding of what it does. While you're there, also look up the other logical operators:
PHP 文档中有关运算符(逻辑和其他类型)的更多信息。在!
那里查看以巩固您对它的作用的理解。当你在那里时,还要查找其他逻辑运算符:
&&
logical AND||
logical ORxor
logical EXCLUSIVE-OR
&&
逻辑与||
逻辑或xor
逻辑异或
Which are also commonly used in logic statements.
也常用于逻辑语句。
回答by DaveRandom
The !
character is the logical "not" operator. It inverts the boolean meaning of the expression.
该!
字符是逻辑“非”运算符。它反转表达式的布尔含义。
If you have an expression that evaluates to TRUE
, prefixing it with !
causes it evaluate to FALSE
and vice-versa.
如果您有一个计算结果为 的表达式TRUE
,则在其前面加上!
导致计算结果为 的前缀,FALSE
反之亦然。
$test = 'value';
var_dump(isset($test)); // TRUE
var_dump(!isset($test)); // FALSE
isset()
returns TRUE
if the given variable is defined in the current scope with a non-null value.
isset()
返回TRUE
如果给定的变量与一个非空值的当前范围限定。
empty()
returns TRUE
if the given variable is notdefined in the current scope, or if it is defined with a value that is considered "empty". These values are:
empty()
返回TRUE
如果给定的变量未在当前范围定义,或者如果它是与被认为是“空”的值来定义。这些值是:
NULL // NULL value
0 // Integer/float zero
'' // Empty string
'0' // String '0'
FALSE // Boolean FALSE
array() // empty array
Depending PHP version, an object with no properties may also be considered empty.
根据 PHP 版本,没有属性的对象也可能被认为是空的。
The upshot of this is that isset()
and empty()
almostcompliment each other (they return the opposite results) but not quite, as empty()
performs an additional check on the value of the variable, isset()
simply checks whether it is defined.
这样做的结果是,isset()
和empty()
几乎是互补的(它们返回的相反的结果),但不完全,因为empty()
执行额外的检查对变量的值,isset()
简单地检查它是否被定义。
Consider the following example:
考虑以下示例:
var_dump(isset($test)); // FALSE
var_dump(empty($test)); // TRUE
$test = '';
var_dump(isset($test)); // TRUE
var_dump(empty($test)); // TRUE
$test = 'value';
var_dump(isset($test)); // TRUE
var_dump(empty($test)); // FALSE
回答by Churk
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
Edit:
编辑:
here is a test case for you:
这是一个测试用例:
$p = false;
echo isset($p) ? '$p is setted : ' : '$p is not setted : ';
echo empty($p) ? '$p is empty' : '$p is not empty';
echo "<BR>";
$p is setted : $p is empty
$p 已设置:$p 为空