On the surface, it is easy to misunderstand that the empty () function is a function used to judge whether the string is null. In fact, it is not, so I suffered a lot of losses.
The empty () function is used to test whether the variable has been configured. If the variable already exists, non-null string, or non-zero, false is returned; otherwise, true is returned. Therefore, if the value of the string is 0, true is returned, that is, the internal statement of empty is executed. This is a trap.
The code is as follows: |
Copy code |
$ A = array ("1" => "ddf "); Var_dump (empty ($ a); // boolean false $ B = 0; Var_dump (empty ($ B); // boolean true |
We advise you to use the empty () function.
To judge whether the string is null, you can judge it as follows: if ($ value = "")...
* Format: bool empty (mixed var)
* Function: check whether a variable is empty.
* Return value:
* Returns TRUE if the variable does not exist.
* If a variable exists and its values are "", 0, "0", NULL, FALSE, array (), var $ var, and objects without any attribute, true is returned.
* If a variable exists and its values are not "", 0, "0", NULL, FALSE, array (), var $ var, or an object without any attribute, FALSE is returned.
* Version: PHP 3, PHP 4, and PHP 5
On the surface, it is easy to misunderstand that the empty () function is a function used to judge whether the string is null. In fact, it is not, so I suffered a lot of losses. The empty () function is used to test whether the variable has been configured. If the variable already exists, non-null string, or non-zero, false is returned; otherwise, true is returned. Therefore, if the value of the string is 0, true is returned, that is, the internal statement of empty is executed. This is a trap. For example, suppose $ value = 0; then empty ($ value) = false. We advise you to use the empty () function. To judge whether the string is null, you can judge it as follows: if ($ value = "")... format: bool empty (mixed var) function: checks whether a variable has a null return value: if the variable does not exist, TRUE is returned. If the variable exists, its values are "", 0, "0", NULL, FALSE, array (), and var $ var; if the variable exists and the value is not "", 0, "0", NULL, FALSE, array (), var $ var; if the object does not have any attribute, FALSE versions are returned: PHP 3, PHP 4, and PHP 5.
When you use empty to check the results returned by a function, the following error occurs: Fatal error: Can't use function return value in write context.
For example:
The code is as follows: |
Copy code |
<? Php Echo empty (strlen ('be -evil.org ')); |
Go to the PHP manual and check the following text in the description of the empty function:
Note: empty () only checks variables as anything else will result in a parse error. In other words, the following will not work: empty (trim ($ name )).
Empty () only detects variables, and any non-variables will cause parsing errors!
Therefore, we cannot use empty to directly detect the values returned by the function. The solution in the preceding example is as follows:
The code is as follows: |
Copy code |
<? Php $ Length = strlen ('be -evil.org '); Echo empty ($ length ); ?> |