When you go to PHP development, some common pits

Source: Internet
Author: User

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


2, null and empty, 0, three values comparison
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 1echo ($a = = = $b)? 1:0;   Output 0echo ($a = = $c)? 1:0;    Output 1echo ($a = = = $c)? 1:0;   Output 0echo ($b = = $c)? 1:0;    Output 1echo ($b = = = $c)? 1:0;   Output 0
For me this previously only write JS or C # code farm, by the 3 values have been fooled n times, n greater than 3


3. Reference assignment in foreach , see official documentation

This reference is very good, wow, for me in C #, in C # to modify the elements of foreach, it is not possible, it will be abnormal drop, PHP to turn this into a possible, but:
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, Isset and empty contact and difference , isset document empty document
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

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))


When you go to PHP development, some common pits

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.