1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

HTML in PHP: Which method is better?

Discussion in 'PHP' started by swift_lee_o, Mar 25, 2013.

  1. #1
    Alright, so out of all these methods that I currently know of; Which is the better way? I know a bit but i'm still trying to learn more so I appreciate everyone's guidance and help. Constructive critisism is always welcome.

    1.

    <?php
     
    echo <<<HTML
     
    <bar>
    <foo>{$foobar}</foo>
    </bar>
     
    HTML;
     
    ?>
    PHP:
    2.

    <bar>
     
    <foo><?php echo $foobar; ?></foo>
     
    </bar>
    PHP:
    or

    3.
    SEMrush
    <?php
     
    echo '<bar><foo>'.$foobar.'</foo></bar>';
     
    ?>
    PHP:

    Please excuse the lack of indentation, I just threw this together. Hope you don't mind :)
     
    swift_lee_o, Mar 25, 2013 IP
    SEMrush
  2. crazyblogger

    crazyblogger Active Member

    Messages:
    430
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    63
    #2
    I prefer breaking in and out of php rather than echoing out the raw html from php. I think it will save some server resources.
     
    crazyblogger, Mar 25, 2013 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,730
    Likes Received:
    26
    Best Answers:
    13
    Trophy Points:
    125
    #3
    second example, as PHP don't have to parse the HTML code (and doesn't even have to read and parse its content).

    Small edit - for custom benchmark wich shows who's faster
    
     
    <?php
     
    $runningTest = array();
    $starttime = microtime(true);
    $honderdProcent = 0;
     
    // load settings
    $foobar = 'foobar';
     
     
    // running test 1
    $runningTest['test1']['script'] = 'echo <<<HTML
     
    <bar>
    <foo>{$foobar}</foo>
    </bar>
     
    HTML;';
    $runningTest['test1']['starttime'] = microtime(true);
    ob_start();
     
    for ($Xudadaxxee = 0; $Xudadaxxee < 10000; $Xudadaxxee++)
    {
    echo <<<HTML
     
    <bar>
    <foo>{$foobar}</foo>
    </bar>
     
    HTML;
    }
    ob_end_clean();
    $runningTest['test1']['endtime'] = microtime(true);
    $runningTest['test1']['totaltime'] = $runningTest['test1']['endtime'] - $runningTest['test1']['starttime'];
    $honderdProcent = (($honderdProcent > $runningTest['test1']['totaltime']) ? $runningTest['test1']['totaltime'] : ($honderdProcent == 0) ? $runningTest['test1']['totaltime'] : $honderdProcent);
     
    // running test 2
    $runningTest['test2']['script'] = '<bar>
     
    <foo><?php echo $foobar; ?></foo>
     
    </bar>';
    $runningTest['test2']['starttime'] = microtime(true);
    ob_start();
     
    for ($Xudadaxxee = 0; $Xudadaxxee < 10000; $Xudadaxxee++)
    {
    ?>
    <bar>
     
    <foo><?php echo $foobar; ?></foo>
     
    </bar>
    <?php
    }
    ob_end_clean();
    $runningTest['test2']['endtime'] = microtime(true);
    $runningTest['test2']['totaltime'] = $runningTest['test2']['endtime'] - $runningTest['test2']['starttime'];
    $honderdProcent = (($honderdProcent > $runningTest['test2']['totaltime']) ? $runningTest['test2']['totaltime'] : ($honderdProcent == 0) ? $runningTest['test2']['totaltime'] : $honderdProcent);
     
    // running test 3
    $runningTest['test3']['script'] = 'echo \'<bar><foo>\'.$foobar.\'</foo></bar>\';';
    $runningTest['test3']['starttime'] = microtime(true);
    ob_start();
     
    for ($Xudadaxxee = 0; $Xudadaxxee < 10000; $Xudadaxxee++)
    {
    echo '<bar><foo>'.$foobar.'</foo></bar>';
    }
    ob_end_clean();
    $runningTest['test3']['endtime'] = microtime(true);
    $runningTest['test3']['totaltime'] = $runningTest['test3']['endtime'] - $runningTest['test3']['starttime'];
    $honderdProcent = (($honderdProcent > $runningTest['test3']['totaltime']) ? $runningTest['test3']['totaltime'] : ($honderdProcent == 0) ? $runningTest['test3']['totaltime'] : $honderdProcent);
    $endtime = microtime(true);
    $totaltime = $endtime - $starttime;
     
     
    $procent = $honderdProcent / 100;
    foreach ($runningTest AS $key => $row)
    {
        $runningTest[$key]['procent'] = ($row['totaltime'] / $procent);
    }
    ?>
    <!-- stylesheet unavailable online -->
    <link rel="stylesheet" type="text/css" href="createbenchmark-run.css" />
    <div class='Test'>
        <h2>
            <span class='Title'>String Output</span>echo html vs inline vs echo full</span>
        </h2>
        <div class='Blurb'>
            <p>Running times: 10000<br />
              Loaded settings:<br />$foobar = 'foobar';</p>
        </div>
    <?php foreach ($runningTest AS $key => $row) { ?>
        <div class='Option'>
            <div class='Percentage <?=(($row['procent'] < 151) ? "green" : (($row['procent'] < 301) ? "yellow" : (($row['procent'] < 601) ? "orange" : "red")));?>'><?=round($row['procent']);?> %</div>
            <p class='Description'><?=nl2br(htmlspecialchars($row['script']));?></p>
            <p class='Info'>Total time: <span><?=round($row['totaltime']*1000, 2);?> ms</span></p>
        </div>
    <?php } ?>
        <div class='Results'>
            <span>Total time of test:</span>
            <p><?=round($totaltime,2);?> seconds</p>
        </div>
    </div>
    [/code]
    PHP:
    Output each runned 10.000 times:

    echo <<<HTML
    <bar>
    <foo>{$foobar}</foo>
    </bar>
    HTML;
    Total time: 11.27 ms
    134 %
    <bar>
    <foo><?php echo $foobar; ?></foo>
    </bar>
    Total time: 8.61 ms
    100 %
    echo '<bar><foo>'.$foobar.'</foo></bar>';
    Total time: 6.42 ms
     
    Last edited: Mar 26, 2013
    EricBruggema, Mar 25, 2013 IP
  4. trecords

    trecords Well-Known Member

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #4
    I think Smarty Framework is better in current case. You need just practice to code on it then it will be more easy to work on it. It has many benefits security and so on.
     
    trecords, Mar 26, 2013 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,976
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #5
    eric's results mimic my preferences -- I cannot STAND 'heredoc' or any of the other goofy string methods... and constantly dropping in and out of PHP is just IMHO sloppy half-assed coding -- but to be fair, I'm the nutjob who if I had my way would have both heredoc and <?php ?> stricken from PHP entirely as pointless garbage.

    My preferred method is:
    echo '
    	<bar>
    		<foo>
    			',$foobar,'
    		</foo>
    	</bar>';
    Code (markup):
    Making sure there's clear formatting, and notice I use comma's instead of string addition. They tend to use less RAM since each section is sent individually instead of having to create memory to add a new string together before sending. Only works on echo and if you are inside an eval you have to still add things together, but it's pretty important to know given some of the... counterintuitive results string addition can give you.

    An example I use all the time:
    <?php
    
    function writeIs() {
    	echo 'is ';
    }
    
    echo 'this ',writeIs(),'a test.<br />';
    echo 'this '.writeIs().'a test?<br />';
    
    ?>
    Code (markup):
    That code actually outputs:

    this is a test.
    is this a test?

    Illustrating how things work with echo -- each part between the comma's is sent separately in order... added together as a string with periods means a new memory reference is built, and the values are plugged into it, and then the new reference/allocated memory (basically a temp variable on a 'stack') is sent to echo en-mass. This means any functions run before output instead of during output --as that echo in the function shows since it fires before the "this" is echoed out.

    You'll see turdpress developers screwing that one up all the time -- further compounded by their idiotic use of <?php ?> multiple times on every blasted line.
     
    deathshadow, Mar 26, 2013 IP
  6. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,730
    Likes Received:
    26
    Best Answers:
    13
    Trophy Points:
    125
    #6
    I rather choose the third option but that's because i mostly use templates to display html data. If i would use php and html combined i'm agreeing with deathshadow! *)
     
    EricBruggema, Mar 26, 2013 IP
  7. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #7
    2nd option. I can see the mark-up easier in Dreamweaver therefore that's my logic. Quicker turnaround time means more money.
     
    HuggyStudios, Mar 27, 2013 IP
  8. Steve202

    Steve202 Member

    Messages:
    59
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    45
    #8
    Option 2 for me. Easier to see the mark-up.
     
    Steve202, Mar 27, 2013 IP
  9. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    138
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    90
    #9
    Option 2, and 3 are better way's of doing this. Make sure you're nesting your lines of code, and using proper tabs, etc so it's much easier for you to read, and others if you choose to share it.
     
    Vick.Kumar, Mar 27, 2013 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,976
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #10
    I'm shocked when people say option 2 being "easier to see the markup" -- has the exact opposite effect for me... makes the markup AND the PHP nearly impossible to see.

    Admittedly, I say the same thing about color syntax highlighting...
     
    deathshadow, Mar 27, 2013 IP
  11. abraham26

    abraham26 Member

    Messages:
    73
    Likes Received:
    6
    Best Answers:
    2
    Trophy Points:
    48
    #11
    Option 2 looks ok. Separate PHP tags from HTML.
     
    abraham26, Mar 27, 2013 IP
  12. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #12
    You sound like a notepad using old school c programmer. :)
     
    HuggyStudios, Mar 28, 2013 IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,976
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #13
    Worse, I started out hand compiling RCA 1802 code and entering it on toggle switches.

    ... and I'm really more of a Pascal guy -- C always felt needlessly and pointlessly cryptic, never really understood why it or *nix caught on as big as they did, especially AFTER the computer revolution during which *nix was mostly irrelevant except for the back-room server geeks.

    (he says while working on some C++ code for a teensy++ driven device...)
     
    deathshadow, Mar 28, 2013 IP
  14. wiicker95

    wiicker95 Well-Known Member

    Messages:
    438
    Likes Received:
    37
    Best Answers:
    10
    Trophy Points:
    100
    #14
    Is that macramé? So you were a macramé guy all along huh...
     
    wiicker95, Mar 28, 2013 IP
  15. duckz

    duckz Active Member

    Messages:
    245
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    68
    #15
    Number 2 - usually coz I like to separate HTML in a separate template file
    Number 3 - If need to print the html without a template file
     
    duckz, Apr 3, 2013 IP
  16. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,976
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #16
    In the land of way, way, way off topic...
    French knit actually. Macramé is built from knots, not knitting.
     
    deathshadow, Apr 3, 2013 IP
  17. ezprint2008

    ezprint2008 Active Member

    Messages:
    612
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    90
    Digital Goods:
    4
    #17
    Its too bad that PHP doesn't have a {HTML} tag that can start /end everything within it in HTML.
    That way no echo single/double quote issues etc.
     
    ezprint2008, Apr 3, 2013 IP
  18. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #18
    You could use a template engine, or make one to accomplish this.
     
    HuggyStudios, Apr 4, 2013 IP
  19. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,976
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #19
    How is that any different from:

    ?>HTML Goes Here<?php

    You're basically talking extra crap to do what the already sloppy <?php ?> tags do, just in reverse.

    Since PHP is already a template engine, why the **** do people insist on running another one on top of it for no good reason!?! That still confuses the **** out of me to this day, particularly useless crippleware garbage like Smarty... It's not like PHP is THAT hard if you bother using functions, and then *SHOCK* send values to the functions the same way you would a template, keeping the presentational functions in their own file.

    Templating systems... bah! More idiotic BULL!!!
     
    deathshadow, Apr 4, 2013 IP
  20. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #20
    For other developers to be able to code the same so everyone understands within a team. You clearly live in your mums basement writing code all by your own, where everyone else has to work in teams.
     
    HuggyStudios, Apr 4, 2013 IP
    YoGem likes this.