SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast doubli's Avatar
    Join Date
    Sep 2001
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting legible code...

    Whilst writing my PHP code I have found that rather than placing raw HTML In my file I've found it easier to put it into echo statements. Therefore rather than doing say

    some php code here ....
    ?>
    <font class="main">

    <?php
    some more php code there
    etc etc

    I've opted to keep everything within PHP and do something like

    PHP Code:
    echo "<font class='main'>"
    Now, heres my problem ... the code works fine however when you do "View Source" in IE and look at the code it looks totally ugly and unformatted. In kind of makes me look like a messy coder when in actual fact I am pretty tidy.

    My question is, how can I get my code to format better when viewing source?

    Many thanks
    One of lifes luxuries ...

  2. #2
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use the new line \n

    echo "<font class='main'>\n";

    note that this method is actually slower than jumping in and out of PHP tags.

  3. #3
    SitePoint Enthusiast doubli's Avatar
    Join Date
    Sep 2001
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its slower to place the HTML inside ECHOS?? How much slower?
    One of lifes luxuries ...

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    depends on how much html.

    but think about it.
    would it be faster to

    A. Read striaght html

    or

    B. Parse the html through the php parser first, then output it?

    That and it takes longer to type out the html in echo tags and you have to escape the " marks all the time.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well we are only talking milliseconds here

    but.... if you are paranoid about efficiency (like me )

    then check out these test results for different methods of outputting HTML here => http://moorewebsolutions.net/time.php

  6. #6
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe milliseconds, but it's still time the user has to wait

    also, if your server's already slow, why slow it down more by using inefficient code?
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  7. #7
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Formatting legible code...

    Originally posted by doubli
    PHP Code:
    echo "<font class='main'>"
    When you don't have php variables inside your HTML code use single quotes (PHP don't parse those strings)

    PHP Code:
    echo '<font class="main">'

  8. #8
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by firepages

    then check out these test results for different methods of outputting HTML
    Interesting test firepages, but would you provide us more information, what does mean every test (Render by echo breaking in and out(using "), etc)?

  9. #9
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is Lorddogs code for this test, you can see the different methods used in it.


    PHP Code:
    <html>
    <head>
        <title>PHP Document Rendering Times</title>
    </head>

    <body bgcolor=lightblue>
    <center>
    <table>
    <form name=formA>
    <tr><td align=left>Render by echo breaking in and out(using ")</td><td>&nbsp;</td><td><input type=text size=4 name=Render1> ms</td></tr>
    <tr><td align=left>Render by echo breaking in and out(using ')</td><td>&nbsp;</td><td><input type=text size=4 name=Render6> ms</td></tr>
    <tr><td align=left>Render by echo all done in echo(using ")</td><td>&nbsp;</td><td><input type=text size=4 name=Render2> ms</td></tr>
    <tr><td align=left>Render by echo all done in echo(using ')</td><td>&nbsp;</td><td><input type=text size=4 name=Render5> ms</td></tr>
    <tr><td align=left>Render by print breaking in and out</td><td>&nbsp;</td><td><input type=text size=4 name=Render3> ms</td></tr>
    <tr><td align=left>Render by print all done in print</td><td>&nbsp;</td><td><input type=text size=4 name=Render4> ms</td></tr>
    </form>
    </table>
    </center>
    <?
    function getmicrotime(){ 
        list(
    $usec$sec) = explode(" ",microtime()); 
        return ((float)
    $usec + (float)$sec); 
        } 
    // Render1
    $time_start getmicrotime();
    for (
    $i=0$i 1000$i++){
        echo(
    "abcdef");?>ghijk<?echo("lmnop");?>qrstuv<?echo("wxyz");
        }
    $time_end getmicrotime();
    $Render1 $time_end $time_start;
    echo 
    "<BR>";
    // Render6
    $time_start getmicrotime();
    for (
    $i=0$i 1000$i++){
        echo(
    'abcdef');?>ghijk<?echo('lmnop');?>qrstuv<?echo('wxyz');
        }
    $time_end getmicrotime();
    $Render6 $time_end $time_start;
    // Render2
    echo "<BR>";
    $time_start getmicrotime();
    for (
    $i=0$i 1000$i++){
        echo(
    "abcdefghijklmnopqrstuvwxyz");
        }
    $time_end getmicrotime();
    $Render2 $time_end $time_start;
    // Render5
    echo "<BR>";
    $time_start getmicrotime();
    for (
    $i=0$i 1000$i++){
        echo(
    'abcdefghijklmnopqrstuvwxyz');
        }
    $time_end getmicrotime();
    $Render5 $time_end $time_start;
    // Render3
    echo "<BR>";
    $time_start getmicrotime();
    for (
    $i=0$i 1000$i++){
        print(
    "abcdef");?>ghijk<?print("lmnop");?>qrstuv<?print("wxyz");
        }
    $time_end getmicrotime();
    $Render3 $time_end $time_start;
    // Render4
    echo "<BR>";
    $time_start getmicrotime();
    for (
    $i=0$i 1000$i++){
        print(
    "abcdef");?>ghijk<?print("lmnop");?>qrstuv<?print("wxyz");
        }
    $time_end getmicrotime();
    $Render4 $time_end $time_start;
    ?>
    <script>
    document.formA.Render1.value = "<? echo($Render1); ?>".substr(0,5);
    document.formA.Render2.value = "<? echo($Render2); ?>".substr(0,5);
    document.formA.Render3.value = "<? echo($Render3); ?>".substr(0,5);
    document.formA.Render4.value = "<? echo($Render4); ?>".substr(0,5);
    document.formA.Render5.value = "<? echo($Render5); ?>".substr(0,5);
    document.formA.Render6.value = "<? echo($Render6); ?>".substr(0,5);
    </script>
    </body>
    </html>
    From my experience with microtime()... you have to run these things several times and then take an average to get any realistic guide, but the results here always seem pretty much the same.


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
  •