PHP Optimization Tips

  • Use echo’s multiple parameters instead of string concatenation
echo'CHECKMATE: PLAY with PROBLEMS' );
echo $var1, is greater than, $var2 
// is better than 
echo 'PLAY' 'WITH' 'PROBLEMS';
echo $var1.'is greater than'.$var2;
  • Wrap your string in single quotes (‘) instead of double quotes (“) is faster because PHP searches for variables inside “…” and not in ‘…’, use this when you’re not using variables you need evaluating in your string.
echo 'a string ' $name;
// is better than
echo "a string $name";
  • Use functions outside of loop. Otherwise function gets called each time. For example
$max count$array );
for$i = 0; $i $max$i++ ){
//do something
}
//is better than
for$i = 0; $i count$array ); $i++ ){
//do something
}
  • Use full paths in includes and requires, less time spent on resolving the OS paths.
include'/var/www/html/your_app/test.php' );
//is better than
include'test.php' );
  • It’s better to use switch statements than multi if, else if statements.
switch( $name ) {
	case 'saiful':
		// do something
		break;

	case 'ekram':
		// do something
		break;

	case 'arif':
		// do something
		break;

	default:
		// do something
		break;
}

// is better than

if( $name == 'saiful' )	{
	// do something
}
else if( $name == 'ekram' ) {
	// do something
}
else if( $name == 'arif' ) {
	// do something
}
else {
	// do something
}
  • When working with strings and you need to check that the string is either of a certain length you’d understandably would want to use the strlen() function. This function is pretty quick since it’s operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using an isset() trick.

Example:

if (!isset($foo{5})) { echo "Foo is too short"; }
//is faster than
if (strlen($foo) < 5) { echo "Foo is too short"; }

Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it’s execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string’s length.

  • When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don’t go modifying your C or Java code thinking it’ll suddenly become faster, it won’t. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend’s PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.
  • When a variable is no longer useful, unset those variables which will free memory, especially large arrays.
unset($largearray);
  • Every time close your database connections when you got all the datas from DB
mysql_close($connection);
  • Methods in derived classes run faster than ones defined in the base class.
  • echo is faster than print
  • Use sprintf  instead of variables contained in double quotes, it’s about 10x faster.
  • Use require() instead of require_once() where possible. require() and include() are identical in every way except require halts if the file is missing. Performance wise there is very little difference.
  • $row[’id’] is 7 times faster than $row[id], because if you don’t supply quotes it has to guess which index you meant, assuming you didn’t mean a constant.
  • Never trust user data, escape your strings that you use in SQL queries using mysql_real_escape_string, instead of mysql_escape_string or addslashes. Also note that if magic_quotes_gpc is enabled you should use stripslashes first.

Excellent Article about optimizing php by John Lim

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