Home
Username:
Password:
PHP Tutorials

Making Choices / Logical Operators

Subtitles of the Movie

So we’ve had a look at some 'if' statements, we’ve had a look at comparison operators that allow us to create conditions that the 'if' statement can evaluate and carry out commands based on whether those conditions are true or false. Now to make more flexible and complex conditions, we can use the logical operators, where you combine two expressions and put them together as one condition. Let’s have a look at our php script to see how that works. Once again we set a variable and then we use our 'if' statement as before. So in this case greater than 20 and then we use our 'and' operator. This is represented by two ampersands, You’ll probably find these on shift 7 on your keyboard and what that does is it puts these two conditions together or two expressions together to form a condition and in this case we’re going to pick out any age that falls between 20, so if the age is greater than 20 and 40, so if the age is less than 40, then we’re going to say between 20 and 40. Let’s save that and we’ll see if that works. So php has found the age, in this case 32, to be between 20 and 40. What is does is it evaluates this half of the condition and that's true and it evaluates this half of the condition and that's true and because both of them are true then combined together with the and statement then the whole condition is considered to be true. However say if we take age 10 and we try that in our browser, then we can see that, that doesn't work because that condition overall was considered to be false. Even though this side was true, even though 10 is less than 40, because it’s not greater than 20 because this side evaluates to false then the whole thing evaluates false. This is because the ‘and’ condition will only evaluate true when both sides evaluate to true themselves. There is a different condition that we can use to be a little more forgiving and that is the 'or' condition. Now you may have a little trouble finding those symbols on your keyboard, straight up and down slash, of which we use two represent the logical 'or' operator on my keyboard, that's just above the enter key, mine is a standard American standard keyboard, If you use any another kind of keyboard, you may find that in a different place on your keyboard. So let’s try this with our 'or' condition and instead of using this which is a bit misleading now, we’re going to say either greater than 20 or less than 40 or both because that's what our 'or' condition effectively means. Let’s save that again and we’re going to go back here and refresh and as we can see, the message comes up. So we can use our 'or' statement or our 'and' statement depending on whether we want to check to see whether both statements are true or whether either one statement or the other is true. There’s one more operator that we can use and that is the 'not' operator. So in this case, let’s take out one of these expressions, to simplify things, we use the bang or exclamation mark to represent our 'not' operator, then we enclose the expression within brackets so that php knows what we want to work out, what is not true. So effectively what the ‘not’ operator does is reverses whatever we’ve put within that expression, so let’s change this to not greater than 20. Let’s see if this works, not greater than 20, yep! that's been carried out because 10 is not greater than 20. We can see that this statement evaluates false, however when we add the ‘not’, then the condition as whole will evaluate true because the ‘not’ operator basically reverses whatever answer this evaluation will come up with. So if this comes out true, it will come out false, if this comes out false, it will come out true. So if we took for instance the number 25 and save that, we’ll see that we don't get this statement. That is not echoed because 25 is greater than 20 so this evaluates true and so the whole thing evaluates false. So that about sums it up for our logical operators and we’ll be seeing uses for these as we start developing more complex PHP scripts and in the next movie we’re going to look at the 'else' statement and how that can make our 'if' statements more flexible and useful.

Tutorial Information

Course: PHP
Author: Joshua Mostafa
SKU: 33332
ISBN: 1889347787
Release Date: 2002-03-26
Duration: 9.5 hrs / 92 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
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available