A switch statement is most commonly used you have too many possible outcomes/conditions for an if statement. Below is the basic structure of a switch statement:
Code :
This might appear confusing, but if you look at the working example below it will(hopefully) made more sense!
Code :
Output:Hello bob!
And if we changed the variable...
Code :
Output:Hello, I don't know your name!
Switch statements are quite simple, but extremely useful for evaluating a variable(often user input) and then taking a certain path through your script. In the next tutorial we will look at the $_GET function
To post comments you must sign in to your account below!
If you do not currently have an account click here to create one now!