PHP code optimization techniques

PHP code optimization techniques

echo is faster than print.

Instead of using multiple echo parameter string concatenation.

Prior to determining the maximum number of cycles for loop, not in the loop will calculate the maximum, best use foreach instead.

For global variables, you should be spent on the unset () away.

Instead of double quotes in single quotes to contain the string, this will be faster.

Because PHP looks for variables inside of double quotes, the single quotation marks will not.

Regex function instead perform the same function. When the execution is incremented or decremented variable $ i, $ i ++ i will be slower than some of the $ ++.

This is something PHP specific and does not apply to other languages, ++ $ i is because it only requires three instructions (opcodes), $ i ++ you will need four instructions faster.

Postincrement will actually generate a temporary variable, this temporary variable is then incremented.

The pre-incrementation increases the original value directly.

Use select statements (switch case) is better than using multiple if, else if statements.

Use var_dump be PHP code debugging.

If you are looking for php debugging techniques, I must say you're looking var_dump should be the goal of this command is to meet all your needs in terms of information display php, but in most cases debugging code and get the value in PHP related.

Use full paths in includes the time required to resolve the OS paths less.

Easily create a global value is a bad idea, but the actual situation is indeed sometimes they need to do so.

For a database table or database connection information using the global value is a good idea, but do not use global values ​​often in your PHP code.

In addition, a better approach is to put your global variables in a config.php file.

If you want to know the time the script started executing, using $ _SERVER [ 'REQUEST_TIME'] is better than the time ().

Mod_deflate open the apache module.

Screen error message with @ approach is very inefficient.

Try using a large number of PHP built-in functions.

Incrementing a local variable is not predefined increments than a predefined local variable is 9-10 times slower.

Method of running a derived class defined in the same manner as the base class to be faster.

Only define a local variable without using it in a function also slows things down (same amount as incrementing a local variable) Apache A PHP script than a static HTML page 2-10 times slower.

Try to use more static HTML pages and fewer scripts. As mentioned before, any php website is the most important part of the 99% chance that database.

Therefore, you need to be very familiar with how to properly use sql, table and learn to associate more advanced database technology.

Empty function call with a parameter corresponds to the time it takes to perform 7-8 times localvar operation.

When the need to check the operating string and its length meets certain requirements, assume that you would use strlen () function.

This function is pretty fast, because it does not do any calculations, returns only zval structure (C built-in data structures used to store variables in PHP) a known length of the string stored.

Not all cases have to be developed using object-oriented, object-oriented often much overhead, each method and object call consumes a lot of memory.

Unless the script can be cached, or each call will be recompiled. Install a PHP caching mechanism can usually upgrade 25-100% performance by removing compile times.

PHP code optimization Notes

Is not equal to the short code, fast code

A lot of people want to write code more concise as possible when writing programs, but the shorter the code but sometimes takes longer time to execute. When writing the program should focus on the expansion of the program, rather than the pursuit of speed. Prior to optimize your code, take a look at part with the database-related bottlenecks because most applications in the database rather than code.

Guess you like

Origin www.cnblogs.com/gupiao777/p/11622461.html