Hello and welcome to a tutorial on "Logical Operators". Its a very brief tutorial but I will keep it like that at the moment.
I'll use an example of an "if" statement again because that's all I have got at the moment.
What is a logical operator? Let's add a bit of logic and say its the 'and' or the horizontal line operator.
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.
Before we had example such as 1 is greater than 1 which at the moment is going to return 'false'
let's just check it to see where we are.... right . So that's "false".
Now what if I said "if 1 is greater than 1 or equals 1".
Now we don't write it as 'or' we write it as two horizontal lines or two pipes.
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'.
So if we compile this what do you think the result is gonna be?
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...
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".
or either of these could be "true" to make this.
The output. So hopefully we get "true".
Okay so that's the 'or'.
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...
"either" of them are "true", you will be left with "true".
"and" operator is a different manner.
"and" requires both of these to be "true" for this to be executed.
So we have got "false" here because 1 is not greater than 1.
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".
So now, all I can really think is about to add a few variables in to this test.
But I am pretty sure that you have got the hang of variables by now, by following my other tutorials.
So these are the two logical operators.
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....
It is a "login" form. Say a user wants to login into a website.
You've probably logged into a website before and it said to enter your "username" and "password". Now the keywords are in here.
We need to check if the users have entered the "username" and the "password".
If they haven't, there is no point in comparing the "username" to the "password".
So we can say, for example.
If the username in fact let's do this. I'll say "username" is equal to "alex" and my password is equal to "abc".
Now I will substitute these. I can say "username" and "password".
At the moment, this will say "true".
I'll change this. I'll say 'ok' or 'you forgot to fill out a field' because there will eventually be HTML fields.
This is going to be okay because we have got both values.
So let's try it. Yes, that's saying "ok".
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.
'You forgot to fill out a field'.
So if you imagine these are coming from the user - so it has been submitted as you typed your "username" and "password" in.
We are saying "username" and "password"; basically "username" itself is "true" because it exists...
If you had that inside, that would be acceptable; that would be "true".
We'll just check that there you go.
So because we have got "username" and "password" then that's fine.
But for the 'or' that doesn't really makes sense and you can imagine what will happen.
So right now, this will equal "true" because we have got both values. So this is 'ok'.
Now if I go with both of them and try it out.
"if the username exists" so the username is "true"...
At the moment there is no value - so it is "false".
"or the password is true" - that is, the value exists; at the moment it doesn't, so it is "false".
So we are going to say "You forgot to fill out a field".
I'll just write here nothing because at the moment it means nothing.
So refresh I'll make it nothing.
So you see, already I have explained how useful these can be in so many every day php applications.
For example - a form someone can fill in. You will find many other users for it.
But that's it then.
Two operators that are logical operators.
Just try them out and see what all you can do with them.
I will be using these most definitely in one of my projects quite soon.
Thanks for watching.
This is Sidharth dubbing for the Spoken Tutorial project.