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. <?php echo '<bar><foo>'.$foobar.'</foo></bar>'; ?> PHP: Please excuse the lack of indentation, I just threw this together. Hope you don't mind
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.
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 ms134 %<bar><foo><?php echo $foobar; ?></foo></bar>Total time: 8.61 ms100 %echo '<bar><foo>'.$foobar.'</foo></bar>';Total time: 6.42 ms
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.
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.
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! *)
2nd option. I can see the mark-up easier in Dreamweaver therefore that's my logic. Quicker turnaround time means more money.
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.
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...
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...)
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
In the land of way, way, way off topic... French knit actually. Macramé is built from knots, not knitting.
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.
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!!!
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.