Stop PHP code from executing

Discussion in 'PHP' started by jis2507, Mar 14, 2010.

  1. #1
    Hi all!

    I'm writing a PHP tutorial for my site, however I have a very anoying problem.

    When I try to write the tut, it always executes the php code that I'm writing for the tut.

    How can I stop this?? For instance, look at the example below.

    php_tut1.php
    
    <?php
    session_start();
    ?>
    <HTML>
    <head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>Tutorial World</title>
    </head>
    <body>
    <p>Welcome to my PHP Tutorial!</p>
    <br><br><br>
    An example PHP file could be this:
    <br>
    <p style="border:3px solid black;">
    <?php                      /* How do I stop the code from here:*/
    echo "Hello World!";
    ?>                          /* To Here from executing? */
    </p>
    </body>
    </html>
    
    PHP:
    This is just an example file of a .php file that would be on my site.

    Basically I don't want the code that is part of the demo from executing.

    Anybody got any ideas?

    Tere must be some way of doing it because there are so many tut sites out there.
     
    jis2507, Mar 14, 2010 IP
  2. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #2
    <?php
    highlight_string('<?php
    echo "Hello World!";
    ?>');
    ?>
     
    Bohra, Mar 14, 2010 IP
  3. jis2507

    jis2507 Greenhorn

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #3
    Ok I'll try that.

    Is there any way of doing this without using php code however?
     
    jis2507, Mar 14, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Save the tutorial within a non php extension such as .html and post the code within <pre> tags.
     
    danx10, Mar 14, 2010 IP
  5. TYPELiFE

    TYPELiFE Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can use the PHP heredoc syntax: phpf1.com/tutorial/php-heredoc-syntax.html

    <?php
       $str = <<<DEMO
    This is a demo message with heredoc. Put any formatted message in here. <?php echo "hi"; ?> hehe #!/bin/bash/ echo hi
    DEMO;
     
       echo $str;
    ?>
    Code (markup):
     
    TYPELiFE, Mar 14, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    @TYPELiFE

    That would'dnt always work in this case. As if the php contained echo's, it would evaluate it as html, theirfore would cause conflict with the actual webpage's html.

    Theirfore do this:

    php_tut1.php
    <?php
    session_start();
    ?>
    <HTML>
    <head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>Tutorial World</title>
    </head>
    <body>
    <p>Welcome to my PHP Tutorial!</p>
    <br><br><br>
    An example PHP file could be this:
    <br>
    <p style="border:3px solid black;">
    <?php
    highlight_string('<?php                 
    echo "Hello World!";
    ?>');
    ?>
    </p>
    </body>
    </html>
    PHP:
     
    danx10, Mar 14, 2010 IP
  7. SoftCloud

    SoftCloud Well-Known Member

    Messages:
    1,060
    Likes Received:
    28
    Best Answers:
    2
    Trophy Points:
    120
    #7
    Or replace the < with &lt; and > with &gt; :)
     
    SoftCloud, Mar 15, 2010 IP
  8. miller23

    miller23 Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    this is the easiest solution.
     
    miller23, Mar 17, 2010 IP