Jump to content

Recommended Posts

Posted

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??

 

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.)

Posted

One of the great uses for echo is for conditional statments.

As in

if condition{ echo series of html}

else{echo other html}

 

I do not think it is nessary to echo all the html.

But as to speed, I don't think it will matter

on the super fast severs here, LOL

But that is just my opinion.

Posted

Hi vendius.

 

As turtle sad, speed won't be a problem, especially here at TCH :D

 

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

><!-- Lots of HTML code here -->
<p>Hello. Your name is <?php echo $name; ?> and you are <?php echo $age ?> years old.</p>
<!-- Lots of HTML code here -->

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:

><!-- Lots of HTML code here -->
<p>
<?php
  if($age > 100)
  {
  ?>
      You are old!
  <?php
  }
  else
  {
  ?>
      You are not that old.
  <?php
  }
?>
<!-- continue HTML code here -->
<?php //do other PHP stuff here ?>
<!-- mode HTML code here -->
.......

 

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.

 

Let me know if this helps :)

Posted

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. :)

Posted

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 :

Hello. Your name is and you are years old.
Posted

$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

$name="arvind"; $age=109;

and it should work if I haven't made a stupid PHP mistake :)

Posted

echo simply copies what you have written into the html the user gets.

 

so typing this in php....

>echo("<h1>Header for page</h1>");

 

is the same as typing this in html

><h1>Header for page</h1>

 

---------------------------------

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...