Home > Backend Development > PHP Tutorial > PHP Performance Optimization (1) - Five Tips_PHP Tutorial

PHP Performance Optimization (1) - Five Tips_PHP Tutorial

WBOY
Release: 2016-07-13 10:37:39
Original
932 people have browsed it

1. Don’t just copy variables

Sometimes in order to make the PHP code tidier, some PHP newbies (including me) will copy the predefined variables to a variable with a shorter name. In fact, the result of this is doubling the memory consumption, which will only make The program is even slower. Just imagine, in the following example, if the user maliciously inserts 512KB of text into the text input box, this will cause 1MB of memory to be consumed!

BAD:

$description = $_POST['description'];
echo $description;
Copy after login

GOOD:

echo $_POST['description'];
Copy after login

2. Use single quotes for strings

The PHP engine allows the use of single quotes and double quotes to encapsulate string variables, but there is a big difference! Using double-quoted strings tells the PHP engine to first read the contents of the string, find the variables in it, and change them to the values ​​corresponding to the variables. Generally speaking, strings have no variables, so using double quotes will lead to poor performance. It's better to use string concatenation instead of double quoted strings.

BAD:

$output = "This is a plain string";
Copy after login

GOOD:

$output = 'This is a plain string';
Copy after login

BAD:

$type = "mixed";
$output = "This is a $type string";
Copy after login

GOOD:

$type = 'mixed';
$output = 'This is a ' . $type .' string';
Copy after login

3. Use the echo function to output a string

In addition to being easier to read using the echo() function, you can also see better performance in the next example.

BAD:

print($myVariable);
Copy after login

GOOD:

echo $myVariable;
Copy after login

4. Don’t use the connector

in echo

Many PHP programmers (including me) don't know that when outputting multiple variables with stink, you can actually use commas to separate them, instead of concatenating them with strings first, as in the first example below, There will be performance issues due to the use of connectors, because this will require the PHP engine to first connect all the variables and then output them. In the second example, the PHP engine will output them sequentially.

BAD:

echo 'Hello, my name is' . $firstName . $lastName . ' and I live in ' . $city;
Copy after login

GOOD:

echo 'Hello, my name is' , $firstName , $lastName , ' and I live in ' , $city;
Copy after login

5. Use switch/case instead of if/else

For judgments with only a single variable, using switch/case statements instead of if/else statements will have better performance, and the code will be easier to read and maintain.

BAD:

if($_POST['action'] == 'add') {
  addUser();
} elseif ($_POST['action'] == 'delete') {
  deleteUser();
} elseif ($_POST['action'] == 'edit') {
  editUser();
} else {
  defaultAction();
}
Copy after login

GOOD:

switch($_POST['action']) {
case 'add':
  addUser();
  break;
case 'delete':
   deleteUser();
  break;
case 'edit':
   editUser();
   break;
default:
   defaultAction();
   break;
}
Copy after login
更多<font color="#2970a6">PHP</font><font color="#2970a6">代码</font>优化技巧欢迎一起讨论
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735172.htmlTechArticle1. Don’t just copy variables. Sometimes in order to make PHP code neater, some PHP newbies (including me) will Copy the predefined variable to a variable with a shorter name. In fact...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template