7 Tips for PHP Performance Optimization 2023

By XiaoXin
A Bit Randomly

What is an API?What are the different types of API?What is RESTful API?How do you test an API?What are the common security threats to APIs?How do you secure an API?What is an API? An API (Application Programming Interface)... Read 6 Common API Testing Interview Questions And Answers

Main Contents

7 Tips for PHP Performance Optimization

PHP is a very popular scripting language, and now many companies (Sina, Youku, Baidu, Sohu, Taobao, etc.) are using this language for website development. With this article of mine, I just hope to improve your PHP script performance. Please keep in mind that the performance of your PHP scripts often depends 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 to make your website run faster, you should do optimization. What are the things you need to do before you change your code? What is causing the system to be slow? You can optimize your PHP through the following guidance and other ways, it may be the reason for the database or the reason for the network! By optimizing your PHP code, you can try to find out the bottleneck of your system.

Upgrade your PHP version

A member of your team suggested that there have been many token performance improvements in the PHP engine over the years. If your web server is still running an older version, such as php3 or php4. Then before you try to optimize your code, you should investigate the upgrade situation between versions in depth.

Use cache 

Use caching modules (such as Memcache) or template systems (such as Smarty) for caching. We can improve website performance by caching database results and extracting page results.

Use output buffer 

When your script tries to render, PHP will use the memory cache to store all the data. Buffers can make your page look slow because the buffer fills up with all the data to be responded to before responding to the user. Fortunately, you can make a change that forces PHP to force the response to the user before the buffer fills, which will make your site appear to be faster.

Avoid writing naive setters and getters 

When you write PHP classes, you can directly manipulate object properties, which can help you save time and improve your script performance. Rather than the kind of setters and getters that make people feel childish and ridiculous. Here are some examples: the dog class manipulates the name attribute by using setName() and getName().

class dog {
  public $name = '';
 
  public function setName($name) {
    $this->name = $name;
  }
 
  public function getName() {
    return $this->name;
  }
}

Note: setName() and getName() do nothing except store and return the name property.

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

Setting and accessing the name attribute directly can improve performance by 100%, and can also reduce development time!

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

There is no reason not to copy variables

Sometimes junior phper, in order to make the code more "clean", often reassigns the defined variable to another variable. This actually leads to double memory consumption (when changing variables), which leads to poor script performance. For example, if a user inserts a 512KB variable into another variable, 1MB of memory will be consumed.

$description = strip_tags($_POST['description']);
echo $description;

The above code duplicates the variable for no reason. You only need to use the inline method to simply output variables without consuming additional memory.

echo strip_tags($_POST['description']);

Avoid doing SQL operations in loops

A common mistake is to place an SQL operation in a loop, which results in frequent database accesses and, more importantly, directly leads to poor script performance. In the following example, you can reset a loop operation into a single SQL statement.

foreach ($userList as $user) {
  $query = 'INSERT INTO users (first_name,last_name) VALUES("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
  mysql_query($query);
}

process:

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

Instead of this round-robin scheme, you can concatenate data into a single database operation.

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

process:

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

Other resources

http://www.php.net/memcache
http://www.smarty.net/
http://3v4l.org/ - Analyze the code execution efficiency between versions, very good website 
http://www.php-internals.com/ - Study the website of php kernel! 
Please Share This Article Thank You!

Parallel processing in PHP using pcntl_fork() Explanation

The pcntl_fork() function is a function used to create a process in the php-pcntl module. (does not support windows). As for how to install and enable the php_pcntl extension, I won’t introduce it here, only ana...

Auto Increment The Version of Android App

There is an AssemblyInfo.cs file in a general C# application, and the AssemblyVersion attribute in it can be used to set the version number of the application for example: <font style="vertical-align: inherit;">[asse...