Home>

With the rise of domestic PHP development,Code optimization has become a commonplace topic.From time to time, the author himself will come to the csdn forum to talk with people about PHP development and optimization issues.This is also a way to improve yourself quickly.

When programmers write programs,Always want to minimize your program's resources,Runs faster,Less code.Often we lose a lot while pursuing these.The goal of optimization is to get the fastest running speed and the easiest to maintain code at the least cost.The following is a summary of several PHP optimizations:

1, in_array usage

Avoid using in_array () on large arrays, and avoid using this function in loops for arrays with more than 20 elements.in_array () is very resource intensive.This effect may be small for small arrays,But checking a large array in a loop can take several seconds.If you really need this feature,Use isset () to find array elements.In fact, the key value is used to query the key value.Calling isset ($array [$var]) is much faster than in_array ($var, array_keys ($array)).

Array key names

In php, it is legal to use strings without single quotes as array key names,But we don't want this-key names should always be enclosed in single quotes to avoid confusion.Note that this is using a string,This is not the case when using variables for key names.

E.g:

//bad writing
$foo=$assoc_array [blah];
//good writing
$foo=$assoc_array ["blah"];
//bad writing
$foo=$assoc_array ["$var"];
//good writing
$foo=$assoc_array [$var];

String expressionsThere are two different ways to represent a string in PHP-single quotes or double quotes.The main difference is that the syntax interpreter performs variable substitution on double-quoted strings,It does not process single quoted strings.So you should always use single quotes,Unless you really need to handle variables in strings.In this way, we can reduce program running consumption,Because the syntax interpreter doesn't need to process a whole lot of strings without variables at all.

Similarly, if you use a string variable as a parameter in a function call,You don't need to enclose this variable in quotes.This leads to a lot more and less use of the syntax interpreter.Remember that almost all escapes in double quotes are invalid for single quotes.You need to pay attention to the above rules,But sometimes for code readability,An appropriate exception can be made.

E.g:

//bad writing
$str="this is a really long string with no variables for the parser to find.";
do_stuff ("$str");
//good writing
$str="this is a really long string with no variables for the parser to find.";
do_stuff ($str);
//sometimes single quotes are not so appropriate
$post_url=$phpbb_root_path. "posting.". $phpex. "?mode =". $mode. "&start =". $start;
//double quotes sometimes make code lines more focused
$post_url="{$phpbb_root_path} posting. $phpex?mode=$mode&start=$start";

4. Operations in the cycle definitionIf there is an operation in the comparison section, be sure to optimize it.Because this part will operate at each step in the loop:

//the sizeof function is called in every loop
 for ($i=0;$i<sizeof ($post_data);$i ++)
 {
  do_something ();
 }
//You can assign this constant quantity at the beginning of the loop
 for ($i=0, $size=sizeof ($post_data);$i<$size;$i ++)
 {
  do_something ();
 }

5, branch judgment,switchcase is better than ifelseBranch statements are much better than many ifelse.

6. Don't abuse [email protected]

Don't abuse [email protected], it seems simple,But in fact, [email protected] several times worse than not using @.

In particular, don't [email protected] loops.

7.How to get time

Many times we are used to using time () to get the current time,But time () is ultimately a function,It's better to use $_server ["request_time"] directly.

8, string length judgment

Two ways of writing:

(1) if (strlen ($str)<5) {/* do something * /}

(2) if (isset ($str [5])) {/* do something * /}

php
  • Previous Detailed ajax async synchronous and asynchronous in jQuery
  • Next php word guessing game