Switch Statements in PHP

Switch Statement in PHP

The switch statement is similar to a series of if statements on the same expression.

<?php

$i=0;

switch ($i)

{

case 0:

echo “$i equals 0″.”<br/>”;

break;

case 1:

echo “$i equals 1″.”<br/>”;

break;

case 2:

echo “$i equals 2″.”<br/>”;

break;

}

?>

Another Example:

<html>

<form method=”post”>

Enter first number<input type=”text” name=”f”/><br/>

Enter second number<input type=”text” name=”s”/><br/>

Enter your choice<input type=”text” name=”ch”/><br/>

<input type=”submit” value=”Calculate Now”/>

</form>

<?php

$f=$_POST[‘f’];

$s=$_POST[‘s’];

$choice=$_POST[‘ch’];

switch ($choice)

{

case “+”:

$sum=$f+$s;

echo “Sum=”.$sum;

break;

case “-“:

$sub=$f-$s;

echo “Subtraction=”.$sub;

break;

case “*”:

$mult=$f*$s;

echo “Multiplication=”.$mult;

break;

case “/”:

$div=$f/$s;

echo “Division=”.$div;

break;

default:

echo “Invalid choice”;

}

?>

</html>

for read complete php buy our 2nd edition PHP Mysql for advanced learning book online at

PHP MYSQL – FlipKart  Amazon  Google PlayStore   International 

Publisher : FSP Publication    ISBN : 978-1-61813-325-0 (6 May 2016)

Leave a Reply

Your email address will not be published.

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/03/24/php-training-institutes-gurgaon/
YouTube31
YouTube