Get the App
SLTechnology News&Howtos  ›  Development  › 

Skills of PHP Code performance Optimization

Shulou Source: shulou.com Published: 2022-06-03 19:03:36 10月03日 Update

This article focuses on "PHP code performance optimization skills", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "PHP code performance optimization skills" it!

PHP code performance optimization 1. Don't copy variables casually

Sometimes, in order to make the PHP code cleaner, some PHP novices (including me) copy predefined variables into a variable with a shorter name, which actually doubles the memory consumption and only makes the program slower. Imagine that in the following example, if the user maliciously inserts 512KB bytes of text into the text input box, this will cause 1MB's memory to be consumed!

BAD: $description = $_ POST ['description']; echo $description; GOOD: echo $_ POST [' description']

Quote

PHP code performance optimization 2. Use single quotation marks on strings

The PHP engine allows you to use single and double quotes to encapsulate string variables, but there is a big difference! The string in double quotation marks tells the PHP engine to read the string first, find the variable in it, and change it to the corresponding value of the variable. Generally speaking, strings have no variables, so using double quotes can lead to poor performance. * * is a string concatenation instead of a double quotation mark string.

BAD: $output = "This is a plain string"; GOOD: $output = 'This is a plain string'; BAD: $type = "mixed"; $output = "This is a $type string"; GOOD: $type =' mixed'; $output = 'This is a'. $type.' String'

Quote

PHP code performance optimization 3. Use the echo function to output a string

In addition to using the echo () function to print results that are easier to read, you can see better performance in the next example.

BAD: print ($myVariable); GOOD: echo $myVariable

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 stench, you can actually use commas to separate them instead of using strings to concatenate them first. As in the following * * example, there are performance problems due to the use of connectors, because this requires the PHP engine to first concatenate all variables and then output them, and in the second example The PHP engine will output them sequentially.

BAD: echo 'Hello, my name is'. $firstName. $lastName. ' And I live in'. $city; GOOD: echo 'Hello, my name is', $firstName, $lastName,' and I live in', $city

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.

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;} so far, I believe you have a deeper understanding of "PHP code performance optimization techniques", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Performance code variables characters strings quotes examples engines outputs techniques content memory functions programs results statements facets learning consumption utility Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi vpn Microsoft Redmi Shulou Tech Info