inclosed variable heredoc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hihibibihi@hotmail.com

    inclosed variable heredoc

    hi all

    <?php
    $foo="NO";
    $text=<<<EO
    The variable is named: $foo
    EO;

    if (TRUE)
    {
    $foo="YES";
    die($text); //how to get $foo="YES" into $text?
    }
    ?>

    Trying to get $foo="YES" into $text, but NO appears instead. Any ideas?

    gr Jan

  • Janwillem Borleffs

    #2
    Re: inclosed variable heredoc

    hihibibihi@hotm ail.com wrote:
    Trying to get $foo="YES" into $text, but NO appears instead. Any
    ideas?
    >
    The way you are doing it, $text has already been assigned a value before
    you're setting the new value for $foo.

    Turn it around and you will be fine:

    $foo="NO";

    if (TRUE) {
    $foo="YES";
    }

    $text=<<<EO
    The variable is named: $foo
    EO;

    print $text;


    JW


    Comment

    • Toby Inkster

      #3
      Re: inclosed variable heredoc

      hihibibihi wrote:
      <?php
      $foo="NO";
      $text=<<<EO
      The variable is named: $foo
      EO;
      >
      if (TRUE)
      {
      $foo="YES";
      die($text); //how to get $foo="YES" into $text?
      }
      ?>
      As Janwillem said, you're doing things the wrong way around. What I think
      you're actually asking for is a way to specify the format of the error
      message at the top of the code instead of way down the bottom when the
      error actually happens. Here, sprintf() is your friend...

      <?php
      $foo = 'NO';
      define('ERRMSG_ FOO', 'Foo is now "%s", so I shall die.');

      if (1)
      {
      $foo = 'YES';
      die(sprintf(ERR MSG_FOO, $foo));
      }
      ?>

      You don't need to use a constant of course -- you could use:

      <?php
      $foo = 'NO';
      $ERRMSG_FOO = 'Foo is now "%s", so I shall die.';

      if (1)
      {
      $foo = 'YES';
      die(sprintf($ER RMSG_FOO, $foo));
      }
      ?>

      But assuming the message format is not going to change half-way through
      your script, a constant seems more sensible.

      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact

      Comment

      • hihibibihi@hotmail.com

        #4
        Re: inclosed variable heredoc

        Toby Inkster schreef:
        hihibibihi wrote:
        >
        <?php
        $foo="NO";
        $text=<<<EO
        The variable is named: $foo
        EO;

        if (TRUE)
        {
        $foo="YES";
        die($text); //how to get $foo="YES" into $text?
        }
        ?>
        >
        As Janwillem said, you're doing things the wrong way around. What I think
        you're actually asking for is a way to specify the format of the error
        message at the top of the code instead of way down the bottom when the
        error actually happens. Here, sprintf() is your friend...
        Janwillem and Toby,

        Thanks for your quick reply

        But i dont think i made clear enough what i was aiming at (sorry about
        that): i wrote a webform (html code stored in $text), if user hits
        submit-button without filling in required formfields, form should
        reappear with warning -message added (stored in $foo)

        Any further ideas?

        gr Jan

        Comment

        • Janwillem Borleffs

          #5
          Re: inclosed variable heredoc

          hihibibihi@hotm ail.com wrote:
          But i dont think i made clear enough what i was aiming at (sorry about
          that): i wrote a webform (html code stored in $text), if user hits
          submit-button without filling in required formfields, form should
          reappear with warning -message added (stored in $foo)
          >
          HTML code stored in $text? Why not use static HTML with inline code blocks:

          <html>
          <body>
          <?php if (!isset($_POST['foo'])) { ?>
          Foo isn't set
          <?php } ?>
          </body>
          </html>

          Anyways, you can use stringified HTML and combine it with Toby's suggestion:

          <?php

          $html = '<html><body>%s </body></html>';
          $err = '';

          if (true) $error = 'You forgot to supply foo';

          printf($html, $error);

          ?>


          JW


          Comment

          Working...