Compile PHP code summary_php tutorial

Source: Internet
Author: User
Compile PHP code summary. 1-PHP code with good modular code should be modular code. PHP's object-oriented programming functions are some of the most powerful tools that can break down your applications into functions or 1-compiling PHP code with good modular code should be modular code. PHP's object-oriented programming functions are particularly powerful tools that can break down your applications into functions or methods. You should split the front-end HTML/CSS/JavaScript code from the server of your application as much as possible. You can also follow the MVC (Model-View-Controller) mode in any PHP framework.
2-PHP code with good coding specifications should have a complete set of coding specifications. By naming variables and functions, you can use uniform methods to access the database, handle errors, and indent the same code to achieve programming specifications. This makes your code more readable.

3-compiling PHP code with good porting code should be portable. You can use existing php functions, such as magic quotes and short labels. Try to understand your needs, and then write code to make the code INDEPENDENT and portable by adapting to PHP features.

4-PHP code with good security code should be safe. PHP5 provides outstanding performance and flexibility. However, the security problem lies completely with developers. For a professional PHP developer, it is critical to have a deep understanding of major security vulnerabilities, such as cross-site scripting (XSS) and cross-site request forgery (CSRF), code injection, and character encoding vulnerabilities. You can write secure code by using special PHP functions, such as mysql_real_escape_string.

5-code comments are an important part of the code. Through code annotation, you can know what the variable or function is, which will be very useful in future code maintenance.

6-avoid replacing short labels with full PHP labels.

7-use single quotation marks instead of double quotation marks. always use single quotation marks instead of double quotation marks to avoid the performance degradation caused by PHP searching for variables in strings. Use single quotes instead of double quotes to include strings, which is faster. Because PHP searches for variables in strings enclosed by double quotation marks

8-the escape string output uses ENT_QUOTES as a parameter and is passed to the htmlspecialchars function to ensure that single quotes (') are also converted to HTML entities. this is a good habit.

9-use commas to separate string output a string separated by commas (,) in the echo statement, which is better than the use of the string concatenation operator.

10-check the sent value before output

Check the passed value $ _ GET ['query'] before output. The isset or empty function can be used to check whether the variable is null.

11-others
  • If the class method can be defined as static, it should be defined as static as much as possible, and its speed will be increased by nearly four times.
  • $ Row ['id'] is 7 times faster than $ row [id.
  • Echo is faster than print and uses multiple echo parameters instead of periods to replace string connections, such as echo $ str1 and $ str2.
  • Determine the maximum number of cycles before executing the for loop. do not calculate the maximum value every cycle. it is best to use foreach instead.
  • Unregister unnecessary variables, especially large arrays, to release the memory.
  • Avoid using _ get ,__ set ,__ autoload whenever possible.
  • Require_once () is expensive.
  • When you include a file, try to use the absolute path, because it avoids PHP's file search speed in destde_path, and it takes less time to parse the operating system path.
  • If you want to know the TIME when the script starts to be executed (that is, when the SERVER receives a client REQUEST), use $ _ SERVER ['request _ time'] instead of time ().
  • Functions use the same functions instead of regular expressions.
  • The str_replace function is faster than the preg_replace function, but the strtr function is four times more efficient than the str_replace function.
  • If a string replacement function can take an array or character as a parameter, and the parameter length is not too long, you can consider writing an additional replacement code so that each parameter passing is a character, instead of writing only one line of code to accept arrays as query and replacement parameters.
  • Using the select branch statement is better than using multiple if and else if statements.
  • Blocking error messages with @ is very inefficient and inefficient.
  • Open the mod_deflate module of apache to speed up web browsing.
  • When the database connection is used up, it should be switched off. do not use persistent connections.
  • Error messages are expensive.
  • Increasing local variables in the method is the fastest speed. It is almost the same as calling a local variable in a function.
  • Increasing a global variable is twice slower than increasing a local variable.
  • Incrementing an object property (for example, $ this-> prop ++) is three times slower than incrementing a local variable.
  • Increasing an unspecified local variable is 9 to 10 times slower than increasing a predefined local variable.
  • Defining only one local variable rather than calling it in a function also slows down (to an extent equivalent to increasing a local variable ). PHP will probably check whether global variables exist.
  • Method calling seems to have nothing to do with the number of methods defined in the class, because I have added 10 methods before and after the test method, but the performance has not changed.
  • The method in the derived class runs faster than the same method defined in the base class.
  • Calling an empty function with a parameter takes seven to eight times to increase the number of local variables. Similar method calls take nearly 15 times to increment local variables.
  • Apache parses a PHP script two to ten times slower than parsing a static HTML page. Use static HTML pages and less scripts as much as possible.
  • Unless the script can be cached, it will be re-compiled every time it is called. The introduction of a PHP Cache mechanism can generally improve the performance by 25% to 100%, so as to avoid compilation overhead.
  • Use memcached as much as possible. Memcached is a high-performance memory object cache system that can accelerate dynamic Web applications and reduce database load. It is useful for the cache of OP code, so that the script does not have to re-compile each request.
  • When operating on a string and checking whether its length meets certain requirements, you can use the strlen () function. This function is executed quite quickly because it does not perform any calculations and only returns the known string length stored in the zval structure (C's built-in data structure, used to store PHP variables. However, because strlen () is a function, it is more or less slow, because function calling goes through many steps, such as lowercase letters, PHP does not distinguish between case-insensitive function names.) and hash searches are executed along with the called function. In some cases, you can use the isset () technique to accelerate your code execution.
    (Example) if (strlen ($ foo) <5) {echo 'foo is too short ';}
  • (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 () is used as a language structure, this means that function search and lowercase letters are not required for execution. That is to say, in fact, you do not spend too much money in the top-level code that checks the string length.

  • When the execution variable $ I increments or decreases, $ I ++ is slower than ++ $ I. This difference is exclusive to PHP and does not apply to other languages. therefore, do not modify your C or Java code and expect them to become faster and useless immediately. ++ $ I is faster because it only requires three commands (opcodes), and $ I ++ requires four commands. In fact, a temporary variable is generated in post-increment mode, which is then incremented. The pre-increment directly increases on the original value. This is a kind of optimization, as the Zend PHP optimizer does. Keeping this optimization processing in mind is a good idea, because not all command optimizers perform the same optimization and there are a large number of Internet service providers (ISPs) that do not have command optimizers) and server.
  • It is not essential for object-oriented (OOP). object-oriented usually has a high overhead, and each method and object call consumes a lot of memory.
  • Instead of using classes to implement all the data structures, arrays are also useful.
  • Do not subdivide the methods too much. think carefully about the code you actually intend to reuse?
  • When you need it, you can always break down the code into methods.
  • Use as many PHP built-in functions as possible.
  • If a large number of time-consuming functions exist in the code, you can consider using C extension to implement them.
  • Profile your code. The validator will tell you how much time the code consumes. The Xdebug debugger contains an inspection program, which can display the code bottleneck in general.
  • Mod_zip can be used as an Apache module to instantly compress your data and reduce the data transmission volume by 80%.
  • When file_get_contents can be used to replace file, fopen, feof, fgets, and other methods, use file_get_contents as much as possible because of its high efficiency! Note the PHP version of file_get_contents when opening a URL file;
  • Perform as few file operations as possible, although PHP file operations are not efficient;
  • Optimize Select SQL statements and perform Insert and Update operations as little as possible (I have been maliciously approved for update );
  • Try to use PHP internal functions as much as possible (but in order to find a function that does not exist in PHP, it is a waste of time and experience to write a custom function !);
  • Do not declare variables inside the loop, especially big variables: objects (this does not seem to be a concern in PHP ?);
  • Multi-dimensional arrays should not be nested repeatedly;
  • Do not use regular expressions when you can use PHP internal strings to operate functions;
  • Foreach is more efficient. use foreach instead of the while and for loops;
  • "Replace I = I + 1 with I + = 1. In line with the c/c ++ habits, high efficiency ";
  • For global variables, unset () should be used up;

Http://www.bkjia.com/PHPjc/739134.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/739134.htmlTechArticle1-PHP code with good modular code should be modular code. PHP's object-oriented programming functions are particularly powerful tools that can break down your applications into functions or...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.