Can HTML Be Embedded Inside PHP "If" Statement

If statement inside php content, containing html code

Here's a few ways you can do it:

1: Use a Ternary Operator

<?php
$content = "

<table style='text-align: center;'>
<tr>
<td style='text-align: center;" . ($result->name ? " display: none;" : "") . "'>
{$result->name}
</td>
</tr>
</table>
";



2: Assign the CSS to a variable and interpolate it

<?php
$css = $result->name ? " display: none;" : "";
$content = "

<table style='text-align: center;'>
<tr>
<td style='text-align: center;{$css}'>
{$result->name}
</td>
</tr>
</table>
";



3: Break the $content variable assignment into bits so you can use an if() condition

<?php
$content = "

<table style='text-align: center;'>
<tr>
<td style='text-align: center;";

if($result->name) $content .= " display: none;";

$content .= "'>
{$result->name}
</td>
</tr>
</table>
";


4: Use PHP's template style syntax

<table style='text-align: center;'>
<tr>
<td style='text-align: center;<?php if($result->name): ?> display: none;<?php endif; ?>'>
<?= $result->name; ?>
</td>
</tr>
</table>

This is my preferred option if you're working in a template (a .phtml file for example).



5: ... or mix it up a bit (template style with a ternary echo)

<table style='text-align: center;'>
<tr>
<td style='text-align: center;<?= $result->name ? " display: none;" : ""; ?>'>
<?= $result->name; ?>
</td>
</tr>
</table>



Ultimately it comes down to which you find most readable and are most comfortable with.

PHP if statement inside HTML

I found the issue.

You are missing the php from <? on the elseif line. It should be <?php unless you have short tags enabled, which I'm guessing you don't.

<?php if($details['status'] == 0): ?>
This will show if the expression is true.
<? elseif ($details['status'] == 1): ?>
Otherwise this will show.
<?php endif; ?>

Should be:

<?php if($details['status'] == 0): ?>
This will show if the expression is true.
<?php elseif ($details['status'] == 1): ?>
Otherwise this will show.
<?php endif; ?>

Can HTML be embedded inside PHP if...elseif...else statement when HTML code contains PHP?

don't forget to close your foreach like this

<?php foreach ($students as $student) : ?>
<tr>
<td><?php echo $student['studentID']; ?></td>
<td><?php echo $student['studentPassword']; ?></td>
</tr>
<?php endforeach;?>

Can HTML be embedded inside normal (i.e. not alternative) PHP if?

Yes, the standard syntax in PHP works.

As suggested by the term standard I used in my last sentence, I really meant that it is the standard syntax. It always worked. It will always work.

Consider the alternative syntax alternative, that is, non-standard. The wording is from the PHP manual, not from me personally.

Alterantive already suggests that it works exactly the same, so you can use any of those, despite the votes on some Q&A platform somewhere in the world wide web :)


<?php if ($condition) { ?>
<a href="http://yahoo.com">This will only display if $condition is true</a>
<?php } ?>

By request, here's elseif and else (which you can also find in the docs: elseif; else)

<?php if ($condition) { ?>
<a href="http://yahoo.com">This will only display if $condition is true</a>
<?php } elseif($anotherCondition) { ?>
more html
<?php } else { ?>
even more html
<?php } ?>

It's that simple.

The HTML will only be displayed if the condition is satisfied.


I hope this exemplary reqplique does add enough clarity if there were any doubts.

Embedded html php syntax error while nesting if-else

I got your error:

Replace this:

<?php if (isset($_POST['uname']) && isset($POST_['psw'])) : ?>  // You are writing $POST_ but not $_POST

To:

<?php if (isset($_POST['uname']) && isset($_POST['psw'])) : ?>


Related Topics



Leave a reply



Submit