Use less empty () functions during php Development

Source: Internet
Author: User
As we all know, php is a weak type of language, so we must be rigorous in the development process at ordinary times. do not easily bury some low-level pitfalls. The empty () function should be used with caution. let's take a look at the following examples:

As we all know, php is a weak type of language, so we must be rigorous in the development process at ordinary times. do not easily bury some low-level pitfalls. The empty () function should be used with caution. let's take a look at the following examples:


$ ZeroInt = 0;

Var_dump (empty ($ zeroInt); // bool (true)

$ ZeroIntStr = "0 ";

Var_dump (empty ($ zeroIntStr); // bool (true)

$ ZeroFloat = 0.0;

Var_dump (empty ($ zeroFloat); // bool (true)

$ ZeroFloatStr = "0.0 ";

Var_dump (empty ($ zeroFloatStr ));//Bool (false)Error-prone scenario: for example, if the latitude field is of the floating point type, php uses SQL to find out that conversion is a string type. if it is determined by empty, it is very likely to cause problems.

$ SpaceStr = ""; // Space

Var_dump (empty ($ spaceStr ));//Bool (false)Remember that the space is not empty

$ EmptyArr = array ();

Var_dump (empty ($ emptyArr); // bool (true)

$ EmptyStr = "";

Var_dump (empty ($ emptyStr); // bool (true)

$ Null = null;

Var_dump (empty ($ null); // bool (true)

$ EmptyArr2 = array (

Array ()

);

Var_dump (empty ($ emptyArr2 ));//Bool (false)This is also prone to errors.

Var_dump (empty ($ undefine); // bool (true)

?>


Php sacrifices rigor for convenience and flexibility. I think this is an embarrassing part of php. as a phper, you must start from yourself and strictly demand yourself, make programs written by yourself more stable.

 

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.