In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.