Empty functions that are prone to abnormal use in php

Source: Internet
Author: User
Which of the following empty functions is php easy to use? In the official php manual, I wrote ,? Check whether a variable is empty. If var is a non-null or non-zero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, array (), var $ var, and empty functions that are not prone to php errors

?

According to the official php Manual,

?

Check whether a variable is empty.
If var is a non-null or non-zero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, array (), var $ var; and objects without any attributes will be considered empty, if var is null, TRUE is returned .?

?

Then I solemnly stated in his manual that,

?

Empty () only detects variables, and any non-variables will cause parsing errors. In other words, the following statement does not work: empty (addslashes ($ name )).?

Empty is used to check whether the variable is empty, which is often used in website programming. for example, if we submit the user name on the front-end page, we need to determine whether it is empty, if it is empty, let him submit it again. in this case, we need to use the trim function to filter out spaces on both sides and then use empty for detection.

?

if  (empty(trim($_GET['username'])) { …}

?

However, when we run this code, an error is reported. The reason is that"Empty () only detects variables", While trim returns true values, not variables. So an error is reported when empty runs here. The modification method is also very simple. The first method is as follows:

?

if  (trim($_GET['username'] == ’‘) { …}

?

The second method adds the intermediate variable:

?

$username = trim($_GET['username']);if  (empty($username)) { …}
?

The recommended method is to use the verification framework in the 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.