There are many ways to determine whether a string is empty in php, such as equal to null == null, empty, and isset can all be used to determine whether a variable or string is empty. Let me tell you about them below. introduce.
String; determine whether the string is empty; output judgment; you can modify it
The code is as follows | Copy code | ||||
if ($C_char=='') return false; //Is it empty
|
代码如下 | 复制代码 |
$str = ''; if($str==='') {//''==null ''==false ''!==false |
Example
The code is as follows | Copy code |
代码如下 | 复制代码 |
$var = 0; |
The code is as follows | Copy code |
$var = 0;<🎜> // The result is true because $var is empty<🎜> if (empty($var)) {<🎜> echo '$var is either 0 or not set at all';<🎜> }<🎜> // Result is false because $var is set<🎜> if (!isset($var)) {<🎜> echo '$var is not set at all';<🎜> }<🎜> ?> |
On the surface, it is easy to misunderstand that the empty() function is a function that determines whether a string is empty. In fact, it is not, and I suffered a lot because of it.
The empty() function is used to test whether the variable has been configured. If the variable already exists, is a non-empty string, or is non-zero, a false value is returned; otherwise
true value. Therefore, when the value of the string is 0, true is also returned, which is to execute the statement inside empty. This is the trap.
For example: Assume $value = 0; then empty($value)=false.
To determine whether a string is empty, you can determine it like this: if ($value=="") ...
* Format: bool empty (mixed var)
* Function: Check whether a variable is empty
* Return value:
* If the variable does not exist, return TRUE
* If the variable exists and its value is "", 0, "0", NULL,, FALSE, array(), var $var; and an object without any attributes, return
TURE
* If the variable exists and the value is not "", 0, "0", NULL,, FALSE, array(), var $var; and an object without any attributes, return
FALSE
isset() and empty() judgment methods:
The code is as follows
|
Copy code | ||||
echo " isset test:
" ;