Jump to content

Recommended Posts

If you're creating a page that is a lot of standard HTML mixed with PHP, which of the 2 methods below is the preferred method and why?

Method #1
Code the entire page inside PHP start/end tags
So you will have a lot of PHP statements that look like this
print "<TABLE>";
Everytime you want to do basic HTML, you need to use a print statement.

Method #2
Toggle back and forth between HTML and PHP thru the entire document.


I just curious which method is preferred. I've seen it done both ways.

Thanks for the info

Link to comment
https://forums.phpfreaks.com/topic/12481-your-style-of-php-programming/
Share on other sites

[!--quoteo(post=386118:date=Jun 20 2006, 02:47 PM:name=carlg)--][div class=\'quotetop\']QUOTE(carlg @ Jun 20 2006, 02:47 PM) [snapback]386118[/snapback][/div][div class=\'quotemain\'][!--quotec--]
If you're creating a page that is a lot of standard HTML mixed with PHP, which of the 2 methods below is the preferred method and why?

Method #1
Code the entire page inside PHP start/end tags
So you will have a lot of PHP statements that look like this
print "<TABLE>";
Everytime you want to do basic HTML, you need to use a print statement.

Method #2
Toggle back and forth between HTML and PHP thru the entire document.
I just curious which method is preferred. I've seen it done both ways.

Thanks for the info
[/quote]

it really depends on the amount of switching back and forth. i can't give you an overarching answer to that one... although, if there is a page with a lot of that, i usually try to make a template so that i don't have to worry about either one. then, you can completely separate the HTML from the PHP, and you can just plug in the PHP values where they need to go.
[!--quoteo(post=386118:date=Jun 20 2006, 01:47 PM:name=carlg)--][div class=\'quotetop\']QUOTE(carlg @ Jun 20 2006, 01:47 PM) [snapback]386118[/snapback][/div][div class=\'quotemain\'][!--quotec--]
If you're creating a page that is a lot of standard HTML mixed with PHP, which of the 2 methods below is the preferred method and why?

Method #1
Code the entire page inside PHP start/end tags
So you will have a lot of PHP statements that look like this
print "<TABLE>";
Everytime you want to do basic HTML, you need to use a print statement.

Method #2
Toggle back and forth between HTML and PHP thru the entire document.
I just curious which method is preferred. I've seen it done both ways.

Thanks for the info
[/quote]

I use method#1 unless the text isnt dynamic...then i have php at the top then end the php and have text...(cant remember exactly why i had php on that page..but..)
a agree with obsidian. it just depends on the script, for me. If the bulk of the page is gonna be html and maybe a few pieces of dynamic info, i'll toggle back and forth. I personally think it gets really messy and confusing popping back and forth between the two.
Thanks for all the input.

I kinda also lean towards #1, since I think it gets confusing closing and opening <? ?> tags all of the time.

So even if I'm doing a form or something like that and there's 30 lines in the form and 1 of them is dynamic, I still use method #1, so my PHP page just looks like a bunch of statements that look like this

print "<TABLE>";
print "<TR><TD>sdfsdfdsfdsfds";
print "<TD>sdf";
print "</TABLE>";
I adopt both methods it depends on what I'm doing.

But if I use the first method I dont echo/print line by line I do this instead:
echo "<table>
<tr>
<td>asjsdjdj></td>
</tr>
</table>

more html here";
Or I use HEREDOC if its large blocks of html.

its not work typeing echo or print for each line of html print/echo statements can span muiltple lines so which bother echo/print'ing each line one by one.
one other thought to consider is the load time of the page. sometimes it's worth it to run a timer and see which method is more optimal for the given page. to me, speed matters, so if i know that one method will load faster for my user than the other, i'd prefer to use that method.
[!--quoteo(post=386350:date=Jun 21 2006, 07:39 AM:name=carlg)--][div class=\'quotetop\']QUOTE(carlg @ Jun 21 2006, 07:39 AM) [snapback]386350[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Thanks for all the input.

I kinda also lean towards #1, since I think it gets confusing closing and opening <? ?> tags all of the time.

So even if I'm doing a form or something like that and there's 30 lines in the form and 1 of them is dynamic, I still use method #1, so my PHP page just looks like a bunch of statements that look like this

print "<TABLE>";
print "<TR><TD>sdfsdfdsfdsfds";
print "<TD>sdf";
print "</TABLE>";
[/quote]

you dont need "print" on every line.

print "<TABLE>
<TR><TD>sdfsddfsdfds</TD>
<TD>sdf</TD>
</TABLE>";
I always use PHP no matter what... if you find that most of the site will be in HTML, then do one of these

echo<<<endhtml
BULK OF HTML CODING HERE
endhtml;

that way if you need to print some variables in there, you don't need to toggle. And you don't have to worry about using quotes or anything, because that structure allows anything.

Hope this helps!

Barry
[!--quoteo(post=386448:date=Jun 21 2006, 11:29 AM:name=complex05)--][div class=\'quotetop\']QUOTE(complex05 @ Jun 21 2006, 11:29 AM) [snapback]386448[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I always use PHP no matter what... if you find that most of the site will be in HTML, then do one of these

echo<<<endhtml
BULK OF HTML CODING HERE
endhtml;

that way if you need to print some variables in there, you don't need to toggle. And you don't have to worry about using quotes or anything, because that structure allows anything.

Hope this helps!

Barry
[/quote]

anything? what if I need to print "endhtml;" in my html? [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
I strongly suggest using Smarty for this problem, [a href=\"http://smarty.php.net/\" target=\"_blank\"]http://smarty.php.net/[/a], it is really easy to use and install. The only trick to installion is that your permissions are right. As for use it happily sidesteps the problem you've outlined above by letting you put all of your html in separate template files. This means that you can concentrate on the the business logic in classes that are doing work moving objects/data around and leave the visual logic to the template. As an added bonus you can cache your compiled templates, which means that you won't have to remake the page every time someone wants to load it; this translates into faster load times. Finally, Smarty has a ton of plug-ins, and it is as easy as creating a function to add a custom one, that let you do things like strip slashes, format date, etc. I strongly recommend something like Smarty if you want to keep your code separated by function.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.