Sunday, September 16, 2012

PHP Switch Statement Tutorial with Practical Example

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;
}

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:&nbsp;&nbsp;&nbsp;
<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>

PickMe
Delicious Save this on Delicious

29 comments :

  1. Η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

    ReplyDelete
  2. Hello! What a amazingly looking personal site you run! Did you build your blog by yourself?

    ReplyDelete
  3. 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 .

    ReplyDelete
  4. Nice blog...Very useful information is providing by ur blog..here is a way to find

    Php training institute in bangalore

    ReplyDelete
  5. Hi, Its really very informative and useful blog..Thanks for sharing this useful information about PHP Course.

    PHP Training

    ReplyDelete
  6. 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

    ReplyDelete
  7. 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.

    ReplyDelete
  8. your blog is very helpful to students....Click here to SeePhp Training in bangalore

    ReplyDelete
  9. 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

    ReplyDelete

  10. 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

    ReplyDelete
  11. 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

    ReplyDelete
  12. 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

    ReplyDelete
  13. 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

    ReplyDelete
  14. 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

    ReplyDelete
  15. 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

    ReplyDelete
  16. 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

    ReplyDelete
  17. 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

    ReplyDelete
  18. 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.

    ReplyDelete
  19. 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

    ReplyDelete
  20. 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.

    ReplyDelete
  21. 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

    ReplyDelete
  22. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
    Oracle Training In Chennai

    ReplyDelete
  23. 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

    ReplyDelete
  24. Great tutorial, I like it. Thanks for sharing.

    php training in chennai

    ReplyDelete
  25. It's a very useful post and your instruction is clear, Thanks for sharing this information.
    IT Job Training in Chennai

    ReplyDelete

If you have any Question , Suggestion or idea then please comment regarding the post.