SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Nov 2006
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Variable Verbage

    I would like to make a multi line variable, that is to say, I would like to imitate something an if statement can do

    With an if statement you can do this:
    PHP Code:
    <?php
    if ($var == 1) { ?>
    Html code here
    can be as long
    or as short as you like
    blah blah blah more html
    <?php ?>
    and that will work fine, what I would like to do is something similar with a variable so far I came up with this
    PHP Code:
    <?php $var ' ?>
    HTML code here
    <?php '
    ?>
    The problem with the above is that when you echo that statement, it will echo the "?>" as well. Is there a way around this???

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Remove the ?> and <?php. They don't belong there since your goal isn't to stop the PHP interpreter and break out into text mode.

    A few easy ways to write it:
    PHP Code:
    $var "HTML code here
    can be as long
    or as short as you like
    blah blah blah more html"

    PHP Code:
    $var "HTML code here\ncan be as long\nor as short as you like\nblah blah blah more html";
    //Note: The newline characters (\n) will only be correctly
    // interpreted if this string is enclosed in double quotes, 
    // not single quotes 
    PHP Code:
    $var = <<<EOD
    HTML code here
    can be as long
    or as short as you like
    blah blah blah more html
    EOD; 

  3. #3
    SitePoint Addict
    Join Date
    Nov 2006
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Remove the ?> and <?php. They don't belong there since your goal isn't to stop the PHP interpreter and break out into text mode.

    A few easy ways to write it:
    PHP Code:
    $var "HTML code here
    can be as long
    or as short as you like
    blah blah blah more html"

    PHP Code:
    $var "HTML code here\ncan be as long\nor as short as you like\nblah blah blah more html";
    //Note: The newline characters (\n) will only be correctly
    // interpreted if this string is enclosed in double quotes, 
    // not single quotes 
    PHP Code:
    $var = <<<EOD
    HTML code here
    can be as long
    or as short as you like
    blah blah blah more html
    EOD; 

    hmmm. What I was going to say was
    "You know what, Nevermind, I was asking because I wanted a way to make a template page that I would call using the include function, yet that I could still Visually edit with software like frontpage. The vairables as such do allow me to edit using the design view, however once in the preview the varible blocks out anything that was attached to it. "

    But now, I'll add on that I don't want to use the double quotes so that I can avoid having to escape all the " in the code, however, i'm intriged by the EOD thing, and I can't seem to make it work, do I need to escape anything.... I'm trying this right now...
    PHP Code:
    <?php $test = <<<EOD ?>
    <p><img border="0" src="j0149887.gif" width="134" height="107"></p>
    <?php EOD?>

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's called heredoc syntax, and it only works exactly as I gave it. Again, you don't want to break out of PHP, since this is PHP code, so get rid of the ?> and <?php. Second, the EOD; line (which can be any identifier, as long as it matches what you put after the <<< to start it) must be flush with the start of the line -- there can't be any characters or whitespace before it.

    Everything between is interpreted virtually the same as if it were enclosed in double quotes, minus having to escape double quotes: variables within the string are interpreted and replaced with their values.

  5. #5
    SitePoint Addict
    Join Date
    Nov 2006
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Dan!, unfortunatly, frontpage doesn't see what's inbetween <?php ?> so I guess I'll do it with if statements, besides it is only two extra lines to write, I just wanted to compact it.

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by prince_mallow View Post
    Thanks Dan!, unfortunatly, frontpage doesn't see what's inbetween <?php ?> so I guess I'll do it with if statements, besides it is only two extra lines to write, I just wanted to compact it.
    You should really get rid of Frontpage. There's a reason it's not a developed product anymore -- it's a POS

  7. #7
    SitePoint Addict
    Join Date
    Nov 2006
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eh... Whatever floats ones boat


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •