IF conditional in PHP

2

I know that as such, in HTML you can not use the conditional if, but with PHP you can integrate perfectly.

What I'm looking for is that instead of doing the conditional completely in PHP:

<?php
if (condicion) {
   echo "<p>hola</p>";
} else {
   echo "<p>adios</p>";
}
?>

Implement better PHP and that the HTML does not have to carry echo for each element.

<?php if (condition): ?>
    <p>Hola</p>;
<?php else if (condition): ?>   
    <p>Adios</p>;
<?php endif; ?>

From what I see, the part of the code <?php else if (condition): ?> does not exist, or is not written like that, but I can not find a way to do it, or do I have to do each conditional separately?

    
asked by JetLagFox 07.06.2017 в 11:22
source

2 answers

4

From the manual from PHP :

  

Note: Keep in mind that elseif and else if will be considered exactly the same only when keys are used as in the previous example. When using the colon to define if / elseif conditions, else if must not be separated in two words or PHP will fail with an interpreter error.

You want to say that the correct way to do it would be else if together:

<?php if (condition): ?>
    <p>Hola</p>;
<?php elseif (condition): ?>   
    <p>Adios</p>;
<?php endif; ?>
    
answered by 07.06.2017 / 11:39
source
2

Another thing you could do is assign the value of what you want to show in a variable, then show a single echo:

<?php
if (condition) {
   $resultado = "hola";
} elseif(condition) {
   $resultado = "adios";
}

echo "<p>".$resultado."</p>";
?>
    
answered by 07.06.2017 в 12:06