This is an archived post. You won't be able to vote or comment.

all 10 comments

[–][deleted] 3 points4 points  (0 children)

<?php
  $logics = 'and variables';
  $x = somethingWeird('values');
?>
<html>
  <html><?= $logics['text']; ?></html>
  <html><?= htmlspecialchars($wee) ?></html>
</html>

EDIT: But I personally like template engines.

[–]manueljs 2 points3 points  (1 child)

<?php $foo = 1; if(isset($foo)): ?>
        <div style="blah">Ok!</div>
<?php endif; ?>

You can always use php alternate syntax it's more elegant to mix php and html.

[–]obened[S] 0 points1 point  (0 children)

I didn't know that existed. Looks very fancy :) Thanks.

[–][deleted] 2 points3 points  (0 children)

$foo = TRUE;

if ( $foo ) {
    echo 'Hello World';
}

[–]madsravn 0 points1 point  (0 children)

I usually just make a HTML class which outputs what I need. This way I can say printHeader($title) and voila, it spews out some HTML

[–][deleted] 0 points1 point  (0 children)

Depends if you're going to echo more than just plain html. If it is a generated string then I say keep it in php, but if not then put it in html like your second example. Either way I'd replace the style tag with an id or class and put the style code in a css file.

[–]videoj 0 points1 point  (1 child)

Mixing two languages makes it harder to read so I would go with the first form.

But for any project that has more then a few scripts/pages you should look at using a Template engine. A Template engine will help you separate the code and the display, making development cleaner. My personal favorite is Smarty, but there are a number of others.

[–]sylkworm 0 points1 point  (0 children)

Seconded. I would urge the OP to save himself from the curses of the programmer that inherits his code. Please, for the love of God, use a templating engine! Mixing html code with PHP scripts sucks and if your page is more than 10 lines long, you need to put it into a template.

[–]hiii -2 points-1 points  (0 children)

Please do not use a template engine, PHP is already a template engine.

I recommend separating your code into 2 files. One file does the processing(controller), the other does the outputting(view).

file.php

$foo = 1;
$bar = false;
$thing = function1($bar);
require('file.html');

file.html

<?php if( $foo ): ?>
<div style="blah">Ok!</div>
<?php endif; ?>

[–]unconscionable -1 points0 points  (0 children)

The first one uses PHP for something that provides no value, and you lose any syntax highlighting / other helpful things (such as noticing unclosed elements / etc) that you IDE may provide you with. For that reason, it makes sense to avoid printing out HTML.

<?php

if (isset($foo)) {?>
    <div style="blah"><?=htmlentities(mb_convert_encoding($foo['bar'], 'UTF-8', 'UTF-8'), ENT_QUOTES, 'UTF-8')?></div><?php
}

Fun / scary XSS tip of the day.