Jump to content

Recommended Posts

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!

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/146621-63-best-practices-to-optimize-php/
Share on other sites

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.

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.

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.