Quantcast

Maximum PC

It is currently Thu Aug 21, 2014 2:16 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: coding PHP and HTML together into one PHP file
PostPosted: Sun May 22, 2011 7:21 pm 
Willamette
Willamette

Joined: Tue Feb 01, 2005 12:17 pm
Posts: 1479
I may have asked this a long while back, but need to ask it a gain because I never really saw the result of my question....

Anyway, is there any standard regarding mixing HTML and PHP code in the same PHP file? As in, instead of using ECHO statements to write the HTML code out, is it "safe" to just close out the PHP block, use HTML and then open another PHP code block?

Example:
<?php
//SomePHP
Code ?>

<p>This is a sentence</p>

<?php
//MorePHPCode
?>

--or--

<?php
//PHPCode
echo "<p>This is a sentence</p>";
//MorePHP stuff
?>

Is one way safer than another? I know that PHP just ignores non-php commands.


Top
  Profile  
 
 Post subject: Re: coding PHP and HTML together into one PHP file
PostPosted: Mon May 23, 2011 2:49 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
That is probably the most common way to do it actually.


Top
  Profile  
 
 Post subject: Re: coding PHP and HTML together into one PHP file
PostPosted: Mon May 23, 2011 5:36 pm 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
The first way you mentioned is fine. I might be wrong, but I think you can even have a '?>' mid block and switch to HTML.


Top
  Profile  
 
 Post subject: Re: coding PHP and HTML together into one PHP file
PostPosted: Tue May 24, 2011 8:50 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Go with one that is easiest to debug, that is what I say.

This generally works better:
Code:
<? if (isAdmin() == true) { ?>
   <div> This is the admin bar </div>
<? } ?>


While this is an oversimplistic example, it's way more readable, and can be debugged easier since you're no longer trying to trace echo outputs.


Top
  Profile  
 
 Post subject: Re: coding PHP and HTML together into one PHP file
PostPosted: Tue May 24, 2011 9:15 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Not to mention performance is probably better...


Top
  Profile  
 
 Post subject: Re: coding PHP and HTML together into one PHP file
PostPosted: Mon May 30, 2011 6:22 pm 
Willamette
Willamette

Joined: Tue Feb 01, 2005 12:17 pm
Posts: 1479
Alright. Like I said, I'm not into what is the standard and what's not. Obviously, I find that just embedding the <? and ?> tags where I need them is far easier, and just let the PHP processor dump everything else out to the web page...

A long while back, though, I did write a fairly complex page using the first example I gave (where PHP would write all HTML via ECHO commands) and that was a PITA to debug.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group