Logical expressions are used to control how code decides what to do based on specific situations. If statements are the most commonly used logical expressions. They revolve around a statement either being true or false and, if true, executing another line of code. Learn how to use if statements in this PHP with MySQL training tutorial.
- View Offline
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. And we're going to do that by using logical expressions. The most common of all types of logical expression, is the if statement,. This is the basic format of an if statement in PHP. We've got if followed by in parentheses an expression. And that expression is going to evaluate to either true or false. It's going to have a Boolean result. So 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. So for example, if we have if a is greater than b, then echo back a is larger than b. So 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. Now, 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 to 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. So always use curly braces. Notice also that I've indented the statement portion. PHP doesn't care about the indentation. Remember whitespace doesn't matter to PHP. But 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 open and curly brace, that I have a matching closing curly brace. And we start nesting these inside of each other, it can sometimes to be hard to tell, and easy to lose track of them.
So let's start with those two good programming habits, always using curly braces, and always indenting our code. Let's try some if statement for ourselves. So here I am in my sandbox and I'm just going to open up basic.html. I'm going to choose Save As. Then I'm going to call this logical.php and save that in my sandbox. Logical. So let's just try the example that we just saw there. So we have php and in it, we're going to define a is equal to 4.
And let's have b and have that be equal to 3. And then we'll write our statement. If a is greater than b. Use my curly braces. Notice that it indented for me, TextMate does that helpfully. Echo a is larger than b. Okay so that's my statement. Let's go and try this out and see how it works. Switch back over to Firefox and I'm going to be going over to localhost/~kevinskoglund/sandbox/logical.php.
A is larger than b. Now you can play with those values, and we can say all right, 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 Alright, simple enough. Alright, lets try some some, some more maybe real world examples. Something a little less experimental. Lets say that we, have the 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 im going to say that it's true, If that is true, then we're going to display this welcome message to them. So we can experiment with that, you can try this from true to false and back again, but let's just go over here, try it out in our browser, and you'll see that we get our welcome message, because it was true. Another good example, you remember that we talked about that you can't divide by 0/ That gives you problems? Well, here's a way to check that. Our numerator that's 20, the denominator that's 4 If the denominator is greater than zero, than it's okay to proceed with the division. If it isn't greater than zero, then we may have a problem here, because this can't be zero.
Now I 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. Now, 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, alright. So 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 blow that up. It works just fine. But if I now make the denominator into zero, so that it would not execute this statement, right? Because now this fails, this is false. Let's come back over here, and let's reload the page. And you'll see, oops, undefined variable.
And the result, no result there. So 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, right? So we're going to start with a result of 0, and then, that way result will still have a value. Because this code. Does not execute at all if that statement is not true. So you'll want to watch for that. Think about the 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.
Okay, let's go back up here to the top and you see that we've got this if a is greater than b. So 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 and we're going to see how to do that in the next movie.
- What is PHP?
- Installing and configuring PHP and MySQL
- Exploring data types
- Controlling code with logical expressions and loops
- Using PHP's built-in functions
- Writing custom functions
- Building dynamic webpages
- Working with forms and form data
- Using cookies and sessions to store data
- Connecting to MySQL with PHP
- Creating and editing database records
- Building a content management system
- Adding user authentication