Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
In the last movie, we saw the basics of an if statement and how we could have a section of code that only executes if an expression evaluates to true. In this movie, we're going to build on that by talking about else and else if. That is the ability to have an alternative that gets executed if something is not true. Let's take a look. So, if we have if a is greater than b, we want to echo a is larger than b. We can use else immediately after it to say if that's not true, then the alternative version, should be to echo this section of code.
This statement, echo a is not larger than b. So one of these two things will be true. We've basically divided the path into two and said, if this is true, do the first statement, else do the second statement. Now again, this is without the curly braces, which is suitable if you have only a single line of code, but I'm going to strongly encourage you, even if you have a single line of code to always use curly braces. And if you have multiline code, you'll definitely want to use them. I think it's a good habit to be in. Now I do want you to see that it is a binary choice between these. It's going to do one or the other.
Once that first test passes and its statement is executed, the rest of the options below that are skipped over. It jumps to the very end of this whole statement down to the very final curly quote, and that's where it picks up and keeps executing. So if a is greater than b, echo a is larger than b, and then, jump down to that final curly quote and proceed from there. It doesn't continue checking for additional options. Once it finds one, it's done. It's done with this whole, entire statement. Now, in addition to just else, we also have else if, and that lets us string multiple conditions together. So we could say, if a is greater than b, then echo a is larger than b, else if is a is less than b, echo a is smaller than b.
Notice before I said not larger, because there's a possibility that the two are equal. So now I can say else if do another test. So if the first one matches, then do the first statement. If the second one matches, they'll do the second statement. If a and b are equal, well then neither one of these are going to execute. Right? Neither one of its conditions were met, so we'll just keep going from there. And of course, we can combine all three of these together, so that if a is greater than b we echo a is larger than be, if as is less than b we echo a is smaller than b, or the final catch-all, is the possibility that a is equal to b.
It's not greater than, it's not less than, it must be equal to. So when we only have two, else is the alternative version, but if we have multiple ones, then else is sort of the default. It's the final thing, if none of these other conditions matched up above, this is the final result that you ought to do. Let's go back to our page and try those. So, I'm going to go back to the same page that I was working on before. That's the logical.php. We'll just do it the same page. Notice before, that I had two if statements. If a is greater than b, or if a is less than be, I can now convert those.
And I could do it by using else and say else a is not larger than b. Or, the other way to do it would be just back up and undo that to change this to say, else if a is less than b, a is not larger than b. Now, that's not quite true, it's a is smaller b in that case. And so then, we can add that final condition, which will echo, a is equal to b. Now once again, if a is greater than b, this bit of code gets executed. After that, it jumps right down to here and keeps executing our PHP.
If this one's not met, then it goes to the second one. If that's not met, then it goes to the third one. But after each one of these, after each determination is made, that block gets executed and then it jumps down here to continue with our PHP. So the combination of these three, the ability to have if, else if and else, really gives us lots of control over the flow of our code. We can now start to fork our code and say if the new user is true, well give them a welcome message. If they're not a new user, give them a different message. If they are a user type of student, give them one thing, if they're a business give them something else.
If they're a consumer, give them something else, and we can start nesting these inside of each other, as well. So that if they're a student and they also are enrolled in a special program, we give them a different message. You can really start to see the important role that logical expressions are going to play in programming with php.
Find answers to the most frequently asked questions about PHP with MySQL Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.