Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2004, 12:51 AM
Authorized User
 
Join Date: Jun 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to tp194
Default RE: Quotes in Arrays

I have a basic question on using quotes with arrays...

I have this code:

$user = array("name" => "Tom", "age" => 24, "gender" => "male");

print "Name: " . $user[name] . "<br>\n";
print "Age: " . $user[age] . "<br>\n";
print "Gender " . $user[gender] . "<br>\n";

But when I run the script, I get this error message:

Notice: Use of undefined constant age - assumed 'age' in c:\inetpub\wwwroot\vegTest.php on line 79
Age: 24

When I add single quotes, $user['name'], it runs fine.

Can somebody please explain why this is? Every book I've read has stated not to use quotes...

Thanks

T--


 
Old March 9th, 2004, 01:28 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

The simple answer is unquoted tokens are assumed a different type of token than what you would have intended in PHP, in this case a constant. Tokens, if you've never heard the term before, are more or less the building blocks of a programming language. Variables, constants, operators, delimiters, keywords, punctuation.. are all tokens. Each token is interpretted by the parser or compiler and you get a working program (or non working program, whatever the case may be!).

In PHP a constant is a token without a dollar sign that acts as a placeholder for another value, like variables, but unlike variables constants can only be set once (hence the name) and may not be unset once set and are not subject to variable scope.

When you use an unquoted token in an array indice (or anywhere else for that matter), and that token is a string that does not contain a dollar sign but also fits the definition of what a constant is (contains letters, numbers, underscores) PHP will look for a defined constant by that name, if none are found and error_reporting is set to E_ALL (report all warnings and notices) PHP will spit out an error complaining that the constant didn't exist, then it will assume that you mean the token to be the string literal and will go on with execution. The books you are reading were likely written before the PHP group made the default value of error_reporting to be E_ALL which in effect was intended to give PHP greater security to find "unsafe" variable usage, variables that are used without being first defined, as well as to aid programmers discover things like logic errors from mispelled variable.

So all that being said you should always quote the array indices (if you're using strings, obviously integers don't have to be quoted).

Here are a few pages from the PHP manual which discuss this:
http://www.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/language.types.string.php
http://www.php.net/manual/en/language.constants.php
http://www.php.net/manual/en/security.errors.php

hth,
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old March 9th, 2004, 11:54 PM
Authorized User
 
Join Date: Jun 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to tp194
Default

Wow...thanks for all the info!

T--






Similar Threads
Thread Thread Starter Forum Replies Last Post
Quotes in textbox amit_p_patel VB How-To 7 May 22nd, 2007 02:29 AM
displaying single quotes and double quotes ren_123 Classic ASP Databases 2 February 22nd, 2006 02:17 PM
Multidemmesional Arrays OR arrays gmoney060 Classic ASP Basics 3 November 1st, 2004 03:42 PM
Double Quotes and Single Quotes Problem phungleon Classic ASP Basics 7 May 27th, 2004 01:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.