PHP code performance optimization 1. Don’t just copy variables
Sometimes in order to make the PHP code tidier, some PHP novices (including me) ) will copy the predefined variable to a variable with a shorter name. In fact, this will double the memory consumption and will only make the program slower. Just imagine, in the following example, if the user maliciously inserts 512KB of text into the text input box, this will cause 1MB of memory to be consumed!
<ol class="dp-xml"> <li class="alt"><span><span>BAD: </span></span></li> <li> <span>$</span><span class="attribute">description</span><span> = $_POST['description']; </span> </li> <li class="alt"><span>echo $description; </span></li> <li><span>GOOD: </span></li> <li class="alt"><span>echo $_POST['description']; </span></li> </ol>
Quote
PHP code performance optimization 2. Use single quotes for strings
The PHP engine allows the use of single quotes and double quotes to encapsulate string variables, but there is a big difference! Using double-quoted strings tells the PHP engine to first read the contents of the string, find the variables in it, and change them to the values corresponding to the variables. Generally speaking, strings have no variables, so using double quotes will lead to poor performance. It's better to use string concatenation instead of double quoted strings.
<ol class="dp-xml"> <li class="alt"><span><span>BAD: </span></span></li> <li> <span>$</span><span class="attribute">output</span><span> = </span><span class="attribute-value">"This is a plain string"</span><span>; </span> </li> <li class="alt"><span>GOOD: </span></li> <li> <span>$</span><span class="attribute">output</span><span> = </span><span class="attribute-value">'This is a plain string'</span><span>; </span> </li> <li class="alt"><span>BAD: </span></li> <li> <span>$</span><span class="attribute">type</span><span> = </span><span class="attribute-value">"mixed"</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">output</span><span> = </span><span class="attribute-value">"This is a $type string"</span><span>; </span> </li> <li><span>GOOD: </span></li> <li class="alt"> <span>$</span><span class="attribute">type</span><span> = </span><span class="attribute-value">'mixed'</span><span>; </span> </li> <li> <span>$</span><span class="attribute">output</span><span> = </span><span class="attribute-value">'This is a '</span><span> . $type .' string'; </span> </li> </ol>
Quote
PHP code performance optimization 3. Use the echo function to output a string
Using the echo() function to print the results is not only easier to read, but in the next example, you can also see better performance.
<ol class="dp-xml"> <li class="alt"><span><span>BAD: </span></span></li> <li><span>print($myVariable); </span></li> <li class="alt"><span>GOOD: </span></li> <li><span>echo $myVariable; </span></li> </ol>
Quote
PHP code performance optimization 4. Do not use connectors in echo
Many PHP programmers (including me) don’t know that when outputting multiple variables with stink, you can actually use commas to separate them, instead of concatenating them with strings first. As in the first example below, there will be performance problems due to the use of connectors, because this will require the PHP engine to first connect all the variables and then output, while in the second example, the PHP engine will They will be output in order.
<ol class="dp-xml"> <li class="alt"><span><span>BAD: </span></span></li> <li><span>echo 'Hello, my name is' . $firstName . $lastName . ' and I live in ' . $city; </span></li> <li class="alt"><span>GOOD: </span></li> <li><span>echo 'Hello, my name is' , $firstName , $lastName , ' and I live in ' , $city; </span></li> </ol>
Quote
PHP code performance optimization 5. Use switch/case instead of if/ else
For judgments with only a single variable, using switch/case statements instead of if/else statements will have better performance, and the code will be easier to read and maintain.
<ol class="dp-xml"> <li class="alt"><span><span>BAD: </span></span></li> <li><span>if($_POST['action'] == 'add') { </span></li> <li class="alt"><span>addUser(); </span></li> <li><span>} elseif ($_POST['action'] == 'delete') { </span></li> <li class="alt"><span>deleteUser(); </span></li> <li><span>} elseif ($_POST['action'] == 'edit') { </span></li> <li class="alt"><span>editUser(); </span></li> <li><span>} else { </span></li> <li class="alt"><span>defaultAction(); </span></li> <li><span>} </span></li> <li class="alt"><span>GOOD: </span></li> <li><span>switch($_POST['action']) { </span></li> <li class="alt"><span>case 'add': </span></li> <li><span>addUser(); </span></li> <li class="alt"><span>break; </span></li> <li><span>case 'delete': </span></li> <li class="alt"><span>deleteUser(); </span></li> <li><span>break; </span></li> <li class="alt"><span>case 'edit': </span></li> <li><span>editUser(); </span></li> <li class="alt"><span>break; </span></li> <li><span>default: </span></li> <li class="alt"><span>defaultAction(); </span></li> <li><span>break; </span></li> <li class="alt"><span>} </span></li> </ol>