24 Tips for Improving Program Efficiency in PHP_PHP Tutorial

WBOY
Release: 2016-07-20 11:15:23
Original
747 people have browsed it

This article is reproduced from the book "300 Questions You Must Know in PHP". It is recorded here for future reference

(1) Use single quotes instead of double quotes to include strings, which will be faster. Because php will search for variables in strings surrounded by double quotes, but single quotes will not. Note: Only echo can do this, it is a "function" that can take multiple strings as parameters (echo is a language structure, not a real function).

(2) $row['id'] is much faster than $row[id].

(3) echo is faster than print, and multiple parameters of echo are used instead of string concatenation, such as: echo $str1,$str2

(4) foreach is more efficient. Try to use foreach instead of while and for loops.

(5) Determine the maximum number of loops before executing the for loop. Do not calculate the maximum value every loop. It is best to use foreach instead.

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

(7) Try not to use _get, _set, _autoload.

(8) When using include() to include files, try to use absolute paths, because it saves PHP the time to find files in include_path, and it will take less time to parse the operating system path.

(9) If you want to get the time when the script starts executing (that is, the server receives the client request), using $_SERVER['REQUEST_TIME'] is better than time().

(10) Try to use PHP built-in functions to implement functions in the project, and colleagues try to use functions instead of regular expressions to complete the same functions.

 (11) The str_replace function is faster than the preg_replace function, but the strtr function is more efficient than the str_replace function.

(12) Using a selective branch statement (switch) is better than using multiple if, else if statements.

(13) It is not recommended to use @ to block error messages, as its efficiency is very low.

(14) Turning on Apache’s mod_deflate module can improve the browsing speed of web pages.

(15) When connecting to the database, try to use short connections and close used database connections in a timely manner.

 (16) Increasing local variables in the method is the fastest. Almost as fast as calling local variables in a function.

 (17) Methods in derived classes run faster than the same methods defined in base classes. If you can define a class method as static, try to define it as static, and its speed will increase a lot.

(18) Conduct file operations as little as possible, although PHP’s file operations are not inefficient.

(19) The time for Apache to parse a php script is much slower than parsing a static HTML page. Try to use more static HTML pages and less PHP scripts.

 (20) Unless the script can be cached, it will be recompiled every time it is called. Introducing a PHP caching mechanism can usually improve performance by 25% to 100% to avoid compilation overhead.

 (21) When operating a string and need to check whether its length meets certain requirements, the first idea is to use the strlen() function. This function executes quickly because it does not do any calculations and just returns the known string length stored in the zval structure (C's built-in function structure used to store PHP variables). However, since strlen() is a function, it will be somewhat slow because the function call will go through many steps. In some cases, isset() can be used to speed up code execution. For example:

 

if(strlen($foo)<5){ echo "Foo is too short" $$}

 if(!isset($foo{5})){ echo "Foo is too short" $$}

 ?>

Calling isset() happens to be faster than strlen(), because unlike strlen(), isset(), as a language construct, means that its execution does not require function lookup and letter lowercase. That is, there's actually not a lot of overhead spent in the top-level code checking the string length.

(22) When executing the increment or decrement of variable $i, $i++ will be slower than ++$i (php language only)

 (23) Everything does not have to be object-oriented (OOP). Object-oriented is often very expensive, and each method and object call consumes a lot of memory.

(24) If 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. However, be aware of the PHP version issue with file_get_contents when opening a URL file.

I haven’t verified the above content yet. How to improve efficiency? Let’s wait until I learn the low-efficiency fool’s method first. O(∩_∩)O Ha!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440192.htmlTechArticleThis article is reproduced from the book "300 Questions You Must Know in PHP". It is recorded here for future reference (1) Use Use single quotes instead of double quotes to enclose the string, which is faster. Because php will...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!