Discuss quotes around array index? in the PHP Development forum on Dev Shed. quotes around array index? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 29
Time spent in forums: 8 h 36 m 39 sec
Reputation Power: 0
quotes around array index?
I have a question about quoting array indices. This is from Rasmus Lerdorf's Programming PHP:
Quote:
You don't have to quote single-word strings. For instance, $age['Fred'] is the same as $age[Fred]. However, it's considered good PHP style to always use quotes, because quoteless keys are indistinguishable from constants. When you use a constant as an unquoted index, PHP uses the value of the constant as the index:
define('index',5);
echo $array[index]; // retrieves $array[5], not $array['index'];
You must use quotes if you're using interpolation to build the array index:
$age["Clone$number"]
So far so good.
Quote:
However, don't quote the key if you're interpolating an array lookup:
// these are wrong
print "Hello, $person['name']";
print "Hello, $person["name"]";
// this is right
print "Hello, $person[name]";
I understand why the 2nd example is wrong but why is the 1st example wrong?
Posts: 437
Time spent in forums: 14 h 30 m 23 sec
Reputation Power: 11
it could be the function print(), it might be weird, try echo or the print with the ()
anyways, i tried it, just change it to this:
PHP Code:
print "Hello, " . $person['name'];
works fine.
I dont think print likes two singles quotes, it doesnt like it
__________________
---------
Linux...Macs.... there boring, they just work....... Windows users on the other hand have all the fun!!
--------- PHP | MySQL | Apache | Zend
Posts: 29
Time spent in forums: 8 h 36 m 39 sec
Reputation Power: 0
m0nk3y,
Thanks for your code, but I didn't need a workaround, because the correct way to do it is right there: print "Hello, $person[name]";.
By the way, echo "Hello, $person['name']"; doesn't work either.
Neither does print ("Hello, $person['name']");
I can see from trying the code myself that it doesn't work but I was a little curious as to why.
I can understand why the 2nd of Rasmus's examples doesn't work, because using the double quotes around the array's index closes the first double quote from before the Hello.
I was looking for a similarly intuitive reason for why echo "Hello, $person['name']"; is bad syntax.
If the answer is simply that's the way it is, I'll accept it.
Posts: 7
Time spent in forums: 1 h 3 m 11 sec
Reputation Power: 0
PHP just doesn't like you using quotes for array lookups without curly brackets, it's too complicated for it to parse.
So use either of the following: (btw it has nothing to do with print(), it's a string issue)
echo "blah $array[index] blah";
echo "blah {$array['index']} blah";
You can use curly braces for all sorts of complicated expressions...
echo "blah {$object->array['index']['subindex']} blah";