To write good code, this is need you accumulate over a long period in the normal development, if you have to notice at ordinary times the following code coding, so congratulations you, you are in the area of skills upgrading has foundation under some, to write good code, namely, you notice the performance of this problem, the code optimization is also part of the performance optimization. Let’s take a look at the coding you need to pay attention to. Also hope you can develop good habits!

1. It’s faster to include strings with single quotes instead of double quotes. Because PHP looks for variables in double-quoted strings, single quotes don’t. Note: only echo can do this, which is a “function” that takes multiple string arguments.

2. If you can define a class method as static, try to define it as static. It will be nearly four times faster.

3, Row [‘id’] is 7 times faster than row[‘id’].

Echo is faster than print and uses echo’s multiple arguments instead of string concatenation, e.g. Echo str1,str1,str2.

5. Determine the maximum number of loops before executing the for loop. Do not calculate the maximum foreach loop.

6. Unregister unused variables, especially large arrays, to free memory.

7. Try to avoid __get, __set, __autoload.

Require_once () is expensive.

9. Use absolute paths for include files, because it avoids the speed at which PHP finds files in the include_path and takes less time to resolve OS paths. $_SERVER[‘REQUEST_TIME’] is better than time() if you want to know when the script will start executing.

Functions do the same thing instead of regular expressions.

12. Str_replace is faster than preg_replace, but STRTR is four times as efficient.

13. If a string substitution function accepts arrays or characters as arguments, and the argument length is not too long, consider writing an extra substitution code so that the arguments are passed one character at a time, rather than just one line of code that accepts groups of arguments as queries and replacements.

14. It is better to use select branch statements than multiple if and else if statements.

15. Using @ to block error messages is very inefficient, very inefficient.

16, Open apache mod_deflate module, can improve the speed of web browsing.

17, the database connection should be closed when the use is finished, do not use long connection.

Error messages are expensive.

Increasing local variables in the method is the fastest. Almost as fast as calling a local variable in a function.

Incrementing a global variable is 2 times slower than a local variable.

21, incrementing an object attribute ($this->prop++) is 3 times slower than a local variable.

22. Incrementing an undefined local variable is 9 to 10 times slower than a predefined one.

23. Defining a local variable without calling it in a function also slows things down (by the same amount as incrementing a local variable). PHP will probably check to see if there are global variables.

24. Method calls appear to be independent of the number of methods defined in the class, since I added 10 methods (both before and after testing the methods) with no performance change. Methods in derived classes run faster than the same methods defined in base classes.

26. Calling an empty function with one argument takes the same amount of time as performing 7 or 8 local increment operations. A similar method call takes close to 15 local variable increment operations.

Apache takes 2 to 10 times longer to parse a PHP script than a static HTML page. Use more static HTML pages than scripts.

28. Unless the script can be cached, it is recompiled each time it is called. Introducing a PHP caching mechanism can typically improve performance by 25% to 100%, eliminating compilation overhead.

29. Do as much caching as possible, use memcached. Memcached is a high-performance in-memory object caching system that can be used to speed up dynamic Web applications and reduce database load. A cache of OP codes is useful so that scripts do not have to be recompiled for every request.

30. When you manipulate strings and need to verify that their length meets certain requirements, you naturally use strlen(). This function executes fairly quickly because it doesn’t do any calculations and just returns the known length of the string stored in the Zval structure, C’s built-in data structure for storing PHP variables.

However, since strlen() is a function, it is somewhat slower because the function call goes through various steps, such as lowercase, hash lookup, and follows the function being called. In some cases you can use the isset() technique to speed up the execution of your code.

If (strlen(foo) < 5) {echo “foo is too short” $$} (compare to the technique below) if (! Isset (foo{5})) {echo “foo is too short” $$} The isset() call happens to be faster than strlen() because, unlike strlen(), isset() is a language construct, meaning that it is executed without function lookup and lowercase letters. In other words, you don’t actually spend much overhead in the top-level code that checks the string length.

31. When I increment or decrement, I ++ will be slower than ++ I when I increment or decrement. This difference is SPECIFIC to PHP and does not apply to other languages, so please don’t modify your C or Java code and expect it to be instantly faster. It won’t work. ++ I slower. This difference is SPECIFIC to PHP and does not apply to other languages, so please don’t modify your C or Java code and expect it to be instantly faster. It won’t work. ++ I slower. This difference is SPECIFIC to PHP and does not apply to other languages, so please don’t modify your C or Java code and expect it to be instantly faster. It won’t work. ++ I is faster because it requires only three opcodes, whereas $I ++ requires four. Postincrementing actually produces a temporary variable, which is then incremented. Pre-increment increment directly increments the original value. This is a form of optimization processing, as Zend’s PHP optimizer does. It’s a good idea to keep this optimization in mind, because not all instruction optimizers do the same optimization, and there are a large number of Internet Service providers (ISPs) and servers that do not have an instruction optimizer.

32. Not everything is object oriented (OOP). Object orientation tends to be very expensive, with each method and object invocation consuming a lot of memory.

33. It is not necessary to implement all data structures with classes. Arrays are also useful.

34. Don’t break down methods too much. Think about what code you really want to reuse.

When you need it, you can always break the code into methods.

36. Use as many PHP built-in functions as possible.

37. If you have a lot of time-consuming functions in your code, you can consider implementing them as C extensions.

Profile your code. The validator will tell you which parts of the code are consuming how much time. The Xdebug debugger includes validators that, in general, show bottlenecks in your code.

39. Mod_zip can be used as an Apache module to instantly compress your data and reduce data transfer by 80%.

40. In cases where file_get_contents can be used instead of file, fopen, feof, fgets, etc., try to use file_get_contents because it is much more efficient! But be aware of the PHP version of file_get_contents when opening a URL file;

41, as little as possible file operation, although PHP file operation efficiency is not low;

42. Optimize Select SQL statements to do as little Insert and Update as possible (I was criticized on Update);

43, Use PHP internal functions as much as possible (but I wasted time writing a custom function when I could have found one that doesn’t exist in PHP, experience problem!) ;

44, Do not declare variables inside loops, especially large variables: objects. ;

45, Multidimensional array as far as possible do not loop nested assignment;

46. Do not use regular expressions when you can manipulate functions with PHP internal strings.

47, Foreach is more efficient, try to use foreach instead of while and for loops;

48. Use single quotation marks instead of double quotation marks to quote strings.

49. “Replace I = I +1 with I +1. C/C ++ convention and high efficiency.”

Unset (); unset();

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be free to share with you