PHP use Empty () function to note _php tutorial

Source: Internet
Author: User
When you use empty () to determine the number 0, empty () will also return true, that is, if your variable may appear the number 0 this variable, to use Isset () to judge it will be better!!

On the face of it, it is easy to misunderstand that the empty () function is a function that determines whether a string is empty, in fact it is not, I eat a lot of losses.

The empty () function is used to test whether a variable has been configured. Returns False if the variable already exists, a non-empty string, or nonzero, and returns a true value. So, when the value of the string is 0 o'clock, it also returns True, which is the statement inside the empty. This is the 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

Advise everyone, be careful to use the empty () function.

Judging if the string is empty, you can say so: if ($value = = "") ...
* Format: BOOL empty (mixed Var)
* Function: Check if a variable is empty
* Return value:
* Returns TRUE if the variable does not exist
* If the variable exists and its value is "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties, the TURE is returned.
* If the variable exists and the value is not "", 0, "0", NULL, FALSE, Array (), Var $var; And an object that does not have any properties, it returns FALSE
* Version: PHP 3, PHP 4, PHP 5

On the face of it, it is easy to misunderstand that the empty () function is a function that determines whether a string is empty, in fact it is not, I eat a lot of losses. The empty () function is used to test whether a variable has been configured. Returns False if the variable already exists, a non-empty string, or nonzero, and returns a true value. So, when the value of the string is 0 o'clock, it also returns True, which is the statement inside the empty. This is the trap. such as: suppose $value = 0; Then empty ($value) =false. Advise everyone, be careful to use the empty () function. Judging if the string is empty, you can say so: if ($value = = "") ... Format: bool Empty (mixed Var) function: Checks whether a variable is a null return value: Returns TRUE if the variable does not exist if the variable exists and its value is "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties, return TURE if the variable exists and the value is not "", 0, "0", NULL, FALSE, Array (), Var $var; and an object without any attributes, it returns FALSE version: Php 3, PHP 4, PHP 5


When you use empty to check the results returned by a function, you get an error: Fatal Error:can ' t uses function return value in write context

For example:

The code is as follows Copy Code

echo Empty (strlen (' be-evil.org '));

To the PHP manual, where the empty function describes the following text:

Note:empty () Only checks variables as anything else would result in a parse error. In other words, the following would not Work:empty (trim ($name)).

Empty () detects only variables and detects anything that is non-variable will result in parsing errors!

Therefore, we cannot take empty to directly detect the value returned by the function, the solution of the above example is as follows:

The code is as follows Copy Code

$length = strlen (' be-evil.org ');

echo Empty ($length);
?>

http://www.bkjia.com/PHPjc/631570.html www.bkjia.com true http://www.bkjia.com/PHPjc/631570.html techarticle When you use empty () to determine the number 0, empty () will also return true, that is, if your variable may appear the number 0 this variable, to use Isset () to judge it will be better!! From the surface ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.