SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Different ways of scaping in php

    First way of escaping..


    PHP Code:
     echo '<ul  id="nav-subjects-thumbs">',"\n";

    some kind of loop with a format table......


      echo 
    '</ul>',"\n"
    I can see that ,"\n" is used to scape the <ul> instead of using \ slashes as it is escaped in the example below.

    Second way of scaping
    PHP Code:
    "<table width=\"202\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#FF0033\" bgcolor=\"#FFFFFF\" style=\"float:left; position:relative; left:194px; top:150px;\"></td><td width=\"95\"><table width=\"96%\" height=\"217\"></table>"
    I have a question why in the first way of escaping ,\"n" is placed in at the end of the first echo instead of being placed at the beginning;

    PHP Code:
    echo '<ul  id="nav-subjects-thumbs">',"\n"
    The last echo
    PHP Code:
     echo '</ul>',"\n"
    has the ,"\n"; at the end.

    I thought that the first echo since is the first should have the ,"\n"; at the beginning like this:

    PHP Code:
    echo "\n",'<ul  id="nav-subjects-thumbs">'
    Instead of

    PHP Code:
    echo '<ul  id="nav-subjects-thumbs">',"\n"
    and the last echo to stay as it is

    PHP Code:
     echo '</ul>',"\n"

    Don't get me wrong it works like a charm as in the example below


    PHP Code:
     echo '<ul  id="nav-subjects-thumbs">',"\n";

    some kind of loop with a format table......


      echo 
    '</ul>',"\n"
    I just want to learn why it is set up that way in comparison to what I referred to above.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Location
    London, UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is not escaping. This is just printing a new line. It's used to format the html so you can read it better when you go to View Source. It doesnt play any significance at all, you can delete the "\n" and nothing would really change.

  3. #3
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there another way I can use to escape a format table without to use the slashes in every singles quotes like this \"\"?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Maybe heredoc : http://www.php.net/manual/en/languag...syntax.heredoc
    Code PHP:
    $html = <<<EOT
    <table width="202" border="0" cellspacing="0" cellpadding="0"
              bordercolor="#FF0033" bgcolor="#FFFFFF" style="float:left;
              position:relative; left:194px; top:150px;">
    </td>
    <td width="95">
    <table width="96%" height="217"></table>
    EOT;
    Btw, that's some weird html code you posted

  5. #5
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by co.ador View Post
    is there another way I can use to escape a format table without to use the slashes in every singles quotes like this \"\"?
    yep - IMO printing strings containing HTML is poor coding.

    instead of:

    PHP Code:
    echo '<ul  id="nav-subjects-thumbs">',"\n";

    some kind of loop with a format table......

      echo 
    '</ul>',"\n"
    do:

    PHP Code:
    <ul  id="nav-subjects-thumbs">
    <?php
    foreach( $items as $val ){
    ?>
        <li><?php echo $val;?></li>
    <?php
    }
    ?>
    </ul>
    this helps separate the php from the html and makes it easier to work with.

  6. #6
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't worry about the html code it was just an example so you can have a guide

    Thank you guy Jimenez, I think it's another perspective to look at it will be kind of a bug to be putting tags all over but a lot easier than escaping it takes a while.

    Thank you for your perspective

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There are two common methods used by most of the top-dogs round here.

    First is HereDoc, as guido rightly pointed out.

    Second is PrintF, which is extremely useful:
    PHP Code:
    PrintF('<p class="slogan">Welcome to "%s"</p>'$SiteName); 
    As PHP is a language designed FOR the web, outputting HTML is something it is well suited for. Entering and exiting PHP isn't required, and some would argue that entering/exiting PHP constantly is, itself, bad coding.

    PHP Code:
    echo '<ul  id="nav-subjects-thumbs">';
    foreach( 
    $items as $val ){
        echo 
    "<li>{$val}</li>";
        
    //OR:
        
    PrintF('<li>%s</li>'$val);
        
    //OR:
        
    echo <<<ITEM
            <li>{$val}</li>
    ITEM;
    }
    echo 
    '</ul>'
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    some would argue that entering/exiting PHP constantly is, itself, bad coding.
    how so?
    It makes it easier when working with web designers that want to edit the HTML but not the PHP.
    And it also doesn't get in the way of syntax highlighting/ code assist in popular IDEs.

    edit: I have a relevant blog entry on this:
    http://www.adamjimenez.com/blog/web-...-php-from-html
    Last edited by adam.jimenez; Jun 8, 2009 at 08:46.


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
  •