Jump to content

Your style of PHP programming


carlg

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
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.
Link to comment
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..)
Link to comment
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?
Link to comment
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>";
Link to comment
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.
Link to comment
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.
Link to comment
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>";
Link to comment
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
Link to comment
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\" /]
Link to comment
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.
Link to comment
Share on other sites

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.