separating html from code in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adamSpline
Forum Newbie
Posts: 11
Joined: Fri Sep 16, 2011 1:30 pm

separating html from code in php

Post by adamSpline »

Hi all,

I have a quick question. I come from the Java world where it is stressed (without end) the importance of separating code from html (using controller servlets that generate objects, that are passed to jsps, that use a tag library or such within the html... or some similar variation).

In general, the rule is that within JSPs, one should not put scirplets (java code) intermingled with html.

I am now exploring the PHP world, and it seems to me that PHP is pretty much written like JSP with scriplets. Browsing the major tutorials on PHP, it appears that it is pretty common practice to have html with intermingled PHP code. My question is, is this standard practice within PHP?

Thanks,
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: separating html from code in php

Post by Celauran »

Common practice and best practice aren't necessarily the same thing. It's fine for learning, though. Once you're comfortable with the basics of PHP, I'd encourage you to take a look at one of the many OO MVC frameworks out there.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: separating html from code in php

Post by twinedev »

Or at the very least, what I do with simple scripts (ie, just a contact form), all processing is done at the top, then once you have the opening HTML tag, the only PHP used is echoing out values, looping through arrays and/or simple if statements to determine parts to display.

-Greg
implications
Forum Commoner
Posts: 25
Joined: Thu Apr 07, 2011 3:59 am

Re: separating html from code in php

Post by implications »

It doesn't necessarily have to be intermingled but there isn't anything glaringly 'wrong' about using PHP alongside HTML. With forms, it is common practice to deal with the PHP first and then write all the HTML complementing the PHP at the bottom, but with other types of things, you might echo out some HTML after IF statements.
Post Reply