I need to build a PHP if/else statement. What I don’t know how to do is addHTML in the middle of it… Specifically, where the <?php and ?> tags would go…
If you’re going to stick loads of html in there, then you’d want to pop out of PHP and back in again (or find a better way to generate the page without jumping in and out of PHP every couple of lines), but IMO for something this small, it’s not worth it.
Oddly, I find printf harder to read. I suspect it has something to do with a combination of language and background, though.
Echoing is a straight left-to-right read; printf is back-and-forth as you figure out what each of the parameters does. shrug
No, you’re not missing anything, typo on my part, sorry for the confusion. Still, it does illustrate the point I was making - just put the html in as quoted strings where you need it. Or, as @rpkamp suggested, use printf if you prefer.
To me, the choice is down to how much will be output - for small amounts, it makes the code very difficult to read if it’s jumping in and out of PHP every couple of lines. Ideally you would separate the two completely, or at least as much as possible.
if you are writing this in pure php without the use of a framework. I would use Smarty. It will allow you to separate the php from the design elements but will also allow conditionals and loops for use with html.
Maybe I am not understanding fully but it seems to me reading the question, what you want is to include actual html in an if/else statement. Then you can do this simply by including the php code inside the php tags <?php and ?> and placing the html outside the php tags ie between the end of the last tag ?> and before the next ?<php - like this -
<?php
$status=1;
if ($status == 1) {
?>
<strong>hello world the status is 1</strong><br>
<?php
}
else {
?>
<h1>hey there - the status is NOT 1</h1>
<?php
}
?>
in this way you do not have to worry about echo or prinf or single or double quotes, you just use php and html.
In this way php does the processing / decision and you just have to write pure html
Fair comment, but I have to admit I get frustrated with complex multiple echoes that contain multiple single and double quotes required for some HTML. I then find this code complicated to debug or edit so I take the easy route and just write the HTML outside the php tags