Re: I am RTFM, but still stumbling on how to get built-in functions parsed in heredoc

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

 



On Sat, Jul 11, 2009 at 14:15, Eddie Drapkin<oorza2k5@xxxxxxxxx> wrote:
>
[snip!]
> which would be cast to a string (as an undefined constant) and then
[snip!]

    That's exactly the point I was waiting for someone to make.  I
wanted someone to question why it was a Bad Idea[TM] to leave the key
unquoted and typecast.  I even tried to make a big enough deal about
it so as to draw attention, but not make the answer plainly-obvious.
And there ya' go.  (Sorry, I feel like I'm in Teacher Mode today.... I
don't mean to come off as condescending, if I am.)

    Q: Why should I use quotes in my array keys?
    A: Because, while it can be typecast to the literal string, it
will be translated to the value of a previously-defined constant if
one exists by the same name.

    .... but wait, there's more!  This is where it gets fun and confusing.

    When inside of a HEREDOC, all non-braced array variables' array
keys are forced literals, as opposed to translatables when the array
variable is braced.  The following example (hopefully) will help to
explain what I mean:

<?php

$bar[apple] = 'Apple';
$bar[orange] = 'Orange';

define(apple,'orange');

echo $bar['apple']."\n"; // Apple - Quoting causes the literal string
to grab the typecast string
echo $bar[apple]."\n"; // Orange - The unquoted string is prioritized
as the defined constant

$foo = <<<EOT
$bar[apple] $bar[orange]
EOT;

$oof =<<<EOT
{$bar[apple]} {$bar[orange]}
EOT;

echo $foo."\n"; // Apple Orange - $bar[apple] is translated as a
whole, apple is not
echo $oof."\n"; // Orange Orange - apple is translated first into
orange, making both $bar[orange]
?>


-- 
</Daniel P. Brown>
daniel.brown@xxxxxxxxxxxx || danbrown@xxxxxxx
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig

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