PHP Flow Control / Selection with "IF" pt. 1
Subtitles of the Movie
Now we're going to talk about controlling the flow of logic in PHP. We'll start off by talking about the selection statements. There are two selection statements in PHP. The if statement and the switch statement. This lesson is going to focusing on the if statement. PHP's if statement is a lot like using the if script step in FileMaker Script Maker. Here we can specify whether or not a condition is going to be true or false and if it's true, you can specify one or more script steps to execute. If it's false, we can use the else script step to specify one or more script steps to execute on that basis. If the resulting condition is false, we can also evaluate further conditions by using else if. And we can continue to evaluate further conditions until we're satisfied. If you understand how to use FileMaker's if script steps and how to control the flow of logic in this application, then you'll be able to catch on very fast to using the if selection statement in PHP. The two are almost completely identical. Let's look at how the if selection statement is used in PHP. Open up the 0301.php example file in your code editor. Here I'm going to start off by creating the variable X and saying it's equal to 20. Now I'll create my first if selection statement. I'll say if X is equal to 20, then perform or execute some PHP code. I start my PHP code that I want to execute by creating a block of code in open and closing curly braces. The open and closing curly braces signify what code to execute if the condition is true and what code to skip if the condition is false. I'm going to echo out the value of X is equal to, and then the actual variable. Because we're using double-quoted strings, PHP will use variable substitution. Let's view this in the browser to see our results. The value of X is equal to 20. Notice that I used two equal signs to compare my two values. In PHP, two equal signs is always the comparative value, whereas one equal sign is always an assignment value. So, if I were to only use one equals sign, I would actually be assigning the value of 20 to the variable X all over again. I'll say this and refresh it and you can see that it looks like it works, until you change the value that you're supposed to be comparing against. X is equal to 20 and my condition says if X is equal to 120, then output the value of X. If this was a true comparative operator, then I would get a blank white page and no output to the browser. Instead, what PHP is going to do is assign the value of 120 to the variable X and it will assign it successfully, therefore, it will return a true statement and then execute my code that I have in my curly braces. I hit refresh in my browser and it did exactly what we expected it to do. Always remember to use at least two equal signs as your comparative operator. The reason why I say to use at least two is because there's another comparative operator that uses three equal signs. That comparative operator not only compares the values, but it also compares the types of values. As you can see, I'm assigning X a number value of 20. In FileMaker, we're able to specify different data types to fields, such as text, number, date and so on. In PHP, we also have different data types, which I'll be talking about later on. But we need to know how to use the different comparative operators. So for this example, I'll say that X is equal to the string 20 and we'll be looking to compare against the number 20. If I use just two double quotes, we'll get output to the screen that says X is equal to 20. But if I use three double quotes, I'm not only evaluating the value, but the value type. And the types don't match. Because we're assigning X a string value and we're looking for a number value, the result is going to be false. Therefore, I hit refresh in my browser and I get no output to the screen.
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
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 