Log in

View Full Version : $variable[option] / $variable['option']


tehQspm
05-23-2007, 01:45 AM
I don't know if I've used the terms correctly and this is probably a basic php question but what's the difference between something like say $userinfo[username] and $userinfo['username']. Are there specific situations in which you use the apostrophes for this in templates or php files?

Adrian Schneider
05-23-2007, 02:54 AM
$userinfo['username'] is proper, because 'username' (string) is key in the $userinfo array. The first one would be correct if username was a constant, or if its being parsed within double quotes (which templates are).

echo $userinfo['username']; // correct

echo $userinfo[username]; // incorrect [issues notice of unknown constant]

echo "$userinfo['uesrname']"; // parse error

echo "$userinfo[username]"; // correct, but a tad slower than example 1

tehQspm
05-23-2007, 04:15 AM
I think I get you.

So how does this work inside templates? Like with <if condition="$something[somethingelse]"></if> rather than <if condition="$something['somethingelse']"></if>?

Adrian Schneider
05-23-2007, 04:20 AM
Template conditions are parsed as raw PHP (using ternary opreator) so you will need to quote the array keys. Not doing so can result in some nasty PHP-5 related bugs (like if the key is 'private', since its a new keyword). But, in the rest of the template, you don't need it.