Inserting an HTML comment into PHP

Discussion in 'PHP' started by archard, Feb 19, 2008.

  1. #1
    I'm using Drupal, and on one of my pages I have to input some PHP code, and within that PHP code I want to insert an HTML comment that looks like this:

    <!--pagebreak-->
    PHP:
    This comment activates a function in another module that splits the page up into to separate pages. The reason I need it within the PHP code and not in between two separate <?php ... ?> blocks is because the code before and the code after the HTML comment need the same variables. If I just do a normal print statement PHP basically ignores the whole comment and goes straight to the ; at the end of the line and gives me an 'unexpected $end' error. So what I want to do is to be able to put that HTML comment within the confines of a block of PHP code and still have it parse as it would if it was outside the block of PHP code.
     
    archard, Feb 19, 2008 IP
  2. walkere

    walkere Active Member

    Messages:
    112
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    I think you're misunderstanding the scope of php. If you create a variable in the global scope in any php snippet, it'll be available anywhere further on the page in the global scope.

    Closing the php block and opening a new one doesn't kill all of the existing variables.

    <?php $x = "Roar!"; ?>
    ... random html here ...
    <?php echo $x; //  This outputs "Roar!" ?>
    PHP:
    You must have made a typo. I was able to output <!-- pagebreak --> just fine with both print and echo.

    echo "<!-- pagebreak -->";
    PHP:
    Good luck,
    - Walkere
     
    walkere, Feb 19, 2008 IP
  3. sandeepb

    sandeepb Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well there are different ways:

    An IF Statement if your achieving that.

    <?php
    
    if($something){
    echo "<!-pagebreak-->";
    }
    else
    {
    echo "Something Else";
    }
    PHP:

    A simple echo

    <?php
    echo "<!-pagebreak-->";
    ?>
    
    PHP:


    Something Else

    <?php
    if($something){
    $x = "<!--pagebreak-->";
    }
    else
    {
    $x = "SOMETHING ELSE";
    }
    
    echo $x;
    ?>
    
    PHP:
     
    sandeepb, Feb 22, 2008 IP