Precautions for using the empty () function in php _ PHP Tutorial

Source: Internet
Author: User
Tags parse error
Precautions for using the empty () function in php. When empty () is used to determine the number 0, empty () returns true, that is, if your variable may contain the number 0, you must use isset () will be better !! When empty () is used to determine the number 0, empty () returns true, that is, if your variable may contain the number 0, you must use isset () will be better !!

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:

$ 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:

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:

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

Echo empty ($ length );
?>

When evaluate () is used to determine the number 0, empty () returns true, that is, if your variable may contain the number 0, you must use isset () will be better !! On 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.