PHP Switch Function

The function switch() in PHP is used to execute different codes based on a variable’s value. This is used in place of the IF/ELSE IF statements. A default value is optional and, if specified, is used when no other option is matched. You must include a break; after each case or the following cases will all return true.

[code lang=”php”]

[/code]

You can use this with forms and such to determine an action based on a submitted value as well! Here is an example using the switch() function in a form:

[code lang=”html”]

Select your gender:

[/code]

Now on our PHP side of the script (submit.php) we will use the switch() function to evaluate the value.

[code lang=”php”]

[/code]

Filed under: PHP, Web ProgrammingTagged with: , ,

13 Comments

  1. I was just having a conversation over this I am glad I came across this it cleared some of the questions I had.

  2. I’ve just started off a blog, the knowledge you give on this site has aided me extremely. Thank you for all your time & work.

  3. Very good article. Coherent and informative just like a good article should be. I’ll definitively be back for more.

  4. The great summary assited me a lot! Saved the blog, extremely interesting categories everywhere that I read here! I really like the info, thank you.

  5. A good friend of mine visits your blog quite often and recommended it to me to read too. The writing style is solid and the content is pertinent. Many thanks for the insight you provide the readers!

  6. This nice summary encouraged me very much! Bookmarked the blog, extremely excellent topics just about everywhere that I see here! I appreciate the info, thank you.


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *
Name *
Email *
Website