In this tutorial we're going to be talking about controlling the flow of logic. And we're going to start off with the IF Statement. Now if you're familiar with FileMaker you'll know that up in the ScriptMaker inside of the Script Editing Dialog there are script steps that help you to control the flow of the logic and there's an IF Script Step that allows you to place some sort of logical condition. Which if it's true then you can add script steps to execute inside that IF Statement. You can also move on to alternative conditions if it's not true by using the ELSE IF. And you can apply other script steps to execute. And then if none of the conditions are true you can finally use the ELSE Statement and add some sort of logic to perform if none of the conditions are met. You can do the same kind of IF and ELSE IF Statements inside of PHP and this is used very frequently. So let's see how those work in the PHP Code Editor. First of all I'm going to open up my 0301.php file. And here I'm going to go ahead and assign the value of X is equal to 20. And then I'm going to check that condition. In PHP you use the IF Statement and in open and closing parenthesis you will place your condition. We'll say if X is equal to 20 then execute some php code. And you surround that code to execute if it's true in open and closing curly braces. So we'll say if X is equal to 20 then let's go ahead and output that the condition is true. And now let's go ahead and refresh our browser and it says the condition was true. Here I have the 0301.php page open in my browser. Now what if the condition was not true. Let's go ahead and we'll say that X is actually equal to 30. Well if I refresh the browser I'm going to get a blank page because it won't execute this code and there's no code after that. So we'll say if it's equal to 20 then the condition is true, else echo the condition is false. And let's make sure we put our closing brace and go ahead and refresh that. And now we have the condition is false. Notice that when I do my comparative up here in the parenthesis that I'm using two equal signs. One equal sign assigns the value to the variable. So if we only had one equal sign here we would be assigning the value of 20 to X and it would do it successfully so the result would be true. Even though X is not equal to 20. So let's go ahead and refresh that and you can see the condition is true. X is assigned successfully the value of 20. And if we put X in here we can see that in the output. So I'm saying X is equal to 30 but in my comparison I am accidentally reassigning the value 20 to X. So watch out for that. You always want to use two equal signs as a simple comparative. Now next, you may want to check to make sure it's the same data type. So even though 20 as a number is equal to 20, 20 as text is not equal to the number 20. With two equal signs it will convert that to a number or this to text to compare them. It's going to do it loosely so it will turn out to be true. But if we want to make sure that what we're comparing is not just the value but the data type, a text is not equal to a number and we use the third equal sign to compare types. So if I refresh the page the condition is now false. Lastly I can also have an ELSE IF Statement to compare one more condition and we'll say if X is less than 8. And here we'll say that our less than condition was true. So we have an IF, an ELSE IF and then an ELSE. And we're going to go ahead and change our value to negative 60 and we'll refresh our browser and the less than condition was true. And if no condition was true then the conditions are all false. So this concludes our lesson on using the IF and ELSE IF Control Operator.
| Course: | FileMaker and PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 34393 |
| ISBN: | 978-1-61866-077-0 |
| Release Date: | 2012-12-07 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |