Embedding HTML in PHP

Is this a good syntax to use? Best practices question

Page 1 of 1

5 Replies - 616 Views - Last Post: 19 April 2010 - 07:11 PM Rate Topic: -----

#1 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Embedding HTML in PHP

Posted 12 April 2010 - 06:08 AM

Hello all,

I am fairly new to PHP(a month or so) and have been working on a project as part of a student work term. My question is regarding some syntax and best practices. All of the beginner books and tutorials that I have read so far that output HTML based on a condition were done so like this :

<?php
if(condition)
{

echo " HTML HERE " ; 

}
?>



However, after looking around for a better way I found the following syntax this morning :
<?php if(condition): ?>

HTML HERE

<?php endif; ?>




The second method seems much cleaner to me. My question is, is there any reason I shouldn't use the second method? Is there another way that is even better? I am only asking because this is the first time I have seen that syntax and I have read a fair bit in the past few weeks and am wondering if that is because it is not typically used for some reason.

Thanks to anyone who can offer some advice, it is very much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Embedding HTML in PHP

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,008
  • Joined: 08-August 08

Re: Embedding HTML in PHP

Posted 12 April 2010 - 06:53 AM

It's a matter of opinion. In my opinion, the second method often leads to multiple breaks in/out of php, creating confusion and making the code difficult to read. Most of the newbie problems I see in this forum use the second method.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Embedding HTML in PHP

Posted 12 April 2010 - 08:13 AM

There's no real silver bullet here...you should see some of the code we've written!

Heredocs -- see also PHP.net's documentation is another option.
Was This Post Helpful? 1
  • +
  • -

#4 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Embedding HTML in PHP

Posted 12 April 2010 - 08:22 AM

Hmmm guess I will just have to play around and find what works best for me, thanks much guys. Also, the HEREDOCS thing is new to me as well :D .
Was This Post Helpful? 0
  • +
  • -

#5 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: Embedding HTML in PHP

Posted 15 April 2010 - 01:11 AM

You should use whatever you feel comfortable with. I personally use the second method because i use an MVC structure. I like to separate my HTML from my PHP which also gives me colour coding in my editor.

Often my HTML is in a separate file so the PHP is the minority. But if you were using PHP primarily, just a little bit of echo'd HTML will probably be better. Like i said, choose for yourself, you don't have to stick to certain styles, just use whatever suits the situation.
Was This Post Helpful? 0
  • +
  • -

#6 LiDoNg_9_0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 159
  • Joined: 03-September 09

Re: Embedding HTML in PHP

Posted 19 April 2010 - 07:11 PM

you can do watever you like..
it's just the same.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1