sudeep
sudeep

Reputation: 170

Embedidng PHP code in HTML?

I am just wondering if its normal to embed PHP in the HTML, or is it considered dirty/unprofessional. For example consider following lines:

<? if($photo == 0) { ?>
    <div class ="reminder">Hey <?=$name;?>, You don't have any photo. </div>
<?  } else { ?>
    <div class ="ok">Do you want to change your photo? </div>
<? } ?>

Is this kind of code ok? How the similar work can be done in a clean/professional way (without PHP frameworks? ) Thanks.

Upvotes: 2

Views: 183

Answers (9)

bearcatFulton
bearcatFulton

Reputation: 81

From what I have read either will work, but most people just use the echo statements for small, conditional html.

Upvotes: 1

ravi
ravi

Reputation: 37

for that you can use smarty templete .... it improves your coding style and works fast ... visit smarty and try it, it's really awesome

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157828

there was an answer from guy named alexn
Dunno why did he delete it, it's looks best answer to me, so, I am only reproducing it:

I would say that's the way to go. You have a nice, clean separation of PHP and HTML.

Here's another option:

<? if($photo == 0): ?>
    <div class ="reminder">Hey <?=$name?>, You don't have any photo. </div>
<?  else: ?>
    <div class ="ok">Do you want to change your photo? </div>
<? endif ?>

Upvotes: 0

simonrjones
simonrjones

Reputation: 1182

It's OK to use PHP in templates but many people prefer to work with a templating language because it forces separation and ensures you don't litter your templatse with loads of PHP. If you do go down the template route Twig and Smarty are quite good since they compile the template into PHP which speeds things up.

If you're writing PHP in your templates try to follow some best practise coding standards to keep things neat. For example:

  • Use full <?php tags for compatibility.
  • When writing any loops instead of curly braces use colons. To end the statement you need to explicitly write it as endforeach/endif/endwhile/etc. This makes PHP templates more readable.
  • If you have a lot of logic move this into an external PHP file to keep the PHP in your template short and readable
  • If there is only one PHP statement in your PHP tag you don't need to end it with a semi-colon. Again, helps readability

An example:

<?php if ($photo == 0): ?>
   <div class ="reminder">Hey <?php echo $name ?>, You don't have any photo.</div>   
<?php else: ?>
    <div class ="ok">Do you want to change your photo?</div>
<?php endif ?>

See more at:

Upvotes: 1

strauberry
strauberry

Reputation: 4199

As long as you keep the logic of your program outside the html, it is ok. You need to mix it in your templates, for example. Template-engines like smarty replace the {$myVar} with < ? php echo $myVar; ? > (simply spoken), so it is not possible to avoid it completely. But things like

<?php

include "db.php";
connect_db();

// check login

echo "< html >< head><body>...";

?>

is NOT good practice, because you mix everything together: program logic (login-check, db-stuff) and output (echo html). Just have a look at the MVC and keep the "separation of concerns" in mind.

Your example looks ok because you have only that logic in your html which is needed to control the output directly. But calculating the value of $photo, depending on a db entry for example, would NOT be good in your html, because this is program logic. Output (HTML) and logic should be devided all the time.

Upvotes: 3

BugFinder
BugFinder

Reputation: 17858

To make it more readable, I tend to keep the brackets on separate lines, just so you can be sure to be able to find them all easily, but thats just me.

Upvotes: 0

Smandoli
Smandoli

Reputation: 7019

There will be many opinions about this. Mixing HTML and PHP can become very messy, but this looks fine. Many professionals work just this way. If it works for you, it's good.

Upvotes: 0

bradley.ayers
bradley.ayers

Reputation: 38372

It's very normal, whether it's good or not is a completely separate topic. It's really all about the size of your project and your requirements for maintainability/flexibility.

If it's a small project, just do what you have to in order to get it done. However a point exists at which it becomes unwieldy to develop without a framework. This is all very subjective and varies from project to project, but this is the general principle.

Upvotes: 1

Jan Dragsbaek
Jan Dragsbaek

Reputation: 8101

I try to keep this at a minimum, because i find it harder to debug it with all the < ? } ?> flowing around in the code (wordpress theme gurus does this alot)

Upvotes: 0

Related Questions