PHP Performance Optimization

Source: Internet
Author: User

Preface to this article of mine, I just want to be able to improve your PHP scripting performance.  Keep in mind that your PHP scripting performance is often dependent on your PHP version, your Web server environment, and the complexity of your code. Optimize bottlenecks in your code Hoare once said that "premature optimization is the root of all misfortune." When you want your site to run faster, you should do the optimization thing. What do you need to do before you change your code to cause the system to slow down?  You can use the following guidance and other ways to optimize your PHP, may be a database or network reasons! By optimizing your PHP code, you can try to find your system bottleneck. Upgrade your PHP version

Your team members suggest that the PHP engine has had a lot of symbolic performance gains over the years. If your Web server is still running older versions, such as PhP3 or PHP4. So before you try to optimize your code, you should look into the upgrade between versions.

Click on the link below to find out the details:

Porting from PHP 4 to PHP 5 porting from PHP 5.0.x to php 5.1.x porting from PHP 5.1.x to PHP 5.2.x
Caching is handled using a cache module (such as Memcache) or a template system (such as Smarty). We can cache database results and extract page results in a way that improves site performance.
Using an output buffer when your script tries to render, PHP uses the memory buffer to save all the data. The cache may make your page look slow because the buffer fills up all the data to be responded to and then responds to the user. Fortunately, you can make a change, forcing PHP to force the data to respond to the user before the buffer fills up, which makes your site look faster.
    • Output cache Control

Avoid writing childish setters and getters when you write a PHP class, you can manipulate object properties directly, which can help you save time and improve your scripting performance. Not the kind of setters and getters that make people feel childish and ridiculous. Here are some examples: the Dog class operates on the Name property by using the SetName () and GetName () methods.
1234567891011 classdog {  public$name= ‘‘;  publicfunctionsetName($name) {    $this->name = $name;  }  publicfunctiongetName() {    return$this->name;  }}

  

Note: SetName () and getname () do not have any work in addition to storing and returning the Name property.

123 $rover= new dog();$rover->setName(‘rover‘);echo$rover->getName();

  

Setting and accessing the Name property directly can improve performance by 100% and also reduce development time!

123 $rover= new dog();$rover->name = ‘rover‘;echo$rover->name;

  


There is no reason not to copy variables sometimes in the primary phper, in order to make the code more "clean", often the already defined variables are re-assigned to another variable. This actually results in the consumption of dual memory (when variables are changed), which results in performance degradation of the script. For example, when a user inserts a 512KB variable into another variable, it causes 1MB of memory to be consumed.
12 $description= strip_tags($_POST[‘description‘]);echo$description;

  

The above code does not have any reason to copy the variable again. You only need to use inline to simply output the variable without additional memory consumption.
1 echostrip_tags($_POST[‘description‘]);

  

Avoiding loops doing SQL operations often makes the mistake of putting a SQL operation into a loop, which leads to frequent access to the database and, more importantly, to the poor performance of the script directly. In the following example, you can reset a looping operation to a single SQL statement.
1234 foreach($userListas$user) {  $query= ‘INSERT INTO users (first_name,last_name) VALUES("‘. $user[‘first_name‘] . ‘", "‘. $user[‘last_name‘] . ‘")‘;  mysql_query($query);}

Process:

1 INSERT INTO users (first_name,last_name) VALUES("John", "Doe")

  

Instead of this loop scheme, you can splice data into a single database operation.

123456 $userData= array();foreach($userListas$user) {    $userData[] = ‘("‘. $user[‘first_name‘] . ‘", "‘. $user[‘last_name‘] . ‘")‘; }$query= ‘INSERT INTO users (first_name,last_name) VALUES‘. implode(‘,‘, $userData);mysql_query($query);

Process:

1 INSERT INTO users (first_name,last_name) VALUES("John", "Doe"),("Jane", "Doe")...

  

    • MySQL INSERT Syntax

Other resources
    • PHP Memcache Module
    • Smarty Templating Engine
    • http://3v4l.org---Analyze code execution efficiency across versions, very good websites
    • http://www.php-internals.com/——— Study the PHP kernel website!

Summary PHP in the performance of the optimization there are many, if there is a deeper understanding of the people, you can discuss together, I will put everyone's good advice into the blog, for other phper reference. As a phper can improve the ability of many Phper is very proud of things. --Many people have PHP as a grassroots language, I personally hope that the PHP language will go further, so as phper in the hands of money will be more and more!

PHP Performance Optimization

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.