PHP-and-MySQL/C2/If-Statement/English

From Script | Spoken-Tutorial
Revision as of 11:59, 29 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
0:0 Welcome to this basic php Spoken Tutorial. Here we will discuss the 'IF' statement.
0:05 If you have written code before, you would have come across the 'IF' statement
0:08 Its not much different in php. I'll execute one shortly and show you
0:14 So, let us start.
0:16 Okay, here is a brief about the 'IF' statement. It takes a condition.
0:20 If the condition is True, it executes one path of code.
0:23 If it is False, it will execute another path of code.
0:27 For example - this is the structure.
0:32 If - inside the bracket is the condition to know whether 1 equals 1.
0:37 Notice I am using a double equal to sign here. This is the comparison operator.
0:42 In another tutorial we're going to learn about operators.
0:46 It reads as 'is equal to' though it is not the same as 'equals'
0:55 When we're using variables, when we want to compare, we use double equal to.
0:58 If you are going for the True path, you can use two curly brackets.
1:02 We're going to open one here.
1:04 Our code will go in between the brackets.
1:07 If its Not True, we'll say else.
1:11 The same structure - so two curly brackets.
1:14 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:26 Since 1 is equal to 1 what we get when we run our file is True
1:35 Let us change this, if 1 equals 2, which it doesn't, then we'll get False.
1:41 So we would have already created a simple program to tell us if one number equals another.
1:46 This is quite a silly application for a program.
1:49 So I will just add something more. I'll create a little program for a password access.
1:56 We are going to store the password in a variable here.
2:00 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:14 And I'll say 'Access granted'
2:20 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:38 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:46 If this equals 'def', we're going to say 'Access granted' else 'Access denied'.
2:54 Lets try this.
2:58 'Access denied'. This is because the passwords do not match.
3:00 On this basis, you can see here that I've incorporated a variable.
3:06 Change this to 'def' and we'll get 'Access granted'.
3:13 Because I have one line of code here and another one line of code here.
3:20 I can get rid of these curly brackets.
3:24 To me that looks a lot neater.
3:27 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:34 If you're going to have line after line here, you'll need the curly brackets.
3:39 For example, lets set a new variable here.
3:44 Access equals 'Allowed'
3:50 That's just basically another line of code.
3:55 But when I try and run this, we get an error.
4:00 It says an unexpected T_else on line 8
4:07 Lets find line 8. Its here. The line before it is causing a problem.
4:11 Which is why we need to add our curly brackets back in to cater for two or more lines of code.
4:20 We refresh this and Access is granted.
4:23 Now I've set a new variable, access to be allowed.
4:26 This won't be of much help.
4:29 But I was just giving you an example.
4:31 You can see this is still a single line and these are double lines and you can't mix them up.
4:37 OK, so I've created a variable. I've incorporated it into an IF statement. Hope this was useful.
4:42 This brings us to end of this tutorial.
4:45 This is Mad Madhur dubbing for the Spoken Tutorial Project. Thanks for watching. Bye.

Contributors and Content Editors

Chandrika