Alternative If and Else to combine PHP and HTML more simply

In this tutorial we are going to see an alternative form of representation to the common IF and ELSE that we usually use in php. Specifically, it is very useful when we make our templates by combining php and html.

Content

  • Alternate if and else structure
  • If and ElSE alternative with html
  • if and else comparison
  • Extra: Data to take into account of alternative IF and ELSE
  • Extra: Other alternative structures
    • SWITCH alternative representation
    • Alternative representation WHILE

Alternate if and else structure

The alternative representation of if and else is based on replacing the opening braces with a colon “:” and the closing brace with “endif;”. Let’s see a graphic example:

<?php
if ( $a < 5 ):
    echo 'Hola mundo';
endif;
?>

To this representation we can also add the sentences else and else if:

<?php
if ( $a < 5 ):
   echo 'A es menor que 5';
elseif ( $a == 5 ):
   echo 'A es 5!!';
else:
   echo 'Por desgracia A es mayor que 5!';
endif;
?>

As you can see, its representation is simple and, although it doesn’t follow the control structure representation standard, we can find advantages for certain uses where the most well-known representation can be confusing.

If and ElSE alternative with html

The use that I like the most for the use of this alternative IF and ELSE is none other than the development of our PHP templates. A code is better than a thousand words:

<h2>
<?php if ( $action == 'profesional' ): ?>
    Bienvenido a la mejor <strong>web PROFESIONAL de programación</strong> del mundo
<?php elseif ( $action == 'amateur' ): ?>
   Bienveido querido amateur a la <strong>mejor web creada para programadores Amateur del mundo</strong>
<?php else: ?>
   ¡Seas quien seas! <strong>¡Bienvenido a la mejor web del mundo para aprender programación!</strong>
<?php endif; ?>
#lg;/h2> 

If you’ve been doing this for some time (if I don’t confirm it) you will have noticed that a closing brace in a template with html sometimes becomes almost invisible, especially in templates with complex html. As you have seen, this representation is much more readable than the common one, at least as far as combining it with HTML is concerned.

Regarding the programming of scripts entirely written in php Yes, I have found that for students or new people in the world to see an endif; it is much more understandable than a simple key. Especially if we nest these control structures over and over again.

scripts in php with the alternative representation they are easier to understand by novice programmers or students.

if and else comparison

Comparing both representations we find few differences with respect to what I have already shown, on the other hand I would like to leave an extra example for greater clarity.

php script to get the largest of 3 numbers with common if and else:

<?php
if ( $a > $b ) {
    if ( $a > $c ) {
       echo 'El mayor es a';
    } else {
       echo 'El mayor es c';
    }
} else {
    if ( $b > $c ) {
       echo 'El mayor es b';
    } else {
       echo 'El mayor es c';
    }
}
?>

Equivalent php script with if and else alternatives:

<?php
if ( $a > $b ):
    if ( $a > $c ):
       echo 'El mayor es a';
    else:
       echo 'El mayor es c';
    endif;
else:
    if ( $b > $c ):
       echo 'El mayor es b';
    else:
       echo 'El mayor es c';
    endif;
endif;
?>

Extra: Data to take into account of alternative IF and ELSE

In summary, the use of if and endif is more attractive for the following reasons:

  • Whether we want to contemplate one line or several within the condition of the if we will always use the same representation.
  • Combining PHP and HTML makes it more readable and much easier to identify.
  • Searching in a template for the closure of an if will be really easy thanks to endif;
  • It avoids confusion problems for new (and not so new) programmers when we nest different control structures. For example a large number of IF, WHILE, FOR, etc.
  • One-line representations like if($a < $b): echo 'Hello'; endif; they become easier.

Extra: Other alternative structures

As additional data to say that there are other control structures with an alternative representation very similar to this IF and ELSE. These include SWITCH, WHILE, FOR, etc.

SWITCH alternative representation

<?php
switch ( $a ):
   case 1:
        echo 'a es 1';
        break;
endswitch;
<?php

Important not to print any white space between the switch statement and the first case.

Alternative representation WHILE

<?php
$a = 1;
while ( $a < 5 ):
   $a++;
endwhile;

Leave a Reply