Jump to content

Archived

This topic is now archived and is closed to further replies.

carlg

Your style of PHP programming

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
[!--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..)

Share this post


Link to post
Share on other sites
I just leave the html alone unless some parts require php coding,
so in my scripts there's more than 1 opening and closing php tag.
Yeah, I know it's messy :s

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest edwinsweep
9 out of 10 times Method NR1.
dont really know why.
and i used to work with single qoutes, untill i stumbled on a problem when using /n for new lines.
then i switched to double qoutes.
anybody else have a prefference for single or double qoutes?

Share this post


Link to post
Share on other sites
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>";

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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>";

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[!--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\" /]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.