Home > php > Optimize php code

Optimize php code


1.) Multiple argument with echo:
You use echo probably like this
echo ‘first string’.$variable.’second string ‘;
Use this code it is faster
echo ‘first string’, $variable, ‘second string’;
Passing multiple argument is faster than concate string. echo is faster than print. print take one argument.

2 .) Use  single quotes:
In double quotes everything is parsed, with single quotes string is not parsed.
if you need to print only string then use
echo ‘string’;
than
echo “string”;
3.)Pre-increment is faster than post-increment:
++$var;
is faster than
$var++;

4.) Use split() instead of explode():
split() uses
regular expressions, while explode()  not.

5.) Use isset() instead of strlen():
Example:
( i ) $str  = “string”;
if(strlen($str) < 4){
echo “String length is less than 4”;
}

( ii ) $str   = “string”;
if(!isset($str{4}){
echo “String length is less than 4”;
}

faster than this:
$var++;

 

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: