PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development
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.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 9th, 2002, 10:05 AM
cpv204 cpv204 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: NYC
Posts: 29 cpv204 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old August 9th, 2002, 11:59 AM
m0nk3y's Avatar
m0nk3y m0nk3y is offline
Certified m0nk3y!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 437 m0nk3y User rank is Lance Corporal (50 - 100 Reputation Level)m0nk3y User rank is Lance Corporal (50 - 100 Reputation Level)m0nk3y User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 14 h 30 m 23 sec
Reputation Power: 11
Send a message via MSN to m0nk3y
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

Reply With Quote
  #3  
Old August 9th, 2002, 12:53 PM
cpv204 cpv204 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: NYC
Posts: 29 cpv204 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old August 9th, 2002, 01:30 PM
Donncha's Avatar
Donncha Donncha is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Galway, Ireland
Posts: 7 Donncha User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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";

Have a look at http://www.php.net/manual/en/language.types.php , there's a lot of "Gee I didn't know that before" stuff in there.
__________________
Gamers Europe :: Keeping it Simple

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > quotes around array index?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap