📜 ⬆️ ⬇️

40 tips for optimizing your PHP code

Good day to all.

Translation of the note " 40 Tips for optimizing your php code ". Author - Reinhold Weber .

  1. If the method can be static, declare it static.
  2. echo is faster than print.
  3. Pass several parameters to echo instead of using string concatenation.
  4. Set the maximum number of passes of your for loops before the loop, and not during its execution.
  5. Delete your variables to free memory, especially if these are large arrays.
  6. Beware of magical methods such as __set, __get, __autoload.
  7. require_once is expensive.
  8. Specify full paths in the include / require constructs, less time will be spent searching for the file.
  9. If you need to determine the time when the script was launched, use $ _SERVER ['REQUEST_TIME'] instead of time ().
  10. Try to use strncasecmp, strpbrk and stripos instead of regular expressions.
  11. str_replace is faster than preg_replace, but strtr is faster than str_replace.
  12. If a function, like string replacement, can accept both arrays and single characters in its arguments, and if your argument list is not too long, consider writing several identical replacement expressions, passing one character at a time, instead of one line. code that takes an array as a search and replace argument
  13. It is better to choose statements using the else if construct than to use several if statements.
  14. Suppressing errors when using @ is very slow.
  15. Use the Apache module mod_deflate.
  16. Close your database connections when you're done with them.
  17. $ row ['id'] is seven times faster than $ row [id].
  18. Error messages are expensive
  19. Do not use functions inside a for loop condition, for example like this: for ($ x = 0; $ x <count ($ array); $ x). In this case, the count () function will be called with each pass of the loop.
  20. The increment of a local variable in the method is the fastest. Almost also the local variable increment in the function works.
  21. The increment of a global variable is two times slower than a local one.
  22. The increment property of an object (ie, $ this-> prop ++) is three times slower than a local variable.
  23. The increment of an undefined variable is 9-10 times slower than it was previously initialized.
  24. Declaring a global variable, without using it in a function, also slows down the work (by about the same amount as the increment of a local variable). PHP probably checks for the existence of a variable.
  25. The speed of the method call, apparently, does not depend on the number of methods defined in the class. I added 10 methods to the test class (before and after the test method), without changing the performance.
  26. Methods in derived classes are faster than the ones defined in the base class.
  27. Calling a function with one parameter and an empty function body is on average 7-8 increments of a local variable ($ localvar ++). Calling a similar method is, of course, about 15 increments.
  28. Your strings defined with the help of 'rather than' will be interpreted a little faster, because PHP searches for variables inside "..", but not '...'. Of course, you can use it only when There are no variables in the row.
  29. Strings separated by commas are displayed faster than strings separated by periods. Note: this only works with the echo function, which can take multiple lines as arguments.
  30. PHP scripts will be processed at least 2-10 times slower than static HTML pages. Try using more static HTML pages and fewer scripts.
  31. Your PHP scripts are recompiled every time if the scripts are not cached. Caching scripts typically increases performance by 25-100% by removing compile time.
  32. Cache as much as possible. Use memcached is a high-performance system of caching objects in memory, which improves the speed of dynamic web applications by facilitating the loading of the database. The cached microcode is useful because it allows your script to not compile again for each request.
  33. When working with strings, when you need to make sure that the string is of a certain length, you certainly want to use the strlen () function. This function works very quickly, because it does not perform any calculations, but only returns the already known length of the string available in the zval-structure (internal structure C, used when working with variables in PHP). However, because strlen () is a function, it will work slowly by calling some operations, such as casting a string to lower case and searching the hash table, only after which the main functions of the function will be performed. In some cases, you can speed up your code by using tricks with isset ().
    It was: if (strlen ($ foo) <5) {echo "Foo is too short"; }
    It became: if (! Isset ($ foo {5})) {echo "Foo is too short"; }
    A call to isset () is faster than strlen () because, unlike strlen (), isset () is not a function, but a language construct. Due to this, isset () has almost no overhead for determining the length of the string.
  34. Increment or decrement of a variable with $ i ++ is a bit slower than ++ $ i. This is a special feature of PHP, and you do not need to modify your C and Java code in this way, thinking that it will work faster, this will not happen. ++ $ i will be faster in PHP because instead of four commands, as is the case with $ i ++, you only need three. Post-increment is usually used when creating temporary variables, which then increase. While the pre-increment increases the value of the original variable. This is one of the ways to optimize PHP-code in byte-code utility Zend Optimizer. However, this is a good idea, since not all bytecode optimizers optimize this, there are also a lot of scripts that work without optimizing into bytecode.
  35. Not everything should be OOP, often this is unnecessary, since each method and object takes a lot of memory.
  36. Do not define each data structure as a class; arrays are very useful.
  37. Don't break up the methods too much. Think that you really will reuse.
  38. You can always break the code into methods later, if necessary.
  39. Use countless predefined functions.
  40. If your code has functions that take a very long time, consider writing them in C as an extension.
  41. Profile your code. Profiling will show you how much time your code parts are running.
  42. mod_gzip is an Apache module that allows you to compress your data on the fly and can reduce the amount of data transferred by up to 80%.
  43. Extended article on optimization by John Lim.
    ')
    I'd add that not all of the tips here I agree. But translation is translation.

Source: https://habr.com/ru/post/22799/


All Articles