Html Inside Your Php

liunx

Guest
Is it better to have all of your HTML echo'ed to the screen or jump in and out of php to code your HTML straight without needing echo??<br /><br />Does it affect the processing speed enough to matter when the server has to gkepp getting into and out of php mode?? (I hope this question makes sense, if not, I'll try and post an example.)<!--content-->
One of the great uses for echo is for conditional statments.<br />As in <br />if condition{ echo series of html}<br />else{echo other html}<br /><br />I do not think it is nessary to echo all the html.<br />But as to speed, I don't think it will matter<br />on the super fast severs here, LOL<br />But that is just my opinion.<!--content-->
Ok, I didn't think it would effect enough to make much difference. Thanks.<br /><br /><br /> Rock Sign<!--content-->
Hi vendius.<br /><br />As turtle sad, speed won't be a problem, especially here at TCH <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/wink.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" /><br /><br />As for the other question, if I have to output some data in the middle of my HTML, I like to do it "inline", like so<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><!-- Lots of HTML code here --><br /><p>Hello. Your name is <?php echo $name; ?> and you are <?php echo $age ?> years old.</p><br /><!-- Lots of HTML code here --><!--c2--></div><!--ec2--><br />I prefer that method, unless I have a huge amount of PHP code to put or have to break out of the PHP code lots of times. In such cases, I do it this way:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><!-- Lots of HTML code here --><br /><p><br /><?php<br />   if($age > 100)<br />   {<br />   ?><br />       You are old!<br />   <?php<br />   }<br />   else<br />   {<br />   ?><br />       You are not that old.<br />   <?php<br />   }<br />?><br /><!-- continue HTML code here --><br /><?php //do other PHP stuff here ?><br /><!-- mode HTML code here --><br />.......<!--c2--></div><!--ec2--><br /><br />You can break out of "PHP mode" right in the middle of an if statement and output whatever HTML you want for that condition. And you don't need to worry about escaping special characters, because you're not in PHP mode but in HTML mode.<br /><br />Let me know if this helps <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Thanks borfast. I do a mix of styles depending on how much php or html I have like you mentioned. I've just always wondered if technically I should be chosing one way other another. Thanks for your help. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /><!--content-->
sorry this is a realy newbie question what does this echo do. I just created a page with the code above but nothign happened only showed me : <!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->Hello. Your name is and you are years old.<!--QuoteEnd--></div><!--QuoteEEnd--><!--content-->
$name and $age are variables that have to be filled elsewhere. It could be a form that asks you to input that info or even a cookie saved from when it asked you last time. Just to see what happens, after the <?php put <br />$name="arvind"; $age=109; <br />and it should work if I haven't made a stupid PHP mistake <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
echo simply copies what you have written into the html the user gets.<br /><br />so typing this in php....<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo("<h1>Header for page</h1>");<!--c2--></div><!--ec2--><br /><br />is the same as typing this in html<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><h1>Header for page</h1><!--c2--></div><!--ec2--><br /><br />---------------------------------<br />It's useful for when you want to dynamically update a page or have a form or something that you can't do in standard html.<!--content-->
 
Back
Top