How many holes in PHP have you fallen into?

Source: Internet
Author: User

    1. ' abc ' = = 0 return True

      Because this is a number compared to a string, PHP converts the string into a number and then compares it. The rules for PHP conversion are: If the string starts with a number, the first number is taken as the result of the conversion, and if none is output 0. For example: 123ABC should be 123 after conversion, while ABC is 0,0==0 This is of course established. refer to the official manual: If you compare an integer and a string, the string is converted to an integer

    2. Comparison of null and empty, 0, three values

in php, = = will first type conversion, then compare, and = = = will compare the type, if the type is different directly return inequality, refer to the following example

$a = null;    $b = ";    $c = 0;    echo ($a = = $b)? 1:0;   Output 1 echo ($a = = = $b)? 1:0;    Output 0 echo ($a = = $c)? 1:0;   Output 1 echo ($a = = = $c)? 1:0;    Output 0 echo ($b = = $c)? 1:0;   Output 1 echo ($b = = = $c)? 1:0; Output 0

Reference assignment in 3.foreach, see official documentation

In the official document there is a warning: the $value reference of the last element of the Warning array remains after the Foreach loop. It is recommended to use unset () to destroy it.

Let's look at a set of code:

$a = [1,2,3];foreach ($a as & $item) {echo $item. ‘,‘;} Unset ($item); Object foreach ($a as $item) {echo $item is not destroyed after reference assignment. ‘,‘;}

The output of the above code is as follows:

1,2,3,1,2,2 See the last output is 2, not 3, because the code is not destroyed $item caused by the following reasons:

The first loop, the reference to 3 is assigned to the $item, the second loop, the 1 is assigned to $item, because $item is a reference, causing the array of elements 3 into 1, understand?

4. The connection and difference between isset and empty

Empty returns True for the following 8 cases:

Null, empty string "", String 0 "0", empty array, Boolean value false, number 0, floating point 0.0, class with VAR defined but not assigned value www.stuhack.com

The isset detects if the variable is set and is not NULL, but for 8 cases of empty, only NULL returns false, and the other 7 cases return true

In summary, except for the 7 non-null cases described in empty, in other cases, if (empty (variable)) is equivalent to if (!isset (variable))

5. STRRCHR function

The comments on the W3school site are as follows:

The STRRCHR () function finds the last occurrence of a string in another string and returns all characters from that position to the end of the string.

If it fails, it returns false.

In fact, this function is looking for a character, not a search string, and should refer to the official document

code example:

$a = ' abcdef.txt '; $b = '. php '; Echo strrchr ($a, $b);

The above code output is:. txt

That is, if $b is a string, only the first character is used, and the following other characters are ignored

Note: PHP provides the STRSTR function, why not provide the STRRSTR function, although the implementation is very simple


How many holes in PHP have you fallen into?

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.