What is PHP Switch Statement?
According to php.net The Switch Statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.
PHP Switch Statement Syntex
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
PHP Switch Statement Practical Example
Objective : Creating a program to enter two number and select task from list then display the result.
Coding
<?php
$n1=$_POST[‘t1’];
$n2=$_POST[‘t2’];
$ch=$_POST[‘choice’];
if(isset($_POST[‘done’]))
{
switch($ch)
{
case ‘add’:
$res=$n1+$n2;
break;
case ‘sub’:
$res=$n1-$n2;
break;
case ‘mul’:
$res=$n1*$n2;
break;
case ‘div’:
$res=$n1/$n2;
break;
}
}
?>
<html><head><title>Form</title></head>
<body>
<form action=”” method=”post”>
Enter First Number<input type=”text” name=”t1″><br>
Enter Second Number<input type=”text” name=”t2″><br>
Result<input type=”text” value=”<?php echo $res;?>”><br>
Select Task:
<input type=”radio” name=”choice” value=”add”>ADD
<input type=”radio” name=”choice” value=”sub”>SUB
<input type=”radio” name=”choice” value=”mul”>MUL
<input type=”radio” name=”choice” value=”div”>DIV<br>
<input type=”submit” name=”done” value=”DONE”>
</form>
</body>
</html>
<?php
$n1=$_POST[‘t1’];
$n2=$_POST[‘t2’];
$ch=$_POST[‘choice’];
if(isset($_POST[‘done’]))
{
switch($ch)
{
case ‘add’:
$res=$n1+$n2;
break;
case ‘sub’:
$res=$n1-$n2;
break;
case ‘mul’:
$res=$n1*$n2;
break;
case ‘div’:
$res=$n1/$n2;
break;
}
}
?>
<html><head><title>Form</title></head>
<body>
<form action=”” method=”post”>
Enter First Number<input type=”text” name=”t1″><br>
Enter Second Number<input type=”text” name=”t2″><br>
Result<input type=”text” value=”<?php echo $res;?>”><br>
Select Task:
<input type=”radio” name=”choice” value=”add”>ADD
<input type=”radio” name=”choice” value=”sub”>SUB
<input type=”radio” name=”choice” value=”mul”>MUL
<input type=”radio” name=”choice” value=”div”>DIV<br>
<input type=”submit” name=”done” value=”DONE”>
</form>
</body>
</html>
Read latest post PHP nested if else statement tutorial with more working example
Ηi, i think that i saw уou viѕitеd my sіte thus i came
to “гetuгn the favor”.I'm attempting to find things to improve my web site!I suppose its ok to use some of your ideas!!
my blog post website advertising
Also visit my web-site :: Submit your website
Hello! What a amazingly looking personal site you run! Did you build your blog by yourself?
yes
NICE BLOG!!! Thanks for sharing useful information about FNT Softwre Solutions and being one of best Software Training institute in Bangalore we agree that this blog is very useful for the students are Searching for best software courses, I would really like to come back again right here for like wise good articles or blog posts. Thanks for sharing…PHP Training Bangalore .
Nice blog…Very useful information is providing by ur blog..here is a way to find
Php training institute in bangalore
Hi, Its really very informative and useful blog..Thanks for sharing this useful information about PHP Course.
PHP Training
Thanks for updating new information about web designing. Practically, you have shared lot of details about website designing. Also share your new updated details about website creation. Thanks mate.
Web Designing training in Chennai
Tutorial about web creations. Practically, website designing, web development and Seo tutorial have discussed in this blog. Gradually, it is helpful when we are going to website creation.
your blog is very helpful to students….Click here to SeePhp Training in bangalore
Thanks for sharing these niche piece of coding to our knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…
Web design training in Chennai
Thanks for sharing this valuable post to my knowledge great pleasure to be here sas training in Chennai has great scope in IT industry. It’s an application suite that can change, manage & retrieve data from the variety of origin & perform statistical analytic on it, if anyone looking for best sas training in Chennai get into FITA…
sas course in Chennai | sas institutes in Chennai
Thanks for splitting your comprehension with us. PHP- It’s an open source server-side scripting language particularly designed for web development but even it’s used as a universal programming language. Make our website as user friendly and responsive, it helps you to get more visibility &familiarity in online.
PHP Training Chennai
Aticle gives a great information about SEM.Now i ready to face any problems in SEM. Thanks for giving worthful information.
css3 Training in chennai | css3 Training chennai | css3 course in chennai | css3 course chennai
Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing. html5 Training in chennai | html5 Training chennai | html5 course in chennai | html5 course chennai
Your article is very useful for me. Thanks for sharing the wonderful information. PHP Training in chennai | PHP Training chennai | PHP course in chennai | PHP course chennai
Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post. PHP Training in chennai | PHP Training chennai | PHP course in chennai | PHP course chennai
This is extremely helpful info!! Very good work. Everything is very interesting to learn and easy to understood. Thank you for giving information. CSS3 Training in chennai | CSS3 Training chennai | CSS3 course in chennai | CSS3 course chennai
This is extremely helpful info!! Very good work. Everything is very interesting to learn and easy to understood. Thank you for giving information. PHP Training in chennai | PHP Training chennai | PHP course in chennai | PHP course chennai
Your blog is really awesome. Thank you for your sharing this informative blog. Recently I did HTML5 Training in Chennai at FITA academy, Its really useful for me. Suppose if anyone interested to learn real time PHP Training in Chennai reach FITA.
I have read your blog, it was good to read & I am getting some useful info's through your blog keep sharing…
Regards,
PHP Course in Chennai
Hi, Thanks for sharing this valuable blog.I was really impressed by reading this blog. I did HTML Training in Chennai at reputed HTML5 Training Institutes in Chennai. This is really useful for me to make a bright future in designing field.
Your post about web design and development is really interesting. When comes to content management system, wordpress is widely used tool. It loaded with various stunning features that make website creation lot simpler. WordPress Training in Chennai | WordPress Course in Chennai | WordPress Training Institutes in Chennai
Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
Oracle Training In Chennai
Great post you could have completed the following. I'm truly happy to learn to read this kind of. This can be a very informative subject matter that you're picked. keep writing.Support Page
You shared useful post. Thanks for sharing.
digital marketing training courses in chennai
Great tutorial, I like it. Thanks for sharing.
php training in chennai
I have to voice my passion for your kindness giving support to those
people that should have guidance on this important matter.
Your Blog is really awesome with useful and helpful content for us.Thanks for sharing ..keep updating more information.
GOOD ONE
Very good to read thanks for posting
Your blog is really awesome. Thank you for your sharing this informative blog.
Develop your IT skills with Linux classes in Chennai. Enhance your job opportunities by receiving professional training to master Linux systems.