Home Tutorial Php Phpbasics Tutorial PHP Alternative Control Structure

 
 

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

Advertisement

Related Tags for PHP Alternative Control Structure:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 25, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com