Tag: switch

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: , ,