NYCPHP Meetup

NYPHP.org

[nycphp-talk] New to group and array question

Anirudh Zala arzala at gmail.com
Mon Sep 11 01:17:16 EDT 2006


On Mon, 11 Sep 2006 05:57:01 +0530, David Krings <ramons at gmx.net> wrote:

> Hello,
>
> 	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.

This could be because you are not properly using them or you do not have  
clear idea of how to properly quote expressions.

>
> 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?

Do not just think in terms of 'echo', 'header' or any other construct or  
function. In much broader sense, just remember simple and golden rule that  
WHEREVER and WHENEVER variable is to be EXPANDED, CONCAT it with rest of  
the expression and use 'SINGLE quote' if certain part of the expression is  
STATIC (i.e. to be used just as it is). In short just using 'single quote'  
and concatenation you can write 99% code in PHP. This is true everywhere  
in PHP whether you assign value to variables, print something, define  
array with elements, pass arguments to function/class methods or evaluate  
any expression. This is good practice throughout PHP and probably for all  
other languages. Let me demonstrate few examples.

#1

$as__name['first']='David';

(Key of array has been quoted using 'single' quotes because key itself is  
static, same for value for that array i.e. 'David';

#2

if('David' == $as__name['first'])
	echo 'Name is '.$as__name['first'];

In above logical expression key of array, value to be compared with that  
array element and conditional echo  statement all have been expressed  
using 'single quotes' only. You can see that left portion of echo  
expression 'Name is' is not to be expanded that is why used in 'single  
quotes' but to display real name variable "$as__name['first']" has to be  
expanded hence it has been concated with first part of the expression.  
However if you have 'single quote' itself as a part of your expression  
then escape it with '\' characters like "echo 'My dad\'s name is  
'.$as__name['first'];")

Bottom line: There is not any MANDATORY use of "Double quotes" in PHP  
except in 1 case when:

#a Special sequence like "\n", "\t", "\s" are to be expanded into New  
line, Tab and Space respectively.

In short "double quotes" tries to expand each and every part of the  
expression, which is not required mot of the time. Hope this will help you  
clear some basic level of evaluation of expressions.

-----------------------------------------------
Anirudh Zala (Project Manager)
ASPL, http://www.aspl.in
arzala at gmail.com
-----------------------------------------------

>
>
> 	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



More information about the talk mailing list