Five PHP performance optimization tips

WBOY
Release: 2016-08-08 09:29:18
Original
1129 people have browsed it

1. Don’t just copy variables

Sometimes in order to make the PHP code cleaner, some PHP newbies (including me) will copy the predefined variables into a variable with a shorter name. In fact, this is The result is doubled memory consumption, which only makes the program 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
Copy after login
Copy after login
Copy after login

GOOD:

echo$_POST['description'];

2. Use single quotes for strings

PHP engine It is allowed to use 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 is 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";

Copy after login
Copy after login
Copy after login
Copy after login

GOOD:

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

Copy after login
Copy after login
Copy after login
Copy after login
3. Use the echo function To output a stringUsing the echo() function to print the result is not only easier to read, but in the next example, you can also see better performance. BAD:

print

(

$myVariable

);GOOD:echo

$myVariable

;4. Don’t be in echo Use connectors

Many PHPprogrammers (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 shown in the following number In one example, there will be a performance problem 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. them.

BAD:

echo'Hello, my name is'.$firstName .$lastName .' and I live in '. echo'Hello, my name is

'

,$firstName,$lastName,' and I live in ' ,$city;5. Use switch/case instead of if/elseFor judgments with only a single variable, it will be better to use switch/case statements instead of if/else statements. performance, and the code is 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
Copy after login
Copy after login
Copy after login

GOOD:

switch($_POST['action']) {
case'add ':
addUser( );
break;
case'delete':
deleteUser();
;case
' edit': editUser();break

;default
: defaultAction(); break
;
}

The above introduces five PHP performance optimization tips, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!