1 Quotes
About double quotes "", single quotes''
If you don't escape, use single quotes, because double quotes will search for variables, and single quotes only wrap strings
2 Arrays pushed onto the stack
$arr [] = 'x'; instead of array_push($arr, 'x'), there is no consumption of function calls
3 Multi-branch selection
switch case instead of multiple if else. By the way, when there is only one correct output, but There are many mistakes to be eliminated in the middle. Don’t use multiple if elses. You can do this, which will make the code more elegant
function demo(){ if () return false; if () return false; if () return false; return 'x'; }
4 The choice of object-oriented and process-oriented
Things that can be simplified, try to simplify them as much as possible. Don’t try to build everything When making a class, the instantiation of the class consumes more memory than the function call. The instantiation of the class requires allocating heap memory, not stack memory
5 Try to use the functions that come with PHP
Don’t think of implementing it yourself, PHP The built-in functions are all written in C language. Can you write it in PHP more efficiently than in C?
6 Try not to use global variables
global, global variables affect the program structure,
7 This is PHP, Not c/c++
In c, i++ only requires one cpu instruction, so I always thought that $i++ was very efficient, but the frameworks I saw (such as tp) did not use $i++, but $i + = 1, I realized that I am too young, $i++ requires 4 opcodes in php
8 Memory leaks There are generally no memory leaks in php (it exits after running, there is no resident process), but I encountered it A program written by a colleague returned an empty page inexplicably, but the code logic seemed to be OK. I used xdebug to debug and found that the recursion has no exit, resulting in infinite calls, so the program may have a memory leak