Does not using single quotes around index names slow execution noticeably/significantly?

StackOverflow https://stackoverflow.com/questions/7801031

  •  22-10-2019
  •  | 
  •  

Question

If I were to say

echo $arr[some_index];

as opposed to saying

echo $arr['some_index'];

Will there be a significant amount of processor time/power lost to the error notice? I am aware that it is not proper syntax, but there is a huge amount of code written like this already on a project I am working on.

Was it helpful?

Solution

Well, it's simple enough to check. You can check the execution time on any statement(s) like such:

$start = microtime(true);    
//Do your code. Try an echo of one kind here.    
$end = microtime(true);    
echo($end - $start); //The elapsed time, in seconds. Precise up to a microsecond.

Do one of those for each type you'd like to test. Whichever is consistently fastest will be the quickest, naturally.

You can also use memory_get_usage to determine how much memory has been used, before and after each call.

Now, you should also be getting a large number of NOTICE's. If a constant isn't defined, it's treated as a string instead, but throws a notice. Another problem is if your key ever conflicts with a constant, you'll be checking the wrong value. It's really just not good practice. I'd go through and replace everything.

OTHER TIPS

I think the performance impact would be negligible, however the purist in me would want to see consistent use of quotes/no quotes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top