40 tips for getting PHP code to fly

  • 2020-03-31 20:37:58
  • OfStack

1. If a method can be static, declare it static, and the speed is increased by 1/4;
2. Echo is more efficient than print, because echo does not return a value, and print returns an integer;
3. Set the maximum number of loops before the loop, instead of in the loop;
4. Destroy variables to free memory, especially large arrays;
5. Avoid using magic methods like arbitration, arbitration set, arbitration autoload, etc.;
6. Requiere_once () comparison consumes resources;
7. Use absolute paths in includes and requires to spend less time analyzing paths;
$_SERVER['REQUSET_TIME'] is better than time() if you need the time from sexinsex to script execution.
9. If you can use character processing functions, try to use them, because the efficiency is higher than the regular;
10. Str_replace character replacement is faster than regular replace preg_replace, but STRTR is 1/4 faster than str_replace;
11. If a function can accept both arrays and simple characters as arguments, such as character substitution, and the list of arguments is not too long, consider using more concise substitution statements, replacing only one character at a time, rather than accepting arrays as search and replace arguments. Let's see, 1+1> 2;
12. Using @ to cover up errors will slow down the script;
13.$row['id'] is 7 times faster than $row[id].
Error messages are useful;
Don't use functions in loops, such as For($x=0; $x < Count ($array); $x), the count() function is evaluated outside;
16. The fastest way to create local variables is in methods, 97oo is almost as fast as calling local variables in methods;
17. It is 2 times slower to set up a global variable than a local variable;
Create an object attribute (a variable in a class) such as ($this-> Prop++) is 3 times slower than local variables;
19. Creating an undeclared local variable is 9-10 times slower than an initialized local variable;
Declaring a global variable that has not been used by any of the functions also degrades performance (as does declaring the same number of local variables). PHP might check to see if the global variable exists;
21. The performance of methods is not related to the number of methods defined in a class, because there is no difference in performance when I add 10 or more methods to the class under test (before and after the test methods);
22. The performance of methods in subclasses is better than that in base classes;
23. The time it takes to run a function with only one argument and an empty body is equal to 7-8 $localvar++ operations, while a similar method (a function in the class) is equal to about 15 $localvar++ operations.
Surrounding your string by 'instead of "will make things interpret a little faster since PHP looks for variables inside"... But not inside '... 'Of course you can only do this when you don't need to have variables in the string.'
25. It is faster to use commas instead of dots when output strings. Note: this only works with echo. This function accepts strings as arguments.
26. In apache server, a PHP script page will take at least 2-10 times more time than the corresponding HTML static page generation, it is recommended to use more static HTML pages and a small number of steps;
27. Unless your installation has a cache, your PHP scripts need to be recompiled every time they are accessed. It is recommended that you install a PHP cache to significantly improve your 20-100% performance by removing some duplicate compilations.
28. It is recommended to use memcached, a high-performance distributed memory object caching system, to improve the performance of dynamic network applications and reduce the burden on the database;
29. Use the ip2long() and long2ip() functions to convert IP addresses into integers and store them in the database instead of characters. This reduces storage by almost a quarter. It also makes it easy to sort and find addresses quickly.
30. Use checkdnsrr() to validate some email addresses by domain name existence. This built-in function ensures that each domain name corresponds to an IP address.
31. If you are using php5 or older versions of mysql4.1, consider using the improved function mysqli_*;
32. Try to prefer the ternary operator (? :);
33. Before you want to completely redo your project, see if PEAR has anything you need. PEAR is a huge repository that many PHP developers know;
34. Use highlight_file() to automatically print a nicely formatted copy of the page's source code;
35. Use the error_reporting(0) function to prevent potentially sensitive information from being displayed to the user. Ideally, error reporting should be completely disabled in php.ini files. However, if you're using a Shared virtual host and you can't modify php.ini, you'd better add the error_reporting(0) function to the first line of each script file (or load it with require_once()).
36. Use gzcompress() and gzuncompress() to compress(uncompress) large volumes of strings as they are stored (retrieved) in the database. This built-in function can be compressed to 90% using the gzip algorithm;
37. To have multiple return values for a function by reference to the address of the argument variable. You can put an "&" before a variable to indicate that it is passed by address rather than by value;
38.Fully understand "magic quotes" and the quotes of SQL injection. I'm hoping that most developers read this are already familiar with SQL injection. I list it here because it's absolutely critical to understand. If you've never heard the term before, spend the entire rest of the day googling and reading.
39. The speed of using strlen() is not good because some other operations such as lowercase and hash table query are to be invoked. We can use isset() to achieve similar functions, and isset() is faster than strlen().
40.When increthings or decrementing the value of the variable $I ++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, So don 't go modifying your C or Java code thinking it' l l suddenly become faster -, It won 't + + $I happens to be faster - home because in PHP 4 opcodes, informs the for $i++ you only need 3. Post incrementation later causes in the creation of a temporary var that is then incremented. While pre-incremented weeks the original value directly. This is one of the optimization that opcode optimized like Zend's PHP optimizer. It is a still a good idea to keep in mind since not all Opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.

Related articles: