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