Although PHP is powerful, it does not have any disadvantages. Like other languages, it also needs to be updated and modified continuously in actual application compiling.Code, Until satisfied. Next we will share with you some tips on optimizing PHP code performance.
PHP code performance optimization 1. Do not copy variables at will
Sometimes, to make PHP code more clean, some new PHP users (including me) will copy the predefined variables to a variable with a shorter name, in fact, the result of this operation is a doubling of the memory consumption, onlyProgramSlower. In the following example, if a user maliciously inserts KB of text into the text input box, 1 MB of memory will be consumed!
- Bad:
- $Description =$ _ Post ['description'];
- Echo$ Description;
- Good:
- Echo$ _ Post ['description'];
Reference
PHP code performance optimization 2. Use single quotes for strings
The PHP engine allows single quotes and double quotes to encapsulate string variables, but there is a big difference! The double quotation mark string is used to tell the PHP engine to read the string content, find the variable, and change it to The value corresponding to the variable. Generally, strings do not have variables, so using double quotation marks will lead to poor performance. It is best to use a string connection instead of a double quotation mark string.
-
- Bad:
-
- $Output ="ThisIsAPlainString";
-
- Good:
-
- $Output ='ThisIsAPlainString';
-
- Bad:
-
- $Type ="Mixed";
- $Output ="ThisIsA$ TypeString";
-
- Good:
-
- $Type ='Mixed';
-
- $Output ='ThisIsA' .$ Type.'String ';
Reference
PHP code performance optimization 3. Use the echo function to output strings
The echo () function is used to print the result, which is easier to read. In the next example, you can see that the result has better performance.
- Bad:
- Print ($ myvariable );
- Good:
- Echo$ Myvariable;
Reference
PHP code performance optimization 4. Do not use connectors in Echo
Many PHP programmers (including me) Don't know how to use commas to separate multiple variables when outputting them with foul smell, instead of using strings to connect them first, in the first example below, there will be performance problems because the connector is used, because this will require the PHP engine to First connect all the variables and then output them, in the second example, the PHP engine will output them in sequence.
- Bad:
- Echo'Hello,MyNameIs'.$ Firstname.$ Lastname.'AndILiveIn'.$ City;
- Good:
- Echo'Hello,MyNameIs',$ Firstname,$ Lastname,'AndILiveIn',$ City;
Reference
PHP code performance optimization 5. Use switch/case instead of IF/else
The use of switch/case statements instead of IF/else statements for the judgment of only one variable will provide better performance, and the code will be easier to read and maintain.
-
- Bad:
- If ($ _ post ['action']='Add '){
-
- Adduser ();
-
- }Elseif($ _ Post ['action']='Delete '){
-
- Deleteuser ();
-
- }Elseif($ _ Post ['action']='Edit '){
-
- Edituser ();
-
- }Else{
-
- Defaultaction ();
-
- }
-
- Good:
-
- Switch ($ _ post ['action']){
-
- Case'Add ':
- Adduser ();
-
- Break;
-
- Case'Delete ':
-
- Deleteuser ();
-
- Break;
-
- Case'Edit ':
-
- Edituser ();
-
- Break;
-
- Default:
-
- Defaultaction ();
-
- Break;
-
- }