|
PHP Tutorial Home
Basic PHPPHP IntroductionPHP Installation PHP Syntax PHP Variables PHP Form Variables PHP If Statements PHP Switch Statements PHP Arrays PHP While Loops PHP For Loops PHP Operators PHP Functions Advanced PHPPHP IncludePHP Upload File PHP File PHP Mail PHP Mail Configuration PHP Database Driven Website PHP Summary FREE Hosting!With every domain name you register with ZappyHost, you get FREE hosting.$1.99 Domain NamesWith every new non-domain purchase thru ZappyHost, you get a domain name for only $1.99. |
PHP Switch StatementsIn the previous lesson we used a PHP if... elseif statement in order to execute different block of code for each different condition. As mentioned, we could use as many "elseif"s as we like. If you have many conditions, PHP switch statements are a more efficient way of doing this. The server will execute a PHP switch statement quicker than multiple "elseif"s. Also, there's actually less code for the programmer to write. To write a PHP switch statement, you start with the switch keyword followed by the expression to evaluate (for example, a variable name). You then follow that with a case for each condition (based on the expression). Syntax
ExampleIn the following example, we declare a variable called $favoriteVege and assign a value to it. We then execute a switch statement in order to display a different string depending on the value of the variable. Don't forget to end each case with a break;. This is required, otherwise all cases would be displayed.
The above code results in the following:
Sorry, don't know how much energy that vegetable contains.
Enjoy this website?
Oh, and thank you for supporting Quackit! |
Need Content for your Website?Get unique, quality digital content for your website. You can even earn money by reselling it!Includes:
|