This post covers most performance improvement tips related to PHP coding. While working with small website or project it’s ok to ignore these tips, but when you are dealing with large website or project which is going to maintained for long term and which have large number of user base. Developer must have to consider the below tips from the starting of the project. It will surely make drastic change in your website performance.
In PHP there are so many ways to perform the same task. Normally developers are using the way which is most comfortable for them or which they are more aware of.
- echo is more faster than print. Both functions are used for the same thing but echo is the language construct which return nothing, which print will return 0 or 1 based on success or failure.
- include_once is more costly than include statement. Because it have to search for class definition you are trying to include is already included or not?
- Always use single quotes for long strings instead of double quotes. Because in double quotes it will search for php variable to evaluate them. So in this case, echo ‘This is long string’ . $name is faster then echo “This is long string $name”. But from above both echo ‘This is long string’ , $name, is faster because it does not require any string manipulation.
- Don’t use for($i=0; $i<count($names);$i++){…} instead of this use $size = count($names); for($i=0; $i<$size;$i++){…}. The first method will call count function on each iteration of for loop, while in second iteration count function is being called only once.
- If you can declare a method as static then let it be, because its 33% more faster than member functions.
- If you can solve your problem without using regular expression then don’t use it. Regular expression are slower then their php counterparts. For example use str_replace instead of preg_replace.
- Try to minimize relative paths for files inclusion. For relative path inclusion it will search for default include path then current directory then so on. So file search in that case may take more time. Better practice is to declare the constant called WEB_ROOT which defines the root.
- Identical operator (===) is faster than (==) operator as identical operator will include type checking also. So If( 1 == ‘1’) will return true, If( 0 == ”) will return true while if you use identical operator both this conditions If( 1 === ‘1’) and If(0 === ”) will return false. So it is recommended to use identical operator when you are going to use some boolean variables for deciding the flow/control of your application.
- Don’t use short tags <?=$name> and try to using <?php echo $name; > , It can create a problem for you if you are going to deploy your application on another server.
- Don’t use or relay on register_globals or magic quotes and read and configure your php.ini settings carefully.
I have not covered all the things here but there are lots of more optimizations tips are available, will see that in next post.
[…] This post was mentioned on Twitter by junichi_y, Avinash Zala and Avi, Avi. Avi said: Xpert Developer | PHP coding tips for Performance Improvement http://goo.gl/fb/uCceu […]
I havent any word to appreciate this post. Really I am impressed from this post. The person who create this post it was a great human. Thanks for shared this with us.
Oh look. More ‘micro optimisations’ (and by ‘more’, I mean the same as the last 20 posts on the subject), most of which will be utterly lost in the noise of running a code cache like APC.
Thanks for the list.
Also for number 4, using a while loop should be faster than a for loop.
What method did you use to benchmark?
So simple yet great tips. I honestly didn’t know about echo “string” (comma) $variable till this day.
I feel like a good programmer thank to you
Very good list!!!
Thanks
Nice tips, i’ve do this things without know this tips thanks
Hi.
Rule 4. i’m using
for ( $i = 0, $size = count($names); $i < $size; $i++ )
Micro Optimizations are simply not worth it (except the one with the for loop).
Professional Programmers know that other things like Database Optimization etc. is a lot more important and has a bigger effect.
Also Point 9 & 10: What has this to do with Performance? They’re good points in general for Coding standards but they have nothing to do with Performance..
true!
Very nice article!
Awesome list!
Maybe it is a micro optimalisation, but you still can adapt your own standards to write new code with these tips. You don’t have to replace old code, but later, I had the feeling that the “messed” code became more noise, because they differ from the new code. But I think that would be personal.
Can someone elaborate on the WEB_ROOT usage for me?
He means instead of including like this:
include ‘../../../some_path/my_file.php’;
do this:
include WEB_ROOT . ‘/some_path/my_file.php’;
Doc Root will be better to use :)
A better name, perhaps…
Awesome little, looking forward to the next one.
Nice indeed… but… “Foo $bar” WAS, IS and ALWAYS WILL BE faster than ‘Foo ‘ . $bar ;)
I decided to code like this after making some benchmarks of my own.
Micro-optimizations are good as long as they don’t create too much fuzzy code :)
That is a comma dude, but thanks for playing.
thank you for this tips.
i already knew the biggest part of its but i learnt new tips, like echo ‘foo’,$var.
Mike said a good think about for loop.
[…] PHP coding tips for Performance Improvement […]
should not use in_array() function with huge data in the array. It’s very slow.
I am very happy to know that i am following all the rules stated here without the Rule #9. :-D Thanks for the post. :)
[…] my earlier article on PHP Coding Tips, I have mentioned that static methods are 33% faster than normal […]
nice post i like this
Very nice article………………..Go Ahead
Thanks for such a wonderful post. It was a great read.