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,
separating html from code in php
Moderator: General Moderators
-
adamSpline
- Forum Newbie
- Posts: 11
- Joined: Fri Sep 16, 2011 1:30 pm
Re: separating html from code in php
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.
Re: separating html from code in php
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
-Greg
-
implications
- Forum Commoner
- Posts: 25
- Joined: Thu Apr 07, 2011 3:59 am
Re: separating html from code in php
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.