www.webdeveloper.com
Results 1 to 6 of 6

Thread: Escaping concept

  1. #1
    Join Date
    May 2009
    Posts
    14

    Escaping concept

    I came across this code in the PHP manual and could not understand how it works. Could someone please explain it in detail. Why does this code work?
    Code:
    <?php
    if ($expression) { 
        ?>
        <strong>This is true.</strong>
        <?php 
    } else { 
        ?>
        <strong>This is false.</strong>
        <?php 
    }
    ?>

  2. #2
    Join Date
    Sep 2008
    Posts
    408
    It's because on ?> the PHP parser is turning itself off until you turn it back on with the <?php

    Although personally I'd vote against that method unless the html in the middle is of a larger context. Otherwise for single lines, stick with if() { echo ''; } etc.

    Note at any point:
    PHP Code:
    <?php
     
    # ..
    ?>Stuff<?php
    exit;
    ?>
    Is pretty much the same as:
    PHP Code:
    <?php
     
    # ..
     
    echo 'Stuff';
     exit;
    ?>
    However, the second one is more efficient as it doesn't leave and reenter Parsing mode. Hopefully that helped a little bit.
    Mullanaphy!
    http://www.mullanaphy.com/

    Unless code is provided or an exact example is requested I think I'm going to start using psuedo code from now on...

    Also, I freelance as well. Inquire within!

  3. #3
    Join Date
    May 2009
    Location
    Herts, UK
    Posts
    68
    I vote for the first method, and have recently blogged about my reasoning:

    http://www.adamjimenez.com/blog/web-...-php-from-html

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,918
    Another approach is to separate the logic from the output:
    PHP Code:
    <?php
    // do your PHP logic first...
    if ($expression) {
       
    $value "true";
    }
    else {
       
    $value "false";
    }
    ?>
    <!-- ...then do your HTML -->
    <p>Blah blah blah.</p>
    <strong>This is <?php echo $value ?>.</strong>
    <p>Etc., etc., and so forth.</p>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2009
    Location
    Herts, UK
    Posts
    68
    Quote Originally Posted by NogDog View Post
    Another approach is to separate the logic from the output:
    PHP Code:
    <?php
    // do your PHP logic first...
    if ($expression) {
       
    $value "true";
    }
    else {
       
    $value "false";
    }
    ?>
    <!-- ...then do your HTML -->
    <p>Blah blah blah.</p>
    <strong>This is <?php echo $value ?>.</strong>
    <p>Etc., etc., and so forth.</p>
    for that i would do:

    PHP Code:
    <p>Blah blah blah.</p>
    <strong>This is <?=($expression) ? 'true' 'false' ?>.</strong>
    <p>Etc., etc., and so forth.</p>
    or for those that hate short-tags:

    PHP Code:
    <p>Blah blah blah.</p>
    <strong>This is <?php echo ($expression) ? 'true' 'false' ?>.</strong>
    <p>Etc., etc., and so forth.</p>

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,918
    For such a trivial example, I'm not going to lose any sleep on how anyone wants to do it. Personally I find lots of switching back and forth between PHP/HTML modes a nightmare when trying to read the code for debugging and maintenance, but your mileage may vary. For non-trivial cases I would tend to put the PHP logic/output into a function or object method, then call that within the HTML.
    PHP Code:
    <?php
    class Foo
    {
       public static function 
    bar($expression)
       {
          
    // let's pretend this actually does something more complicated than this...
          
    return ($expression) ? 'true' 'false';
       }
    }

    $expression = !empty($_GET);
    ?>
    <html><head><title>Test</title></head><body>
    <p>This is <?php echo Foo::bar($expression); ?>.</p>
    </body></html>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles