Jump to content


Photo

Your style of PHP programming


  • Please log in to reply
14 replies to this topic

#1 carlg

carlg
  • Members
  • PipPipPip
  • Advanced Member
  • 104 posts
  • LocationPA

Posted 20 June 2006 - 06:47 PM

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



#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 June 2006 - 07:26 PM

[!--quoteo(post=386118:date=Jun 20 2006, 02:47 PM:name=carlg)--][div class=\'quotetop\']QUOTE(carlg @ Jun 20 2006, 02:47 PM) View Post[/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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 20 June 2006 - 11:49 PM

[!--quoteo(post=386118:date=Jun 20 2006, 01:47 PM:name=carlg)--][div class=\'quotetop\']QUOTE(carlg @ Jun 20 2006, 01:47 PM) View Post[/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..)
"Syntax error" .. WHERE THE **** IS IT?!

#4 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 21 June 2006 - 02:26 AM

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

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 June 2006 - 03:55 AM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 Guest_edwinsweep_*

Guest_edwinsweep_*
  • Guests

Posted 21 June 2006 - 11:18 AM

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?

#7 carlg

carlg
  • Members
  • PipPipPip
  • Advanced Member
  • 104 posts
  • LocationPA

Posted 21 June 2006 - 11:39 AM

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>";

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 June 2006 - 11:44 AM

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.

#9 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 June 2006 - 11:58 AM

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#10 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 21 June 2006 - 03:05 PM

[!--quoteo(post=386350:date=Jun 21 2006, 07:39 AM:name=carlg)--][div class=\'quotetop\']QUOTE(carlg @ Jun 21 2006, 07:39 AM) View Post[/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>";
http://www.DaveLinger.com
dave at linger dot com

#11 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 21 June 2006 - 03:29 PM

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

#12 cwncool

cwncool
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 21 June 2006 - 03:29 PM

Method two normally. Ocasionally #1, but not often.

#13 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 21 June 2006 - 03:45 PM

[!--quoteo(post=386448:date=Jun 21 2006, 11:29 AM:name=complex05)--][div class=\'quotetop\']QUOTE(complex05 @ Jun 21 2006, 11:29 AM) View Post[/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\" /]
http://www.DaveLinger.com
dave at linger dot com

#14 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 21 June 2006 - 03:55 PM

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.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#15 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 21 June 2006 - 05:50 PM

I tend to use

echo <<<__HTML_END
__HTML_END;

a lot... I don't know why, but I feel more comfortable.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users