Locked lesson.
About this lesson
Understanding Variables in PHP.
Exercise files
Download this lesson’s related exercise files.
Variables.docx58.8 KB Variables - Solution.docx
59 KB
Quick reference
Variables
A variable is what you use to store something.
When to use
Anytime you have some data to store, use a variable.
Instructions
Variables in PHP start with a dollar sign: $
Variable names should be descriptive. If your variable will hold an age, name the variable $age or something like that.
Variable names can't start with a number.
Variable names contain alpha-numeric characters, and underscores.
Variable names are case sensitive.
Hints & tips
- Variables are like buckets that let you store stuff.
- 00:05 Okay. It's time to start learning actual PHP.
- 00:07 And in this video I wanna talk about variables.
- 00:09 And variables are one of the most fundamental things in
- 00:12 any programming language.
- 00:13 And a variable, think of it as a bucket, or
- 00:16 a container, something that you can put something else in.
- 00:19 So, if it's a bucket, you can put something in the bucket.
- 00:21 You can take it out later, you can do things with it, it holds stuff.
- 00:27 To create a variable in PHP, and every programming language uses variables, but
- 00:31 they all use them in slightly different ways.
- 00:34 PHP, a variable always starts with a dollar sign.
- 00:38 I don't why this just what they decided to do.
- 00:40 Then you wanna name your variable.
- 00:42 You sort of wanna name your variable in a descriptive way.
- 00:45 So, that if you're gonna create a variable to put people's like first names in there.
- 00:49 Name it first name, if you're gonna use a variable to keep track of somebody's age,
- 00:54 call the variable age, keep it descriptive, so let's call firstname.
- 00:59 This is a variable.
- 00:59 We've now created a variable, and to put something in it,
- 01:02 you just use the equal to sign and quotation marks, and John.
- 01:07 Remember, all PHP statements end in a semicolon, so you always wanna put that.
- 01:11 You're gonna forget.
- 01:12 I forget to this day, but just try and remember that.
- 01:14 There are a few rules when it comes to naming variables.
- 01:17 First of all, like I said, they all start with a dollar sign, and
- 01:20 they can't start with a number, for some reason.
- 01:23 You couldn't put, like, 12 first name.
- 01:24 You see, when we try we get this error, it's telling us, hey, you can't do that.
- 01:29 So, no numbers.
- 01:30 They can have numbers inside them afterwards,
- 01:33 they just can't start with the number.
- 01:35 And they can only contain alphanumeric characters, letters, and numbers and
- 01:39 also underscores.
- 01:41 That thing.
- 01:41 So, if you wanna call it first_name you could do that,
- 01:45 that's sort of a common convention just to make it a little more readable.
- 01:50 Finally variable names are case sensitive.
- 01:53 Uppercase and lowercase, right?
- 01:54 So, Firstname and firstname.
- 02:02 These are two different variables even though they look the same
- 02:05 their case is different.
- 02:06 This is upper case Firstname, this is lowercase firstname.
- 02:10 Also FIRSTNAME that's different too.
- 02:13 So, remember, variables are case sensitive they have to be alphanumeric, letters,
- 02:18 and numbers, and underscore.
- 02:19 You can't start with a number.
- 02:21 So, that's pretty basic.
- 02:23 Get rid of this and change this back to use variables.
- 02:26 We've added John, we've assigned John to the variable first name.
- 02:31 We can echo this out firstname, just like this.
- 02:35 We save this, come back here and hit reload.
- 02:38 Boom, it echoes out John.
- 02:40 It spits out whatever is in our bucket.
- 02:42 We could also type in, Hello my name is, and
- 02:49 quotation marks, save this come back here and hit reload, Hello my name is John.
- 02:56 All kinds of cool things you can do with variables,
- 02:58 we're gonna use these a lot throughout the course.
- 03:00 And just one of the most fundamental things to all programming languages is you
- 03:04 always variables.
- 03:05 And as you can see, very easy to use.
- 03:07 A variable is a bucket, it's a container.
- 03:09 You usually put one thing in, you can take it out, you can output it to the screen.
- 03:15 We'll see later on all the different things you can do with variables.
- 03:18 We can do math with variables, we can all kinds of stuff.
- 03:21 So, that's variables.
- 03:22 In the next video we'll talk about data types.
Lesson notes are only available for subscribers.