PHP-and-MySQL/C2/If-Statement/English-timed
From Script | Spoken-Tutorial
Time | Narration |
---|---|
0:0 | Welcome to this basic php Spoken Tutorial. Here we will discuss the 'IF' statement. |
0:06 | If you have written code before, you would have come across the 'IF' statement |
0:11 | Its not much different in php. I'll execute one shortly and show you |
0:16 | So, let us start. |
0:18 | Okay, here is a brief about the 'IF' statement. It takes a condition. |
0:23 | If the condition is True, it executes one path of code. |
0:28 | If it is False, it will execute another path of code. |
0:32 | For example - this is the structure. |
0:36 | If - inside the bracket is the condition to know whether 1 equals 1. |
0:41 | Notice I am using a double equal to sign here. This is the comparison operator. |
0:47 | In another tutorial we're going to learn about operators. |
0:50 | It reads as 'is equal to' though it is not the same as 'equals' |
0:56 | When we're using variables, when we want to compare, we use double equal to. |
1:02 | If you are going for the True path, you can use two curly brackets. |
1:06 | We're going to open one here. |
1:08 | Our code will go in between the brackets. |
1:12 | If its Not True, we'll say else. |
1:15 | The same structure - so two curly brackets. |
1:17 | For example, if 1 equals 1 we say echo True. |
1:23 | If 1 is not equal 1, what we should get when we run our file is False. |
1:30 | Since 1 is equal to 1 what we get when we run our file is True |
1:36 | Let us change this, if 1 equals 2, which it doesn't, then we'll get False. |
1:42 | So we would have already created a simple program to tell us if one number equals another. |
1:49 | This is quite a silly application for a program. |
1:52 | So I will just add something more. I'll create a little program for a password access. |
1:58 | We are going to store the password in a variable here. |
2:03 | Say the password is abc. |
2:05 | I am going to incorporate a variable into my IF function by saying |
2:11 | If password, remember double equals 'def' |
2:15 | And I'll say 'Access granted' |
2:21 | 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. |
2:32 | So if it doesn't equal 'def', I'll say 'Access denied'. |
2:39 | The password that I've inputted is 'abc'. |
2:42 | We're going to compare the password to 'def', which is the stored password. |
2:50 | If this equals 'def', we're going to say 'Access granted' else 'Access denied'. |
2:57 | Lets try this. |
3:00 | 'Access denied'. This is because the passwords do not match. |
3:05 | On this basis, you can see here that I've incorporated a variable. |
3:10 | Change this to 'def' and we'll get 'Access granted'. |
3:18 | Because I have one line of code here and another one line of code here. |
3:22 | I can get rid of these curly brackets. |
3:25 | To me that looks a lot neater. |
3:29 | Please note - there is no point in adding curly brackets if you have only one line of code for simple IF statements like these. |
3:37 | If you're going to have a line after line here, you'll need the curly brackets. |
3:42 | For example, lets set a new variable here. |
3:46 | Access equals 'Allowed' |
3:52 | That's just basically another line of code. |
3:57 | But when I try and run this, we get an error. |
4:02 | It says an unexpected T_else on line 8 |
4:08 | Lets find line 8. Its here. The line before it is causing a problem. |
4:13 | Which is why we need to add our curly brackets back in to cater for two or more lines of code. |
4:22 | We refresh this and Access is granted. |
4:25 | Now I've set a new variable, access to be allowed. |
4:29 | This won't be of much help. |
4:32 | But I was just giving you an example. |
4:35 | You can see this is still a single line and these are double lines and you can't mix them up. |
4:40 | OK, so I've created a variable. I've incorporated it into an IF statement. Hope this was useful. |
4:46 | This brings us to end of this tutorial. |
4:50 | This is Madhu dubbing for the Spoken Tutorial Project. Thanks for watching. Bye. |