The empty function that PHP is prone to use abnormally

Source: Internet
Author: User
The empty function that PHP is prone to use incorrectly

?

Written in the official PHP manual,

?

Checks if a variable is empty.
If Var is a non-null or nonzero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties will be considered empty and return TRUE if Var is empty.

?

It was then solemnly stated in its handbook,

?

Empty () detects only variables and detects anything that is non-variable will result in parsing errors. In other words, the statement behind will not work: Empty (Addslashes ($name)).

?

Empty is used to detect whether the variable is empty, which is often used in website programming, for example, we submitted the user's user name in the foreground page, when we need to determine whether it is empty, if it is empty, let him resubmit, then we need to use the function trim, that is, filter out the space on both sides, Then test with empty.

?

If  (Empty (Trim ($_get[' username ')) {...}

?

However, when we ran this code, it was an error. The reason is that "empty () only detects variables ", and the exact value returned by trim is not a variable. So empty executes the error when it comes here. The method of modification is also very simple, the first method:

?

If  (Trim ($_get[' username ' = = ') {...}

?

The second method, increase the intermediate variables:

?

$username = Trim ($_get[' username '), if  (empty ($username)) {...}
?

The recommended approach is to use the validation framework in your project to solve this problem.

  • 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.