Ten PHP programming habits help you find a job-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1. A string enclosed in single quotes
When double quotation marks are used to enclose strings, the PHP interpreter replaces and escapes the variables, for example, "\ n ". If you only want to output a basic string, use single quotes to save some resources. Of course, if you need to replace the variable, you must use double quotation marks, but in other cases, you should still use single quotation marks.
2. String output
Which of the following statements can run at the fastest speed?
Print "Hi my name is $ a. I am $ B ";
Echo "Hi my name is $ a. I am $ B ";
Echo "Hi my name is". $ a. ". I am". $ B;
Echo "Hi my name is", $ a, ". I am", $ B;
Echo 'Hi my name is ', $ a,'. I am', $ B;
This may seem strange, but in fact the last line is the fastest. Print is slower than echo, and it is slower to replace the variable in the string, and the connection string is slower than using a comma to connect. The last sentence is the embodiment of the first habit. Therefore, replacing variables without strings will not only speed up the program running, but also make your code more understandable in any syntax highlighted Editor (variables will be highlighted ). Few people know that echo parameters can be connected with commas, and the speed is faster than string connection. The first habit is used, so this statement is very good.
3. Use single quotes in array Indexes
As you have seen in the above test questions, I pointed out that $ x [sales] is strictly incorrect. Indexes should be included, that is, $ x ['sales']. This is because PHP identifies an unenclosed index as a "Bare" string and interprets it as a constant. When the definition of the constant is not found, it is interpreted as a string, so this statement can be run. Excluding indexes can save this part of work. If you want to use this string to define constants in the future, there will be no errors. I have even heard that this is about seven times faster, although I have not personally tested it.
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.