PHP code optimization_PHP tutorial

WBOY
Release: 2016-07-13 17:44:46
Original
802 people have browsed it

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 a double-quoted string tells the PHP engine to first read the content 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/478724.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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!