PHP-and-MySQL/C2/Variables-in-PHP/English
From Script | Spoken-Tutorial
Time | Narration |
---|---|
0:00 | Welcome to a basic tutorial on PHP variables. |
0:04 | Let me quickly go through a few things first. |
0:07 | PHP variables are very easy to use; I'm sure you'll understand them straight away. |
0:14 | You don't need to declare them and they're very easy to write. |
0:18 | You can add a value to a variable half way through the script. |
0:23 | Also, they automatically convert to the data type you require. |
0:28 | So there's no need to declare them in a different way each time, or create a value for them each time. |
0:36 | So, for example, let's create our PHP tags here and our content goes in between. |
0:41 | Okay. Now we start with the dollar sign and then we have our variable name. |
0:47 | Please note that you can't start with a "number". So I can't start with a '1'. |
0:53 | What I can start with is an "underscore" or a "letter". |
0:57 | No other special characters are allowed except underscores, letters and numbers, as long as it doesn't start with a number. |
1:05 | So that would be perfectly acceptable here. |
1:09 | Okay, so I'll create a variable called "name" and that's going to be equal to a string value contained within double quotes, just like we used for the "echo" function. |
1:21 | 'My name is Alex'. |
1:23 | On the next line, we're going to create another variable using again a dollar sign, called 'age'. That's going to be equal to '19', without double quotes. |
1:33 | Now the reason for this is that this is an integer. |
1:36 | You can use it for decimal values as well. So this could be '19.5' or nineteen and a half. |
1:43 | That would also automatically convert this into a decimal. |
1:48 | However, at the moment it's just an integer. That's how I want it - the variable 'name' is a string and the variable 'age' is an integer. |
1:57 | So let's try echoing these out. |
2:00 | What we need is "echo" and the variable name, not forgetting your line terminator. |
2:06 | Okay, let's find our file named "variables". |
2:11 | Okay, "Alex" has been echoed out, just like I've said here, "echo name". |
2:16 | Let's try and echo out my age now. |
2:19 | It's just an integer variable and that's been echoed out here. |
2:24 | Okay so, the thing with variables is that they're very easy to concatenate into a string. |
2:30 | In fact, probably, concatenation is the wrong word – they're very easy to include inside your string. |
2:36 | If you don't know what concatenation is, it just means to join two things together or to join two strings together in a line. |
2:46 | So, an example of concatenation would be, let's see, 'concat' and then I could say, '.' and then 'ination'. |
2:56 | Now, this would echo out 'concatination'. |
2:59 | Let's try this. Okay? |
3:03 | But there is a completely different tutorial on that. So, what I'll say is, for now, you don't need to include this as one of your variables when you're echoing it out. |
3:14 | If you can't follow this, don't worry. This is very, very simple. |
3:18 | I'll say "My name is" name "and my age is" and put my age down. |
3:24 | So, it's all in one string, all in one echo, and we've just got 'My name is' – plain text. |
3:32 | Variable is called. This is put here. Then when age is called, the value for age is put here. |
3:40 | So, we can refresh that and you can see that "My name is Alex". That's our variable. "and my age is 19" and that's our variable. |
3:48 | So they're really easy to put into strings. |
3:52 | Okay, this is all you really need to know now about variables. |
3:56 | There are other types of variables, like boolean, decimal – which I've shown you, for example like '19.5'. |
4:06 | You would declare them in the same way, with a dollar sign. |
4:10 | So practice this and you can come back and learn some more advanced functionality later on, when I'm going through some other projects. |
4:19 | Thanks for watching! This is Joshua Mathew dubbing for the Spoken Tutorial project. (Script contributed by Bhavini Pant). |