Re: sloppy use of constants as strings. WAS: What does "<<<" mean?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 01/05/07, Daevid Vincent <daevid@xxxxxxxxxx> wrote:
> > > echo <<<EOF
> > > BROWSER: $_SERVER[HTTP_USER_AGENT]
> > > EOF;
> >
> > Isn't that form (sans quote marks) deprecated and frowned upon?
>
> <?php
>
> error_reporting( E_ALL );
>
> echo <<<EOF
> BROWSER: $_SERVER[HTTP_USER_AGENT]
> EOF;
>
> Why would cleaner, perfectly error free code be frowned upon?

http://us2.php.net/manual/en/language.types.array.php
"A key may be either an integer or a string. If a key is the standard
representation of an integer, it will be interpreted as such (i.e. "8" will
be interpreted as 8, while "08" will be interpreted as "08")."

I was always under the impression that using:

        $_SERVER[HTTP_USER_AGENT] or $foo[myindex]

Was "bad" compared to the proper way of:

        $_SERVER['HTTP_USER_AGENT'] or $foo['myindex']

True, but notice he's using it inside a heredoc string and the rules
are slightly different for string parsing.

http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing

Whereas outside a string, $foo[myindex] will give you a notice and
$foo['myindex'] is OK; Inside a string, $foo[myindex] will give you no
notice and $foo['myindex'] will give you a parse error.

The manual suggests that $foo[myindex] is treated the same inside a
string as outside - if 'myindex' is defined as a constant then that is
what's used as the index. This doesn't seem to be born out by reality.

<?php
error_reporting(E_ALL);

$test = array(
         'FOO' => 'Bareword',
         'BAR' => 'Constant'
       );

define( 'FOO', 'BAR');

print "\$test[FOO]: FOO is interpreted as a $test[FOO]\n";
print "{\$test[FOO]}: FOO is interpreted as a {$test[FOO]}\n";
print "{\$test['FOO']}: FOO is interpreted as a {$test['FOO']}\n";

?>

$ php5 test.php

$test[FOO]: FOO is interpreted as a Bareword
{$test[FOO]}: FOO is interpreted as a Constant
{$test['FOO']}: FOO is interpreted as a Bareword

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux