Welcome to this basic php Spoken Tutorial. Here we will discuss the 'IF' statement.
If you have written code before, you would have come across the 'IF' statement.
Its not much different in php. I'll execute one shortly and show you.
So, let us start.
Okay, here is a brief about the 'IF' statement. It takes a condition.
If the condition is True, it executes one path of code.
If it is False, it will execute another path of code.
For example - this is the structure.
If - inside the bracket is the condition to know whether 1 equals 1.
Notice I am using a double 'equal to' sign here. This is the comparison operator.
In another tutorial we're going to learn about operators.
It reads as 'is equal to' though it is not the same as 'equals'
When we're using variables, when we want to compare, we use 'double equal to'.
If you are going for the 'True' path, you can use two curly brackets.
We're going to open one here.
Our code will go in between the brackets.
If its 'Not True', we'll say 'else'.
The same structure - so two curly brackets.
For example, if 1 equals 1 we say echo 'True'.
If 1 is not equal 1, what we should get when we run our file is False.
Since 1 is equal to 1 what we get when we run our file is True
Let us change this, if 1 equals 2, which it doesn't, then we'll get False.
So we would have already created a simple program to tell us if one number equals another.
This is quite a silly application for a program.
So I will just add something more. I'll create a little program for a password access.
We are going to store the password in a variable here.
Say the password is abc.
I am going to incorporate a variable into my IF function by saying
If password, remember double equals 'def'
And I'll say 'Access granted'
Sorry, I made a mistake. 'def' is the password we want to ask the user for. 'abc' is the password I'm inputting to the system.
So if it doesn't equal 'def', I'll say 'Access denied'.
The password that I've inputted is 'abc'.
We're going to compare the password to 'def', which is the stored password.
If this equals 'def', we're going to say 'Access granted' else 'Access denied'.
Lets try this.
'Access denied'. This is because the passwords do not match.
On this basis, you can see here that I've incorporated a variable.
Change this to 'def' and we'll get 'Access granted'.
Because I have one line of code here and another one line of code here.
I can get rid of these curly brackets.
To me that looks a lot neater.
Please note - there is no point in adding curly brackets if you have only one line of code for simple IF statements like these.
If you're going to have a line after line here, you'll need the curly brackets.
For example, lets set a new variable here.
Access equals 'Allowed'
That's just basically another line of code.
But when I try and run this, we get an error.
It says an unexpected T_else on line 8
Lets find line 8. Its here. The line before it is causing a problem.
Which is why we need to add our curly brackets back in to cater for two or more lines of code.
We refresh this and Access is granted.
Now I've set a new variable, access to be allowed.
This won't be of much help.
But I was just giving you an example.
You can see this is still a single line and these are double lines and you can't mix them up.
OK, so I've created a variable. I've incorporated it into an 'IF' statement. Hope this was useful.
This brings us to end of this tutorial.
This is Madhu dubbing for the Spoken Tutorial Project. Thanks for watching. Bye.