Home
Username:
Password:
FileMaker 9 & PHP Foundations Tutorials

PHP Flow Control / Selection with "SWITCH"

Subtitles of the Movie

The other selection statement PHP is the switch statement. It might be a little bit more difficult to explain than the if statement, but I'm going to use an example that's very similar in FileMaker and we'll start from there and then go on and execute the switch statement in PHP. In FileMaker, I'm going to open up the script maker and create a sample script, which we'll discard when we're done. And I'm going to use a set variable just so that we can get into the calculation dialog. Here I'm going to use the case function. The case function is very similar in how it compares multiple different tests and provides different results for each one of the tests to how the switch statement uses multiple different tests and different results. Example, we'll say case the variable animal is equal to cat, then output meow. Case animal is equal to dog, then output bark. And one last time, case animal is equal to frog; we're going to need some quotes there, output ribbit. Else, we'll just output growl. This is how you use the case statement in FileMaker. You have a test and then a result based on that test. And then you have the secondary test and a result based on that test. And you continue on with result and test and result and test and if none of the tests have a result of matching value, then you can have a default value as a result. This is exactly how the switch statement works in PHP. I'm going to open up my code editor to the 0302.php example file. And here I'll use the same type of example as we did in FileMaker. I'll say that animal is going to be equal to dog. Then I'll use my switch statement. Switch, and I want it to switch on the value of animal, in other words, that's what we're going to be testing against, and then I have my open curly brace and then my closing curly brace where I can specify all the different switch conditions for tests. First I'm going to say case, similar to FileMaker, use the case word, cat colon and now I can specify the output to occur, or the result to occur if animal is equal to cat. I could say echo meow. Case dog colon echo bark. Case frog, then echo ribbit and then default colon echo growl. This is a complete switch statement. It's very similar to what we did in FileMaker Pro. In FileMaker, we actually stated the same variable inside of each test, whereas in PHP, we just specify the one variable inside the call to the switch statement. And then we specified the result by using the case key word. In this case, we're going to output bark because dog is going to match the variable animal. Let's show this in the browser and you'll see something interesting that you're not expecting. You can see that PHP did output bark, but then it also outputted everything after that, whether it matched the condition or not. This is one of the nuances of using the switch statement. Once a match is found, PHP is going to continue on and it's going to output everything after the first matching case statement, including the default statement. To avoid this from happening, you must conclude each condition statement with the break key word. The break key word tells PHP to stop using the switch statement or to exit out of the switch statement. Now if I hit refresh in my browser, you can see that we echoed bark, but then we broke out of the switch statement. You can use more complex conditions. For example, instead of using cat for case, you could use some sort of function. Cat or dog, a complex condition. If I refresh the browser, you can see that we matched because of the or condition and then we broke out so we never got the bark back. So you can use complex conditions, you can use functions, um, different variables, data from your FileMaker Pro database, of course. Um, and then in your results, you can also use different functions and complex conditions and data from your FileMaker database. So this is a very simple example of the switch statement but as you can see, if you have a complex problem or solution, the switch statement is a very powerful and can be used to solve a lot of different output and conditional output problems. So this is the second way to control the logical flow of your application, aside from the if selection statement. The switch selection statement does take some time getting used to, but its well worth your while to learn how to master it. This concludes our lesson on using the switch selection statement.

Tutorial Information

Course: FileMaker 9 & PHP Foundations
Author: Lance Hallberg
SKU: 33786
ISBN: 1-933736-99-2
Release Date: 2007-08-22
Duration: 9.5 hrs / 107 lessons
Work Files: Yes
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available