Some common pitfalls during PHP Development-PHP Tutorial

Source: Internet
Author: User
Some common pitfalls during PHP development. During PHP development, some common pitfalls 1. the comment of the strrchr function on the W3School site is as follows: the strrchr () function searches for the position where the string last appears in another string, some common pitfalls during PHP Development

1. strrchr function

Note the following on the W3School website:

The strrchr () function finds the position of the last occurrence of a string in another string, and returns all characters from this position to the end of the string.
Otherwise, false is returned.

In fact, this function is used to search for a character rather than a string. please refer to the official documentation.

Sample code:

$a = 'abcdef.txt';$b = '.php';echo strrchr($a, $b);
The above code output is:. txt

That is to say, if $ B is a string, only the first character is used.

Note: php provides the strstr function. Why does it not provide the strrstr function? it is easy to implement it by yourself.


2. Comparison of null, null, and 0 values
In PHP, "=" first converts the data type and then compares the data type, while "=" first compares the data type. if the data type is different, the result is not equal. See 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
I used to write only js or C # code for coders. I have been fooled by these three values for n times, and n is greater than 3.


3. assign values to references in foreach, See the official documentation

This reference assignment is very good. for me who use C #, it is impossible to modify the foreach element in C #. an exception occurs. php makes this possible, however:
In the official document, there is a Warning: the $ value reference of the last element of the Warning array will be retained after the foreach loop. We recommend that you use unset () to destroy it.
Let's look at a group of code:

$ A = [1, 2, 3]; foreach ($ a as & $ item) {echo $ item. ',';} // unset ($ item); // after the value is referenced, the object foreach ($ a as $ item) {echo $ item is not destroyed. ',';}
The output of the above code is as follows:
1, 2, 3, 1, 2, the last output is 2, not 3, because $ item is not destroyed in the code. The reason is as follows:
In the first loop, the reference of 3 is assigned to $ item. in the second loop, 1 is assigned to $ item because $ item is a reference, as a result, Element 3 of the array changes to 1. do you understand?

4. relationship between isset and empty, Isset document empty document
Empty returns true for the following eight cases:
Null, empty string "", string 0 "0", empty array, boolean value false, number 0, floating point number 0.0, class defined with var but not assigned value

Isset checks whether the variables are set and is not NULL. However, for empty, only null returns false, and true for other 7 cases.

To sum up, except for 7 non-null cases described by empty, if (empty (variable) is equivalent to if (! Isset (variable ))


Comment 1. comment the strrchr function on the W3School site as follows: the strrchr () function searches for the position where the string last appears in another string, and...

Related Article

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.