Php code Optimization

php code optimization tips

Do you know the ways to optimize your php code. Here we have some tips for you.

  • If a method can be static, declare it as static. It will improve the Speed by a factor of 4.
  • You should use echo as it is faster than print.
  • If you have to concat string, Use echo’s multiple parameters.
  • Set the value of how many times it should be iterated before the loop. Do not assign the value in the loop.
  • When a variable is no longer useful, unset those variables which will free memory.
  • Use absolute urls in includes and requires.
  • To get the time of when the script started its execution, you must prefer $_SERVER [‘REQUEST_TIME’] than time ()
  • Str_replace is faster than preg_replace
  • It is much better to utilize switch statements than multi if, else if statements.
  • Every time close your database connections when you got all the datas from DB
  • Always use $row [‘name’] to get data from DB.Because it is 7 times faster than $row [name]
  • Don’t use functions inside of for loop, such as for ($y=1; $y <= main ($arr); $y) the main () function gets called each time.
  • If you declare a global variable and you are not using it in any function will slow down the performance because will check if the global exists or not.
  • Methods should be in derived classes. This will run faster than the method which was defined in the base class.
  • Surrounding your string by single quote (‘) instead of double quotes (“) will be little faster as php looks for variables inside “” but not inside ”.
  • A PHP script will be at least 2-10 times slower than a static HTML page by Apache. So it is better to use lot of static HTML pages with less scripts.
  • You must install a PHP caching product to naturally boost the performance by 25-100% by eliminating compile times. Your PHP scripts will be recompiled each time if the scripts are not cached.
  • Do not use OOP concepts too much because each method and object call consumes a lot of memory.
  • Split methods that has the code you will really re-use too much.Do not split all the functions.

Tags:

Request a Free SEO Quote