If you're a developer, it is necessary for you to improve your script in the development process itself early. Following the best practices while coding your PHP script is a good starting point to write PHP code optimization as well.
This tutorial provides few tips to optimize PHP code from a developer point of view.
str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4.
For example, the following will degrade the performance.
for( $i=0; i< count($arr); $i++){ echo count($arr); }The script below will perform much better.
$len = count($arr); for( $i=0; i< $len; $i++){ echo $len; }
For example, let us create a function in two different way to increment by 1, each element of an array having values 0 to 99.
// passing by reference function computeValue( &$param ){ // Something goes here foreach( $param as $k => $value){ $param[$k] = $value + 1; } } $x = array(); for( $i =0; $i<99; $i++){ $x[$i] = $i; } computeValue( $x); // array with 100 elements each incremented by 1 print_r( $x );The function above works faster than the function below although both will produce the same result ( increment each element of the array by 1. )
// passing by value function computeValue( $param ){ // Something goes here foreach( $param as $k => $value){ $param[$k] = $value + 1; } return $param; } $x = array(); for( $i =0; $i<99; $i++){ $x[$i] = $i; } // array with 100 elements each incremented by 1 print_r(computeValue( $x));
$con=mysqli_connect("localhost","username","somepassword","anydb"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL" ; mysqli_connect_error(); } function insertValue( $val ){ mysqli_query($con,"INSERT INTO ttt (someInteger) VALUES ( $val )"); } for( $i =0; $i<99; $i++){ // Calling function to execute query one by one insertValue( $i ); } // Closing the connection as best practice mysqli_close($con);The script above is much slower than the script below:
$con=mysqli_connect("localhost","username","somepassword","anydb"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL" ; mysqli_connect_error(); } function insertValues( $val ){ // Creating query for inserting complete array in single execution. $query= " INSERT INTO tableX(someInteger) VALUES .implode(',', $val)"; mysqli_query($con, $query); } $data = array(); for( $i =0; $i<99; $i++){ // Creating an array of data to be inserted. $data[ ] = '(" ' . $i. '")' ; } // Inserting the data in a single call insertValues( $data ); // Closing the connection as a best practice mysqli_close($con);
Call to the function “forTask1again( )” which is in derived class will work faster than call to the function “forTask1( )” as it is from base class.
class someBaseClass { public function forTask1($string) { // perform task 1 } public function forTask2( ) { // perform task 2 } } class derivedClass extends someBaseClass { public function forTask1again($string) { //perform task 1 same as the function in base class. } public function forTask3($string) { //perform task 3 } } //Instantiating the derived class below. $objDerivedClass = new derivedClass( ); // The call below works slow for task1 as it is from base class. $resultTask1 = $objDerivedClass->forTask1( ); // The call below works faster for task1 as // it is from derived class. $sameResultTask1 = $objDerivedClass->forTask1again();
For example, let us assume that you have a function which returns an array with values or a NULL array. Now you want to check whether the returned array is with values or not, then use the following:
if(isset($returnValue)){ // do something here }In this case, use the above code block, instead of the following:
if(count($returnValue) > 0){ // do something here }