Re: Variable as an index

From: Date: Mon, 22 Dec 2008 02:02:34 +0000
Subject: Re: Variable as an index
References: 1 2 3 4 5 6 7 8 9  Groups: php.general 
Request: Send a blank email to php-general+get-284950@lists.php.net to get a copy of this message
2008/12/21 German Geek <geek.de@gmail.com>: > Yes, i agree with this. Even if it takes a few nano seconds more to write > out more understandable code, it's worth doing it because code management is > more important than sqeezing out the last nano second. And then also an > $var = "Hello"; > echo "$val World"; > > has less characters than and is more readable than > > $var = "Hello"; > echo $var ." World"; > > So it would take maybe a few nano seconds less to read it from the hard > drive. And we all know that disk I/O is more expensive than pushing around > variables in main memory in terms of time. And RAM is soo cheap these days. > > Tim-Hinnerk Heuer > > http://www.ihostnz.com Agreed. Although I tend to use ' instead of " unless I need interpolation, if I feel it's really going to make that much of a difference then I start looking at rewriting that section in C or refactoring. String interpolation shouldn't be a bottleneck. Getting back to the original question though, the correct way to express a multidimensional array access inside a string is to use curly braces, and include quotes around any string index names: echo "An array: {$arr[$foo]['bar']}\n"; Torben > On Mon, Dec 22, 2008 at 12:50 PM, Anthony Gentile <asgentile@gmail.com>wrote: > >> True, it might mean the very slightest in milliseconds...depending on what >> you're doing/hardware. However, no harm in understanding the difference/how >> it works. >> Many will code echo "Hello World" and echo 'Hello World'; and never >> know >> the >> difference, I just happen to think being aware of the details will help for >> the long term programmer. >> Since, I brought it up, I'll go ahead and give another example. Ternaries >> that make a lot of people feel awesome because a lot is being accomplished >> in one line are also more opcodes than their if-else statement >> equivalents...and often times can be more confusing to future maintainers >> of >> the code. >> >> Anthony Gentile >> >> >> >> On Sun, Dec 21, 2008 at 6:20 PM, Chris <dmagick@gmail.com> wrote: >> >> > Anthony Gentile wrote: >> > >> >> for e.g. >> >> $var = 'world'; >> >> echo "hello $var"; >> >> vs >> >> echo 'hello '.$var; >> >> >> >> The first uses twice as many opcodes as compared to the second. The >> first >> >> is >> >> init a string and adding to it the first part(string) and then the >> second >> >> part (var); once completed it can echo it out. The second is simply two >> >> opcodes, a concatenate and an echo. Interpolation. >> >> >> > >> > I'd call this a micro-optimization. If changing this causes that much of >> a >> > difference in your script, wow - you're way ahead of the rest of us. >> > >> > >> > >> Ö|.sEÇ >> 7d}œ²E<`yhttp://blog.libssh2.org/index.php?/archives/28-How-long-is-a-piece-of-string.html >> > >> > http://www.phpbench.com/ >> > >> > -- >> > Postgresql & php tutorials >> > http://www.designmagick.com/ >> > >> > >> > -- Torben Wilson <torben@2powerweb.com>

« previous php.general (#284950) next »