allworknoplay Posted February 24, 2009 Share Posted February 24, 2009 Hey guys, someone posted this link last week and I find it very useful. I'm posting it again for new guys who may have missed it. http://www.chazzuka.com/blog/?p=163 i have a question about one of the tricks...it says: $row[’id’] is 7 times faster than $row[id] Can someone explain why having single quotes makes it 7 times faster?? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/146621-63-best-practices-to-optimize-php/ Share on other sites More sharing options...
allworknoplay Posted February 24, 2009 Author Share Posted February 24, 2009 Ok I might have found my own answer, but I still wonder why it's 7 times faster... Surrounding your string by ‘ instead of ” will make things interpret a little faster since php looks for variables inside “…” but not inside ‘…’. Of course you can only do this when you don’t need to have variables in the string. Quote Link to comment https://forums.phpfreaks.com/topic/146621-63-best-practices-to-optimize-php/#findComment-769745 Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2009 Share Posted February 24, 2009 The reason why $row[id] is several times slower than using the correct syntax $row['id'] or $row["id"], is because php first attempts to find id among all the defined constants because $row[id] is valid syntax if you have defined a constant named id, then when it does not find a defined constant by that name it goes through a painfully slow error response/recovery process (which is where most of the time is lost), then it assumes you meant $row['id'] and looks up the array index, and finally accesses the correct variable. Php attempts to compensate for lazy-way programming of leaving the quotes out at the cost of lost processor cycles on every variable written that way, multiplied by every time your page is executed. Quote Link to comment https://forums.phpfreaks.com/topic/146621-63-best-practices-to-optimize-php/#findComment-769830 Share on other sites More sharing options...
cooldude832 Posted February 24, 2009 Share Posted February 24, 2009 Things I don't agree with the list print vs echo has been tested and its not significant. unset variable is questionable because the process itself takes memory to free memory Close your database when done? Only if you are going to let the page eat a sandwich for 20 minutes, mysql auto terminates any way Incrementing global vs local, if you build all in the global the global is the local so... Comma vs period on echo really? aggregate and their testing methods are a joke "A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts." That is a horrible programing way because it ruins any CMS system (2-10 times is a bad measurement also) Foreach is dangerous and they talk of it as gold (you can over use it a lot) The include/require over include_once/require_once is stupid because you use require_once/include_once for a reason (really its common sense not an optimization) My 2 cents on it, but overall its a decent article, but mostly its common sense or over a new person's head Quote Link to comment https://forums.phpfreaks.com/topic/146621-63-best-practices-to-optimize-php/#findComment-769848 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.