Home  >  Article  >  Backend Development  >  PHP performance optimization tips_PHP tutorial

PHP performance optimization tips_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:38:14833browse

Google has shared some tips, tutorials and tools for web page performance optimization in the "Let's make the web faster" website produced by Google Code. Today I will translate a tips article : PHP performance optimization skills, I don’t know any of the 5 skills he mentioned.

1. Don’t just copy variables

Sometimes in order to make 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;

GOOD:

echo $_POST[description];

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";

GOOD:

$output = This is a plain string;

BAD:

$type = "mixed";
$output = "This is a $type string";

GOOD:

$type = mixed;
$output = This is a . $type . string;

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);

GOOD:

echo $myVariable;

4. Do not 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;

GOOD:

echo Hello, my name is , $firstName , $lastName , and I live in , $city;

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();
}

GOOD:

switch($_POST[action]) {
case add:
addUser();
break;
case delete:
deleteUser();
break;
case edit:
EditUser();
break;
default:
defaultAction();
break;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486495.htmlTechArticleGoogle shared in the website Lets make the web faster (Let us make the Web faster) produced by Google Code I learned some tips, tutorials and tools such as web page performance optimization. Today I...
Statement:
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