1-Writing Modular Code
A good PHP code should be a modular code. PHP's object-oriented programming features are particularly powerful tools that can break down your application into functions or methods. You should separate the front-end HTML/CSS/JAVASCRIPT code from the server side of your application as much as possible. You can also follow the MVC (model-View-controller) pattern on any PHP framework.
2-Code Writing specification
Good PHP code should have a complete code-writing specification. By naming the variables and functions, a unified approach to accessing the database and handling the errors, and the same code indentation, and so on to achieve the programming specifications, so that your code can be more readable.
3-Writing Portable Code
Good PHP code should be portable. You can use PHP's existing features, such as magic quotes and short tags. Try to understand your needs and then write code that is independent and portable by adapting to PHP features.
4-Writing security Code
Good PHP code should be safe. The PHP5 provides excellent performance and flexibility. But the security issue lies entirely with developers. For a professional PHP developer, deep understanding of critical security vulnerabilities is critical, such as cross-site scripting (XSS), cross-station request forgery (CSRF), Code injection vulnerability, and character encoding vulnerability. By using PHP's special functions and functions, such as: Mysql_real_escape_string and so on, you can write safe code.
5-code Comment
Code comments are an important part of your code. Code comments can tell what the variable or function does, which will be useful in future code maintenance.
6-Avoid short labels
Replace all of the short tags with the full php tag.
7-use single quotation marks instead of double quotes
strings always use single quotes instead of double quotes to avoid performance degradation caused by variables within the PHP search string. Using single quotes instead of double quotation marks to contain strings can be faster. Because PHP searches for a variable in a string surrounded by double quotes, the single quotation mark does not
8-Escape String output
It is a good practice to use ent_quotes as a parameter to pass to the Htmlspecialchars function to ensure that single quotation marks (') are also converted to HTML entities.
9-separating string output with commas
Use the Echo statement to output a string separated by commas (,), rather than using the string join operator (.). Performance is better.
10-Check the value before output
Check the value passed before output $_get[' query ']. You can use the Isset or empty function to check whether a variable is a null value.
11-Other
- If you can define a method of a class as static, try to define it as static, and it will rise nearly 4 times times faster.
- $row [' ID '] is 7 times times the speed of $row[id].
- echo is faster than print, and uses Echo's multiple parameters (referring to commas instead of periods) instead of string connections, such as Echo $str 1, $str 2.
- To determine the maximum number of loops before executing a for loop, do not calculate the maximum value once per loop, preferably using foreach instead.
- Unregister those unused variables, especially large arrays, in order to free up memory.
- Try to avoid using __get,__set,__autoload.
- Require_once () is costly.
- Include files with absolute paths as much as possible, because it avoids the speed with which PHP can find files in include_path, and it takes less time to parse the operating system path.
- If you want to know when the script starts executing (the server side receives a client request), use $_server[' request_time '] better than time ().
- function to perform the same function instead of a regular expression.
- The Str_replace function is faster than the Preg_replace function, but the efficiency of the STRTR function is four times times that of the Str_replace function.
- If a string replaces a function that accepts an array or character as an argument, and the parameter length is not too long, consider writing an additional replacement code so that each pass argument is a character instead of just one line of code that accepts the array as a parameter for querying and replacing.
- It is better to use multiple if,else if statements using the Select Branch statement (that is, switch case).
- Blocking error messages with @ is very inefficient and extremely inefficient.
- Open the Apache Mod_deflate module to improve the browsing speed of your Web pages.
- The database connection should be turned off when it is finished, not with a long connection.
- Error messages are costly.
- Incrementing a local variable in a method is the fastest speed. is almost equivalent to the speed at which local variables are called in the function.
- Incrementing a global variable is twice times slower than incrementing a local variable.
- Incrementing an object property, such as $this->prop++, is 3 times times slower than incrementing a local variable.
- Incrementing a non-predefined local variable is 9 to 10 times times slower than incrementing one of the pre-defined local variables.
- Defining only a local variable without calling it in the function also slows down the speed (which is equivalent to incrementing a local variable). PHP will probably check to see if there are global variables.
- The method invocation appears to be independent of the number of methods defined in the class, because I added 10 methods before and after the test method, but there was no performance change.
- A method in a derived class runs faster than the same method that is defined in the base class.
- Calling an empty function with one parameter takes a time equivalent to performing a local variable increment operation of 7 to 8 times. A similar method call takes a time close to 15 local variable increment operations.
- Apache Parsing a PHP script is 2 to 10 times times slower than parsing a static HTML page. Use static HTML pages as much as possible and use fewer scripts.
- Unless the script can be cached, it will be recompiled every time it is called. Introducing a set of PHP caching mechanisms can typically improve performance by 25% to 100% to eliminate compilation overhead.
- Try to cache and use memcached. The memcached is a high-performance memory object caching system that can be used to accelerate dynamic Web applications and reduce database load. Caching of the OP code is useful so that the script does not have to recompile for each request.
- When you manipulate a string and need to verify that its length satisfies a certain requirement, you will of course use the strlen () function. This function executes quite quickly because it does not do any calculations and only returns the length of the known string stored in the Zval structure (the built-in data structure of C for storing PHP variables). However, because strlen () is a function, it is somewhat slower, because function calls go through a number of steps, such as lowercase letters (lowercase, PHP does not differentiate between function names), and hash lookups, which follow the called functions. In some cases, you can use the isset () technique to speed up execution of your code.
(Example below)
(compare with the following tips) if (!isset ($foo [5])) {echo ' foo is too Short ';}
Calling Isset () happens to be faster than strlen (), because unlike the latter, Isset (), as a language structure, means that its execution does not require function lookups and lowercase letters. In other words, you're actually not spending much overhead in the top-level code that examines the length of the string.
- When the variable $i is incremented or decremented, $i + + will be slower $i than the + +. This difference is unique to PHP and does not apply to other languages, so please do not modify your C or Java code and expect them to become fast and useless. + + $i is faster because it requires only 3 instructions (opcodes), $i + + requires 4 instructions. The post increment actually produces a temporary variable, which is then incremented. The predecessor increment is incremented directly on the original value. This is one of the most optimized processing, as Zend's PHP optimizer did. It is a good idea to keep this optimization in mind, as not all of the instruction optimizer will do the same optimizations, and there are a large number of Internet service providers (ISPs) and servers that do not have the command optimizer installed.
- Not necessarily object-oriented (OOP), object-oriented tends to be expensive, and each method and object invocation consumes a lot of memory.
- Arrays are also useful not to implement all of the data structures in a class.
- Don't subdivide the method too much, think about what code you really want to reuse?
- When you need it, you can always break the code down into methods.
- Try to use a lot of PHP built-in functions.
- If there are a lot of time-consuming functions in your code, you might consider implementing them in C extensions.
- Evaluate the test (profile) of your code. The inspector will tell you what parts of the code are consuming time. The Xdebug debugger contains an inspection program that can show the bottleneck of the code in general.
- The mod_zip can be used as an Apache module to instantly compress your data and reduce data transfer by up to 80%.
- In the case can be replaced with file_get_contents file, fopen, feof, Fgets and other series of methods, try to use file_get_contents, because his efficiency is much higher! But pay attention to the PHP version of file_get_contents when opening a URL file;
- As far as possible to file operations, although the PHP file operation efficiency is not low;
- Optimize the Select SQL statement to make the insert and update operations as few as possible (on update, I was ill-approved);
- Use PHP intrinsics as much as possible (but I'm trying to find a function that doesn't exist in PHP, wasting the time that could have written a custom function, experience problems!) );
- Inside the loop, do not declare variables, especially large variables: objects (This is not just a question to be aware of in PHP?). );
- Multidimensional arrays try not to loop nested assignments;
- Do not use regular expressions in cases where PHP internal string manipulation functions are possible;
- foreach is more efficient and uses foreach instead of while and for loops as much as possible.
- "Replace i=i+1 with I+=1." In line with C + + habits, efficiency is high ";
- For the global variable, it should be used up unset () off;
http://www.bkjia.com/PHPjc/739134.html www.bkjia.com true http://www.bkjia.com/PHPjc/739134.html techarticle 1-writing Modular code good PHP code should be modular code. PHP's object-oriented programming features are particularly powerful tools that can break your application into functions or ...