PHP Alternative Control Structure


 

PHP Alternative Control Structure

In this tutorial we will study about alternative control structure in PHP. How to write alternative control structure is discussed in this tutorial. Examples in this tutorial will make it more clear.

In this tutorial we will study about alternative control structure in PHP. How to write alternative control structure is discussed in this tutorial. Examples in this tutorial will make it more clear.

Alternative Control Structure:

PHP provides alternative ways to use control structures like if, else, elseif, for, while, foreach etc. In each control structure we can replace the opening brace with a colon(:) and the closing brace with endif; endfor, endwhile, endforeach, respectively.

We can put any HTML coding within any control structure, the examples are given below:

elseif and else if will be considered identical only if these are used with curly braces otherwise if we want keep else and if must not be separated into two words, PHP will fail to recognize and would produce an error.

Example:

<?php

$a=133;

$b=44;

if($a>$b):

echo "\$a is greater";

else: echo "\$b is greater";

endif;

?>

Output:

$a is greater

Example:

<?php

$a=133;

$b=44;

if($a>$b):

?>

<b>A is greater</b>

<?

else:

?>

<i>B is greater</i>

<?php

endif;

?>

Output:

0

A is greater

Ads