|
-
Nov 20th, 2007, 10:57 AM
#1
Thread Starter
Frenzied Member
variables in heredoc
I just started using this heredoc syntax after reading about it. Pretty handy and easy to use.
One thing I dont understand though.
The following will fail:
Code:
$ret_val = <<<EOT
$_SESSION['usr_sal'];
EOT;
With a
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
error.
However, this will work fine:
Code:
$var = $_SESSION['usr_sal'];
$ret_val = <<<EOT
$var
EOT;
I dont get it...both are essentially the same thing, or not?
The same thing goes for constants I defined in included files.
Last edited by StrangerInBeijing; Nov 22nd, 2007 at 09:21 AM.
Reason: Resolved
-
Nov 20th, 2007, 10:59 AM
#2
Hyperactive Member
Re: variables in heredoc
Try:
PHP Code:
$ret_val = <<<EOT
{$_SESSION['usr_sal']}
EOT;
Heredoc is treated like double quoted strings, without the double quotes, so in theory that should work.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Nov 20th, 2007, 11:01 AM
#3
Thread Starter
Frenzied Member
Re: variables in heredoc
wow..that was the fastest reply i ever seen...thanks!
just closed my ide (exactly midnight....i'll call it a day), so will try it out in morning.
-
Nov 20th, 2007, 08:28 PM
#4
Thread Starter
Frenzied Member
Re: variables in heredoc
That work with the session variable, but not with a CONSTANT.
Code:
$ret_val = <<<EOT
{_WELCOME_} {$_SESSION['usr_sal']}
EOT;
will print out like : _WELCOME_ StrangerInBeijing
but
Code:
$welcome = _WELCOME_;
$ret_val = <<<EOT
$welcome {$_SESSION['usr_sal']}
EOT;
works just fine and give "Welcome StrangerInBeijing".
btw:
Code:
{_WELCOME_ $_SESSION['usr_sal']}
will give an error
-
Nov 22nd, 2007, 09:06 AM
#5
Hyperactive Member
Re: variables in heredoc
Yes, I'm afraid you can't use constants in heredoc as there's no way to differentiate it from normal textual content. I'd recommend simply using the second method that you proposed:
PHP Code:
$welcome = _WELCOME_;
$ret_val = <<<EOT
$welcome {$_SESSION['usr_sal']}
EOT;
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Nov 22nd, 2007, 09:13 AM
#6
Thread Starter
Frenzied Member
Re: variables in heredoc
mystery solved then...thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|