What are some good PHP performance tips? -


i've heard of performance tips php such using strtr() on str_replace() on preg_replace() depending on situation.

as far using functions on others, , code style, of performance tips know of?

edit: i'm not talking use of things make code less readable, !isset($foo{5} on strlen($foo) < 5, i'm talking things using preg_ functions on ereg_ functions regex.

edit: reason ask not nitpicking on when optimize, general idea of tends efficient in limited set of alternatives. instance, checking if mysql statement returned error arguably better practice suppressing errors begin with.

this question vague. when want optimize script, first check database , try optimize algorithms. there aren't many pure php performance tips going matter. let's see :

  • concatening variables faster putting them in double-quotation mark string.

    $var = 'hello ' . $world; // faster $var = "hello $world"; // or $var = "hello {$world}"; 

yes, it's faster, second , third form more readable , loss of speed low doesn't matter.

  • when using loop, if condition uses constant, put before loop. instance :

    for ($i = 0; $i < count($my_array); $i++) 

this evaluate count($my_array) every time. make variable before loop, or inside :

for ($i = 0, $count = count($my_array); $i < $count; $i++) 
  • the worst thing queries inside loops. either because of lack of knowledge (trying simulate join in php) or because don't think (many insert in loop instance).

    $query = mysql_query("select id your_table"); while ($row = mysql_fetch_assoc($query)) {     $query2 = mysql_query("select * your_other_table id = {$row['id']}");     // etc } 

never this. that's simple inner join.

there more, really, it's not worth writing of them down. write code, optimize later.

p.s. started writing answer when there none, there may things said in links.

edit: reason, can't format code correctly. don't understand why.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -