Friday, 28 August 2009

Php Tutorial Part 6

PHP switch statement



Another useful php statement that is used to execute different actions based on different conditions.

Syntax:
switch (variable)
{
case conditon1:
//code to be executed
break;
case condition2:
//code to be executed
break;
default:
//code to be executed
}

Example:
<html>
<body>

<?php

$variable1 = 2;
switch ($variable1)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>

</body>
</html>

switch statement evaluates one time the variable, the value then is compared each case, if it match the block of code is executed if not the switch statement executes the default code if the programmer set it.

No comments:

Post a Comment

Reset numbering for Screenshots Win+PrtSc

I was using today the screen shot functionality of Windows 8.1 Win Key + PrtSc How to reset the numbering?