Welcome to a basic tutorial on PHP variables.
Let me quickly go through a few things first.
PHP variables are very easy to use; I'm sure you'll understand them straight away.
You don't need to declare them and they're quite easy to write.
You can add a value to a variable half way through the script.
Also, they automatically convert to the data type you require.
So there's no need to declare them in a different way each time, or create a value for them each time.
So, for example, let's create our PHP tags here and our content goes in between.
Okay. Now we start with the dollar sign and then we have our variable name.
Please note that you can't start with a "number". So I can't start with a '1'.
What I can start with is an "underscore" or a "letter".
No other special characters are allowed except underscores, letters and numbers, as long as it doesn't start with a number.
So that would be perfectly acceptable here.
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.
'My name is Alex'.
On the next line, we're going to create another variable using a dollar sign, called 'age'. That's going to be equal to '19', without double quotes.
Now the reason for this is that this is an integer.
You can use it for decimal values as well. So, this could be '19.5' or nineteen and a half.
That would also automatically convert this into a decimal.
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.
So, let's try echoing these out.
What we need is "echo" and the variable name, not forgetting your line terminator.
Okay, let's find our file named "variables".
Okay, "Alex" has been echoed out, just like I've said here, "echo name".
Let's try and echo out my age now.
It's just an integer variable and that's been echoed out here.
Okay so, the thing with variables is that they're very easy to concatenate into a string.
In fact, probably, concatenation is the wrong word – they're very easy to include inside your string.
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.
So, an example of concatenation would be, let's see, 'concat' and then I could say, '.' and then 'ination'.
Now, this would echo out 'concatination'.
Let's try this. Okay?
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.
If you can't follow this, don't worry. This is very very simple.
I'll say "My name is" name "and my age is" and put my age down.
Now, it's all in one string, all in one echo, and we've just got 'My name is' – plain text.
Variable is called. This is put here. And then when age is called, the value for age is put here.
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.
So they're really easy to put into strings.
Okay, this is all you really need to know about variables.
There are other types of variables, like boolean, decimal – which I've shown you, for example like '19.5'.
You would declare them in the same way, with a dollar sign.
So practice this and you can come back and learn some more advanced functionality later on, when I'm going through some other projects.
Thanks for watching! This is Joshua Mathew dubbing for the Spoken Tutorial project. (Script contributed by Bhavini Pant).