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