I\'ve heard of some performance tips for PHP such as using strtr()
over str_replace()
over preg_replace()
depending on the situation.
Lets imagine you have an array of words.
Like this: $words=array('banana','cat','tuna','bicycle','kitten','caffeine');
And then you have a search term to find, like this: $find='ca';
And you want to know all the elements that start with that given term.
We would usually do like this:
foreach($words as &$word)if(preg_match('@^'.$find.'@',$word))echo $word,'<br>';
Or the fastest way:
foreach($words as &$word)if(strpos($find,$word)==0)echo $word,'<br>';
But why don't we just do like this:
foreach($words as &$word)if($find==($find&$word))echo $word,'<br>';
You shave off a few bytes AND it is faster 'cause you don't have to waste time calling functions.
It's obvious, but creating objects is also costly, so for ex. if you need a timestamp doing a time() is double faster than doing a date_create()->getTimestamp.
for ($a=1;$a<1000000;$a++) {
$e = time(); // In my tests about 2x faster than:
//$e = date_create()->getTimestamp();
}
Calculate Only Once Calculate and assign the value to the variable if that value is getting used numerous time rather than calculating it again and again where it is being used.
For example, the following will degrade the performance.
for( $i=0; i< count($arrA); $i++){
echo count($arrA);
}
The script below will perform much better.
$len = count($arrA);
for( $i=0; i< $len; $i++){
echo $len;
}
Used Switch Cases
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. 7. Use isset Use isset( ) where ever possible instead of using count( ), strlen( ), sizeof( ) to check whether the value returned is greater than 0.
This question is really vague. When you want to optimize your script, you first check your database and try to optimize your algorithms. There aren't many pure PHP performance tips that are going to matter. Let's see :
Concatening variables is faster than just putting them in a double-quotation mark string.
$var = 'Hello ' . $world; // is faster than
$var = "Hello $world"; // or
$var = "Hello {$world}";
Yes, it's faster, but the second and third form are even more readable and the loss of speed is so low it doesn't even matter.
When using a loop, if your condition uses a constant, put it before the loop. For instance :
for ($i = 0; $i < count($my_array); $i++)
This will evaluate count($my_array) every time. Just make an extra variable before the loop, or even inside :
for ($i = 0, $count = count($my_array); $i < $count; $i++)
The worst thing is definitely queries inside loops. Either because of lack of knowledge (trying to simulate a JOIN in PHP) or just because you don't think about it (many insert into in a loop for instance).
$query = mysql_query("SELECT id FROM your_table");
while ($row = mysql_fetch_assoc($query)) {
$query2 = mysql_query("SELECT * FROM your_other_table WHERE id = {$row['id']}");
// etc
}
Never do this. That's a simple INNER JOIN.
There are probably more, but really, it's not worth writing all of them down. Write your code, optimize later.
P.S. I started writing this answer when there was none, there may be some things already said in links.
Edit: for some reason, I can't format the code correctly. I really don't understand why.
If you're looking for good tips on how to program your code so that it's the most efficient, refer to http://www.phpbench.com/. They show a lot of comparisons on various aspects of programming so you can utilize the best methods that fit your needs. Generally it comes down to whether you're looking to save on processing power or memory usage.
http://talks.php.net/show/digg/0 - A talk given by PHP themselves on performance
http://code.google.com/speed/articles/optimizing-php.html - Recommendations by Google on how to speed up your applications
Most commonly your problems aren't with PHP, but are going to be MySQL or http requests issues.
66 Tips for optimizing your PHP
Here are Webber’s points:
vs.
view sourceprint?1.if (!isset($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.
As Reihold Webber pointed to a post from John Lim (found this article copied without state the source here), then i investigate further and truly that is an excellent best practice tutorial for optimizing the php code performance, covered almost all aspects from low level webserver configuration, PHP configuration, coding styling, and performace comparisson as well.
Another good practice for better php performance as written in cluesheet.com are:
Ref - gist.github.com
I visited other blogs and compared all the above points and tried to add everything here for optimising your PHP code.
Hope this helps you.