HTML in the middle of PHP if/else?

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…

The snippet I am working with is

    <?php if ( has_post_thumbnail($some_post['ID']) ) {
					echo get_the_post_thumbnail($some_post['ID']);
					}
						else  {
						 echo get_stylesheet_directory_uri() . '/assets/img/hero-thumbnail-default.jpg';  
        } ?>

I need to add img tags around

echo get_stylesheet_directory_uri() . ‘/assets/img/hero-thumbnail-default.jpg’;

So that that piece is echoed inside an img tag… I can’t seem to figure out how to add the HTML portion (the img tag) inside the if/else statement.

Just stick it in as a quoted string:

echo '<img>' . get_stylesheet_directory_uri() . '/assets/img/hero-thumbnail-default.jpg</img>';

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.

Maybe I am missing something, but that still just kicks out the URL, instead of adding it at as src="http:/…/assets/img/…

My preference would be printf:

printf('<img src="%s" />', get_stylesheet_directory_uri() . '/assets/img/hero-thumbnail-default.jpg');

but you can achieve the same with plain ol’ echo:

echo '<img src="' . get_stylesheet_directory_uri() . '/assets/img/hero-thumbnail-default.jpg" />';

I just find that incredibly hard to read, especially with all the different single and double quotes in there.

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.

Everything you echo or print will be interpreted by the browser as HTML code.

If you want to have html code separated from PHP, you can close the php tag and open it again after you finished writing the html ode.

For example:

<?php
if(condition) {
?>
<div>
<?php
 echo $somevar;
?>
</div>
<?php 
}
?>

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

But, you’re jumping in and out of PHP all the time, which can be a pain to keep track of. It depends on the complexity of the code, IMO.

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

I suspect there is no “correct” answer, and it will be down to the individual coder and the individual situation.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.