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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:03:15  来源:igfitidea点击:

What does the exclamation point mean in PHP?

phpif-statementisset

提问by oscar

Possible Duplicate:
Reference - What does this symbol mean in PHP?
Getting confused with empty, isset, !empty, !isset

可能的重复:
参考 - 这个符号在 PHP 中是什么意思?
与空、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 NOToperator. It reverses the sense of the logical test.

!是逻辑否定或NOT运算符。它颠倒了逻辑测试的意义。

That is:

那是:

  • if(isset)makes something happen if issetis logical True.
  • if(!isset)makes something happen if issetis logical False.
  • 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 OR
  • xorlogical 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 FALSEand vice-versa.

如果您有一个计算结果为 的表达式TRUE,则在其前面加上!导致计算结果为 的前缀,FALSE反之亦然。

$test = 'value';

var_dump(isset($test));  // TRUE
var_dump(!isset($test)); // FALSE


isset()returns TRUEif the given variable is defined in the current scope with a non-null value.

isset()返回TRUE如果给定的变量与一个非空值的当前范围限定。

empty()returns TRUEif 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 为空