Join Kevin Skoglund for an in-depth discussion in this video If statements, part of PHP Essential Training.
- [Instructor] In this chapter, we're going to be looking at how we can start to control the flow of our code. To have our code make choices about what should happen based on certain conditions. We're going to do that by using logical expressions. Most common of all types of logical expression is the if statement. This is the basic format of an if statement in php. You've got if followed by, in parentheses, expression. That expression is going to evaluate to either true or false. It's going to have a billion result. If the expression is true, then the statement that follows it will be executed.
If it evaluates to false, then the statement will not be executed and the code will skip past it and keep on evaluating down the rest of the php after it. For example if we have, if a is greater than b, then echo back a is larger than b. If the expression a is greater than b evaluates to true, then we'll get something echoed. If it's not true, then the echo line will never execute. Php will just ignore it and move right on past it. This format that I'm showing you here works if the statement portion that's going to be executed is just a single line.
If you have more than one line of code that you want to execute for the statement portion, then you'll want to use curly braces around that statement. This makes it clear that everything inside the braces should be executed if the expression evaluates are true. It's required for multi-line statements, but it's also considered a best practice to add them around single-line statements too. It's clearer, and I also think it's easier to develop using one style and just to stick with it. Always use curly braces. Notice also that I've invented the statement portion. Php doesn't care about the indentation.
Remember white space doesn't matter to php. Indentation is going to help us a lot, because it's going to improve code readability. It's also going to help me to make sure that for every opening curly brace, that I have a matching closing curly brace. When you start nesting these inside of each other, it can sometimes be hard to tell and easy to lose track of them. Let's start with those two good programming habits. Always using braces and always indenting our code. Let's try some if statements for ourselves. Here I am in my sandbox. I'm just going to open up basic.html.
I'm going to choose save as. I'm going to call this logical.php and save that in my sandbox. Logical. Let's just try the example that we just saw there. We have php and in it we're going to define a is equal to four. And let's have b and have that be equal to three. Then we'll write our statement. If a... Is greater than b, use my curly braces.
Notice that it indented for me. Text mate does that helpfully. Echo a is larger than b. That's my statement. Let's go and try this out and see how it works. Switch back over to Firefox. We're going to be going to local host. Kevin Skogland. And sandboxlogical.php. A is larger than b. You can play with those values and we can say alright, let's set these in reverse.
Let's go back and let's reload the page and notice that it doesn't output anything. It only evaluates what's inside the curly braces if this statement is true. Simple enough. Let's try some more maybe real world examples. Something a little less experimental. Let's say that we have a website and we want to have a special welcome message that displays only to new users of the website. So we determine whether someone is a new user or not. In this case I'm going to say that it's true. If that is true, then we're going to display this welcome message to them.
We can experiment with that. You can try this from true to false and back again. Let's just go over here. Try it out in a browser. You'll see that we get our welcome message because it was true. Another good example, you'll remember that we talked about that you can't divide by zero, that that gives you problems. Here's a way to check that. We have our numerator that's 20, the denominator's four. If the denominator is greater than zero, then it's okay to proceed with division. If it isn't greater than zero, then we may have a problem here because this can't be zero. Realize that this doesn't account for negative numbers, but you still get the point.
You see what I'm saying, that it is not letting you divide by zero before it gives you the result. Let's just try that one out real quick. It says no problem. I can go ahead and I can do that division. Notice that the echo for result is occurring inside this if statement. I just want to move that out of there, so that you see what happens if I do this. It doesn't do the division, but it's going to echo then the result here. Let's go back and see what happens when we load that up. It works just fine, but if I now make the denominator into zero, so that it would not execute this statement, because now this fails, this is false.
Let's come back over here, let's reload the page. You'll see oops, undefined variable. The result? No result there. Notice that the result is being defined in the if statement. That's an important point. If we're defining variables in the if statement, then we need to make sure that we account for the possibility that result is not set. We're going to start with a result of zero. Then, that way result will still have a value because this code does not execute at all if that statement is not true.
You'll want to watch for that. Think about this sort of path through your code. If the if statement doesn't execute, none of the variables and assignments that you make in there are going to be available to you. Let's go back up here to the top and you see that we've got this if a is greater than b. If we wanted to account for the possibility that if a was less than b, we could say a is not larger than b. Right? And that would work. However, there's a better way to do that than to just do a series of if statements one after the other.
We're going to see how to do that in the next movie.
- What is PHP?
- Embedding PHP code on a page
- Inserting code comments
- Variables, strings, arrays, and Booleans
- If, else, and elseif statements
- While and for loops
- User-defined functions
- Function arguments and return values
- Debugging and troubleshooting