1 00:00:00 --> 00:00:08 Hello and welcome to a tutorial on "Logical Operators". Its a very brief tutorial but I will keep it like that at the moment. 2 00:00:09 --> 00:00:17 I'll use an example of an "if" statement again because that's all I have got at the moment. 3 00:00:18 --> 00:00:26 What is a logical operator? Let's add a bit of logic and say its the 'and' or the horizontal line operator. 4 00:00:27 --> 00:00:42 Now if I start creating my basic layout for my "if" statement i will get to work on showing you what you can do with these. 5 00:00:43 --> 00:00:53 Before we had example such as 1 is greater than 1 which at the moment is going to return 'false' 6 00:00:54 --> 00:01:03 let's just check it to see where we are.... right . So that's "false". 7 00:01:04 --> 00:01:17 Now what if I said "if 1 is greater than 1 or equals 1". 8 00:01:18 --> 00:01:25 Now we don't write it as 'or' we write it as two horizontal lines or two pipes. 9 00:01:26 --> 00:01:37 Not quiet sure about that but if you know my keyboard it will be next to the shift key - two vertical line that means 'or'. 10 00:01:38 --> 00:01:42 So if we compile this what do you think the result is gonna be? 11 00:01:43 --> 00:01:53 Now let's run through this once - if 1 is greater than 1 - "false" and so we have written "false" or 1 is equal to 1... 12 00:01:54 --> 00:02:08 We know that 1 equals to 1 is "true" so here we are saying 'or' 1 is equal to 1 not 'and' because we said 'and' then both would have to be "true". 13 00:02:09 --> 00:02:11 or either of these could be "true" to make this. 14 00:02:12 --> 00:02:15 The output. So hopefully we get "true". 15 00:02:16 --> 00:02:17 Okay so that's the 'or'. 16 00:02:18 --> 00:02:29 Basically it allows you to take two comparisons, show them in your "if" statement and if either of them are "true" - then its like an "either" operator... 17 00:02:30 --> 00:02:33 "either" of them are "true", you will be left with "true". 18 00:02:34 --> 00:02:38 "and" operator is a different manner. 19 00:02:39 --> 00:02:45 "and" requires both of these to be "true" for this to be executed. 20 00:02:46 --> 00:02:50 So we have got "false" here because 1 is not greater than 1. 21 00:02:51 --> 00:03:03 We'll go back to our comparison operators and we will say "if 1 is greater than 1 or equal to 1 'and' 1 equal 1", here we will get "true". 22 00:03:04 --> 00:03:09 So now, all I can really think is about to add a few variables in to this test. 23 00:03:10 --> 00:03:16 But I am pretty sure that you have got the hang of variables by now, by following my other tutorials. 24 00:03:17 --> 00:03:19 So these are the two logical operators. 25 00:03:20 --> 00:03:29 You will find them very useful because you might want to say for example - this is a very classic example. You'll find it in one of my projects.... 26 00:03:30 --> 00:03:34 It is a "login" form. Say a user wants to login into a website. 27 00:03:35 --> 00:03:42 You've probably logged into a website before and it said to enter your "username" and "password". Now the keywords are in here. 28 00:03:43 --> 00:03:47 We need to check if the users have entered the "username" and the "password". 29 00:03:48 --> 00:03:51 If they haven't, there is no point in comparing the "username" to the "password". 30 00:03:52 --> 00:03:53 So we can say, for example. 31 00:03:54 --> 00:04:03 If the username in fact let's do this. I'll say "username" is equal to "alex" and my password is equal to "abc". 32 00:04:04 --> 00:04:10 Now I will substitute these. I can say "username" and "password". 33 00:04:11 --> 00:04:14 At the moment, this will say "true". 34 00:04:15 --> 00:04:26 I'll change this. I'll say 'ok' or 'you forgot to fill out a field' because there will eventually be HTML fields. 35 00:04:27 --> 00:04:31 This is going to be okay because we have got both values. 36 00:04:32 --> 00:04:36 So let's try it. Yes, that's saying "ok". 37 00:04:37 --> 00:04:47 Now what happens if I forget to type my password in there? There's nothing in there at the moment - no space - lets get rid of that. 38 00:04:48 --> 00:04:49 'You forgot to fill out a field'. 39 00:04:50 --> 00:04:59 So if you imagine these are coming from the user - so it has been submitted as you typed your "username" and "password" in. 40 00:05:00 --> 00:05:06 We are saying "username" and "password"; basically "username" itself is "true" because it exists... 41 00:05:07 --> 00:05:13 If you had that inside, that would be acceptable; that would be "true". 42 00:05:14 --> 00:05:17 We'll just check that there you go. 43 00:05:18 --> 00:05:22 So because we have got "username" and "password" then that's fine. 44 00:05:23 --> 00:05:28 But for the 'or' that doesn't really makes sense and you can imagine what will happen. 45 00:05:29 --> 00:05:35 So right now, this will equal "true" because we have got both values. So this is 'ok'. 46 00:05:36 --> 00:05:40 Now if I go with both of them and try it out. 47 00:05:41 --> 00:05:44 "if the username exists" so the username is "true"... 48 00:05:45 --> 00:05:47 At the moment there is no value - so it is "false". 49 00:05:48 --> 00:05:55 "or the password is true" - that is, the value exists; at the moment it doesn't, so it is "false". 50 00:05:56 --> 00:05:59 So we are going to say "You forgot to fill out a field". 51 00:06:00 --> 00:06:04 I'll just write here nothing because at the moment it means nothing. 52 00:06:05 --> 00:06:07 So refresh I'll make it nothing. 53 00:06:08 --> 00:06:16 So you see, already I have explained how useful these can be in so many every day php applications. 54 00:06:17 --> 00:06:21 For example - a form someone can fill in. You will find many other users for it. 55 00:06:22 --> 00:06:23 But that's it then. 56 00:06:24 --> 00:06:26 Two operators that are logical operators. 57 00:06:27 --> 00:06:30 Just try them out and see what all you can do with them. 58 00:06:31 --> 00:06:34 I will be using these most definitely in one of my projects quite soon. 59 00:06:35 --> 00:06:36 Thanks for watching. 60 00:06:37 --> 00:06:42 This is Sidharth dubbing for the Spoken Tutorial project.