For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > October 2006 > Define Constant via heredoc??









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Define Constant via heredoc??
Koncept

2006-10-30, 7:03 pm


Is there any way to assign a constant or function value via heredoc
syntax? Obviously, I realize the fact I can declare a variable outside
of the scope of the definition, but I was wondering if this is somehow
possible to do..

<?php
define( 'FOO', <<<EOF
This is an example of what I am looking to do.
EOF;
);
?>

Possible in some creative way? I am just curious if anybody knows a way
to get something like this working or if it's possible in PHP.

To save unnecessary replies, yes.. I realize I can do it like this:

<?php
$string = "This is an example of what I am looking to do.";
define( 'FOO', $string );
?>

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Rik

2006-10-30, 7:03 pm

Koncept wrote:
> Is there any way to assign a constant or function value via heredoc
> syntax? Obviously, I realize the fact I can declare a variable outside
> of the scope of the definition, but I was wondering if this is somehow
> possible to do..
>
> <?php
> define( 'FOO', <<<EOF
> This is an example of what I am looking to do.
> EOF;
> );
>
> Possible in some creative way? I am just curious if anybody knows a
> way to get something like this working or if it's possible in PHP.


Yes, the trick is from the PHP Manual:
"It is very important to note that the line with the closing identifier
contains no other characters, except _possibly_ a semicolon (;)."

That 'possibly' is a bit vague for it's purpose:
When assigning a value a value like a string:

$string = <<<HEREDOC
This is specified with heredoc
HEREDOC;

You have to close the statement with a ;, like always.

When using it in a function however, is should not be used until the ; is
actually required, so, your answer:
<?php
define( 'FOO', <<<EOF
This is an example of what I am looking to do.
EOF
);
echo FOO;
?>
--
Rik Wasmus


Koncept

2006-10-30, 7:03 pm

In article <51c57$45403f00$8259c69c$16119@news2.tudelft.nl>, Rik
<luiheidsgoeroe@hotmail.com> wrote:

> Koncept wrote:
>
> Yes, the trick is from the PHP Manual:
> "It is very important to note that the line with the closing identifier
> contains no other characters, except _possibly_ a semicolon (;)."
>
> That 'possibly' is a bit vague for it's purpose:
> When assigning a value a value like a string:
>
> $string = <<<HEREDOC
> This is specified with heredoc
> HEREDOC;
>
> You have to close the statement with a ;, like always.
>
> When using it in a function however, is should not be used until the ; is
> actually required, so, your answer:
> <?php
> define( 'FOO', <<<EOF
> This is an example of what I am looking to do.
> EOF
> );
> echo FOO;
> ?>


Thanks! I never tried omitting the semicolon after the end of the
heredoc in testing (figured it was required under all circumstances).
Thanks so much for the help.

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com