[nycphp-talk] New to group and array question
Michael Southwell
michael.southwell at nyphp.org
Sun Sep 10 21:50:23 EDT 2006
At 08:27 PM 9/10/2006, you wrote:
>Hello,
Just to provide a little background on the answers others have provided:
PHP (for whatever reason; <joke>presumably a feature and surely not a
bug</joke> ;-) doesn't allow you to use the normally expected single
quotation marks around an array element when the array variable
appears within something which has double quotation marks around it,
typically an echo statement containing both text and variables, but
also possibly a header statement or something else. So something like
this is no good, and will throw the error you are getting: echo "The
$variable is $array['elementName']";
There are three ways to accomplish what you are trying to do:
1. concatenate: echo "The $variable is " . $array['elementName'];
2. use curly brackets: echo "The $variable is {$array['elementName']}";
3. NOT RECOMMENDED BUT USUALLY WORKS: don't use the single quotation
marks: echo "The $variable is $array[elementName]";
It is a matter of personal preference whether you choose 1 or 2; both
work and are fine. #3 causes extra work for PHP and can fail if
elementName happens to be a constant; so it should not be used.
Chris Shiflett's point about the dangers of using raw superglobal
variables in output is a very good one; the point is that if the
arrays you are working with contain user-submitted information, you
must take special care to sanitize them. Any of the books on
security that are around can tell you how to do this. If you are not
working with these kinds of arrays, but rather with ones that you
have created yourself, you may not need to worry about security
issues in using them.
> my name is David and I am new in this group. I do some PHP
> for fun for my
>private web site and well, not to bore you with more details about me, here
>is my question. I always stumble across an odd thing with arrays. I read in
>many documentations and books that one should use the single quotes when
>referencing to an array element, such as $array['element']. Generally, this
>works fine and I use it that way, but it always fails when using it in echo
>or header statements (and probably a few others). The error I receive is
>parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or
>T_VARIABLE or T_NUM_STRING.
>
>I know how to get around this parse error by omitting the single quotes
>($array[element] instead of $array['element']), but I somehow am under the
>impression as if this isn't really the way to do it. I could assign it to a
>variable each time, but that is quite annoying (but maybe the right thing
>to do?).
>
>What is the significance of the single quotes? What is the expert advice on
>using or not using them? How would I package the single quote in an echo or
>header statement?
>
>
> Any enlightenment is greatly appreciated.
>
>
> David
>
>_______________________________________________
>New York PHP Community Talk Mailing List
>http://lists.nyphp.org/mailman/listinfo/talk
>
>NYPHPCon 2006 Presentations Online
>http://www.nyphpcon.com
>
>Show Your Participation in New York PHP
>http://www.nyphp.org/show_participation.php
Michael Southwell, Vice President for Education
New York PHP
http://www.nyphp.com/training - In-depth PHP Training Courses
More information about the talk
mailing list