Those pits that PHP stepped on (1) isset () detailed

Source: Internet
Author: User

In daily development, we often encounter the use of the Isset () function to detect whether a variable is set, what needs attention, and today to discuss

Note: The PHP version tested is 5.6.22

There's a pit ahead.

First on the code:

$arr [' a '] = NULL;

$arr [' b '] = ';

$arr [' c '] = 0;

$arr [' d '] = 0.0;

$arr [' e '] = [];

Var_dump (Isset ($arr [' a ']));

Var_dump (Isset ($arr [' B ']));

Var_dump (Isset ($arr [' C ']));

Var_dump (Isset ($arr [' d ']));

Var_dump (Isset ($arr [' e ']));

The operating result is:

BOOL (FALSE) bool (TRUE) bool (TRUE) bool (TRUE) bool (true)

The pits or questions here are:

Isset is to detect whether a variable is set, then set to null is also called set, why return false?

Second, the pit is justified

If you have questions about isset, that means the Bible is still out of the book. The manual:

Definition: isset-detection variable is set

Usage: bool Isset (mixed $var [, mixed $ ...]

Detects if the variable is set and is not NULL.

Returns TRUE if Var exists and the value is not NULL, otherwise FALSE is returned.

In fact, the meaning of NULL is not the meaning of the value, that is, there is no set value, Var_dump (Isset ($arr [' a '])), of course, return to false.

Third, anti-crater expansion

1. Look at the code:

$b = ' ABCD ';

Var_dump (isset ($b [0]);

Var_dump (Isset ($b [' 0 ']));

The result is:

BOOL (TRUE) bool (true)

This is somewhat like a python string, and you need to be careful when using isset.

2. What if you have to decide if the value of $arr[' A ' is null? You can use Array_key_exists () to make judgments.

$arr [' a '] = NULL;

if (array_key_exists (' A ', $arr) && $arr [' A ']===null)

{

echo ' value is null ';

}

3.NULL is case insensitive, meaning

A NULL value indicates that a variable has no value. The only possible value for a null type is NULL.

A variable is considered NULL in the following cases:

-is assigned a value of NULL.

-Has not been assigned a value.

-Be unset ().

On the last question, we did not give the answer, now instead of choosing the question:

$a = ' 123b ';

$b = ' 12a ';

Var_dump ($a < $b);

The result of this code operation is:

A, BOOL (TRUE)

B, BOOL (FALSE)

Those pits that PHP stepped on (1) isset () detailed

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.