Home Tutorial Php Phpbasics Tutorial PHP Alternative Control Structure

 
 

PHP Alternative Control Structure
Posted on: March 25, 2010 at 12:00 AM
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:

A is greater

Related Tags for PHP Alternative Control Structure:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.