Condiciones y ruptura de código PHP
Supongamos que tenemos una variable:
<?php
$test = true;
?>
Mostremos un fragmento de código HTML
si nuestra variable es igual a true:
<?php
if ($test) {
echo '<p>text</p>';
}
?>
Podemos modificar este código al siguiente, con ruptura de etiquetas PHP:
<?php if ($test) { ?>
<p>text</p>
<?php } ?>
Esto se puede simplificar aún más si utilizamos
la sintaxis alternativa if:
<?php if ($test): ?>
<p>text</p>
<?php endif; ?>
Dada la variable:
<?php
$show = true;
?>
Dado el código:
<div>
<p>text1</p>
<p>text2</p>
<p>text3</p>
</div>
Muestra el código HTML proporcionado si la variable
show es igual a true.