On Mon, 2002-02-18 at 15:29, Matt wrote:
> >   $foo = "This page is $_SERVER[PHP_SELF]";
> >
> > Or, better:
> >
> >   $foo = "This page is {$_SERVER['PHP_SELF']}";
> >
> > You do have to concat to do this in single-quoted (noninterpolated)
> > strings, though.
> >
> >
> http://www.php.net/manual/en/language.types.string.php#language.types.string
> .parsing
> 
> Can you expand on why the first option isn't generally acceptable?  I can
> understand there being an issue with spaces in an index, but is there some
> other reason that the latter is prefered?

A couple of reasons. For one, it's just inconsistent in that string
indices should always be quoted--but then not in strings. So people tend
to think that since "$_SERVER[PHP_SELF]" is OK, that $_SERVER[PHP_SELF]
would be OK too--when it's not. It's more of an issue when giving 
examples and writing docs than for coding, really--but it doesn't hurt.

The other reason is that the parser needs help resolving ambiguity with 
more complex expressions inside strings. If you use this syntax all the 
time, you won't get bitten in the butt later. Check this out:

<?php
error_reporting(E_ALL);

$array = array('foo' => array('bar' => 'baz'));
echo "My Baz is $array[foo][bar]\n";
echo "My Baz is {$array['foo']['bar']}\n";
?>

Many times I've had to explain why the output is:

My Baz is Array[bar]
My Baz is baz


-- 
 Torben Wilson <[EMAIL PROTECTED]>
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to