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

Contributors and Content Editors

Chandrika, Pravin1389