php performance efficiency optimization
Recently, I am self-studying and writing PHP programs in the company. Since the company has high requirements for the running efficiency of the program, and I am a novice, it is important to pay attention to the efficiency of the program from the beginning. Here I will summarize the efficiency of the PHP program based on some information on the Internet. Some strategies for optimization:
1. When file_get_contents can be used instead of file, fopen, feof, fgets and other series of methods, try to use file_get_contents because it is much more efficient! But please pay attention to the PHP version problem of file_get_contents when opening a URL file
2. Conduct file operations as little as possible, although PHP’s file operations are not inefficient;
3. Optimize the Select SQL statement and perform as few Insert and Update operations as possible (I was criticized for updating);
4. Use PHP internal functions as much as possible (but in order to find a function that does not exist in PHP, I wasted time that could have been written a custom function, a matter of experience!);
5. Do not declare variables inside the loop, especially large variables: objects (this seems to be not just a problem in PHP, right?);
6. Try not to loop nested assignments in multi-dimensional arrays;
7. Do not use regular expressions when you can use PHP’s internal string manipulation functions;
8. foreach is more efficient, try to use foreach instead of while and for loop;
9. Use single quotes instead of double quotes to quote strings;
10. "Use i+=1 instead of i=i+1. It conforms to the habits of c/c++ and is more efficient";
11. Global variables should be unset()ed when used up