Results 1 to 6 of 6

Thread: variables in heredoc

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Resolved 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

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  3. #3

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    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.

  4. #4

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    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

  5. #5
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  6. #6

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    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
  •  



Click Here to Expand Forum to Full Width