PHP performance optimization (1)-tips 5 _ PHP Tutorial

Source: Internet
Author: User
PHP performance optimization (1)-Five Tips. 1. do not copy variables at will. sometimes, to make PHP code more clean, some new PHP users (including me) will copy the predefined variables to a variable with a shorter name. In fact 1. do not copy variables at will.

Sometimes, to make PHP code more clean, some new PHP users (including me) will copy the predefined variables to a variable with a shorter name, in fact, the result is a doubling of the memory consumption, which only slows down the program. In the following example, if a user maliciously inserts KB of text into the text input box, 1 MB of memory will be consumed!

BAD:

$description = $_POST['description'];echo $description;

GOOD:

echo $_POST['description'];

2. use single quotes for strings

The PHP engine allows single quotes and double quotes to encapsulate string variables, but there is a big difference! The double quotation mark string is used to tell the PHP engine to read the string content, find the variable, and change it to the value corresponding to the variable. Generally, strings do not have variables, so using double quotation marks will lead to poor performance. It is best to use a string connection instead of a double quotation mark string.

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 strings

The echo () function is used to print the result, which is easier to read. in the next example, you can see that the result has better performance.

BAD:

print($myVariable);

GOOD:

echo $myVariable;

4. do not use connectors in echo

Many PHP programmers (including me) don't know how to use commas to separate multiple variables when outputting them with foul smell, instead of using strings to connect them first, in the first example below, there will be performance problems because the connector is used, 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 in sequence.

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. replace if/else with switch/case

The use of switch/case statements instead of if/else statements for the judgment of only one variable will provide 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;}
More PHP code optimization skills are welcome to be discussed together

Http://www.bkjia.com/PHPjc/735172.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/735172.htmlTechArticle1. do not just copy Variables. sometimes, to make PHP code more clean, some new PHP users (including me) will copy the predefined variables to a variable with a shorter name. In fact...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.