Following the best practices while coding your PHP script is a good starting point to write a well optimized PHP code.
This tutorial provides few tips to optimize PHP code from a developer point of view.
- Use the latest version of PHP
- Use PHP's native functions will not only help you write code faster, but will also make it more efficient
- Use “= = =” instead of “= =”, as the former strictly checks for a closed range which makes it faster.
- Disable Debugging Messages
- If a method can be static, declare it static. Speed improvement is by a factor of 4
- Set the maxvalue for your for-loops before and not in the loop.
- Unset your variables to free memory, especially large arrays.
- Use Caching Techniques
- See if you can use strncasecmp, strpbrk and stripos instead of regex
- str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
- Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
- Get into the habit to unset the variables and close database connection in your PHP code. It saves memory.
- Reduce Number of Hits to DB
- Do use switch over lots of if statements
- Use JSON instead of XML while working with web services as there are native php function like json_encode( ) and json_decode( ) which are very fast.
- Use isset( ) where ever possible instead of using count( ), strlen( ), sizeof( ) to check whether the value returned is greater than 0.
- foreach > for > while - According to our benchmarks, the foreach loop is much faster than the for loop, and the for loop is faster than the while loop.
- Make use of the countless predefined functions
- Dont define too many indexes in the tables. It will slow down the DB operations(create, edit, update, read)
- Do consider using the Singleton Method when creating complex PHP classes.
- Do use POST over GET for all values that will wind up in the database for TCP/IP packet performance reasons.
- Do use ctype_alnum,ctype_alpha and ctype_digit over regular expression to test form value types for performance reasons.
- Do use tmpfile or tempnam for creating temp files/filenames
- Do use error_reporting (E_ALL); during debug.
- Do set Apache allowoverride to “none” to improve Apache performance in accessing files/directories.
- Do use a fast fileserver for serving static content (thttpd). static.mydomain.com, dynamic.mydomain.com
- Do serialize application settings like paths into an associative array and cache or serialize that array after first execution.
- Do use PHP output control buffering for page caching of heavilty accessed pages
- Do use PDO prepare over native db prepare for statements. mysql_attr_direct_query=>1
- Do NOT use SQL wildcard select. eg. SELECT *
- Do use database logic (queries, joins, views, procedures) over loopy PHP.
- Do use shortcut syntax for SQL insers if not using PDO parameters parameters. eg. INSERT INTO MYTABLE (FIELD1,FIELD2) VALUES ((“x”,”y”),(“p”,”q”));
Comments
Post a Comment