What’s the deal with Variables?
Lab: PHP 101: Gentle Introduction to WordPress Programming
Video Runtime: 27:05
What is a variable? What does it do? How do you use? Why does it exist? Let’s talk about variables and, more specifically, variables in PHP.
Just like in mathematical equations and formulas, variables are used to “represent” a value. In programming, we use variables to represent some piece of information. We give that information a name and context, i.e. some human-readable meaning by the way we name the variable. Then we use the variable within our code to make decisions, do processing, configure a task to run, etc.
Variables are defined and named by the author of the code. That means if you are writing the code, then you are naming that variable.
How does PHP recognize that it’s a variable?
…by its syntax. Huh? You define a variable by starting with a dollar sign $
. That tells PHP “Hey, this is a variable.” Then you name it. That name is stored in memory. When you use that variable, PHP goes and looks up the value that it’s bound to and uses it to do the work or make the decision.
Naming
PHP is restrictive on the first character of the variable name. You can use a lowercase letter (a-z), uppercase letter (A-Z), or an underscore (_). That’s it. No numbers or special characters. Then the rest of the variable’s name can be any combination of alphanumeric characters except for the special ones like !?#& etc.
$10posts
is not a valid PHP name.
$number_of_posts
is a valid PHP name and it complies to the WordPress coding standard.
$_number_of_posts
is also a valid PHP name. Note, that this variable is not the same as the previous one. Why? It starts with an underscore.
Key Points
Let’s sum up some key points from this episode:
- The value for a variable can varying (hence why it’s called “variable”)
- It’s a symbol
- It represents some information, i.e. some value
- You name it
- Name has to comply with the PHP syntax rules
- When you use a variable, you have to use it exactly as it’s defined.
In this episode, we’ll discuss all of these points. Plus, you’ll write a little code to understand the naming and why you have to use it exactly as it’s defined.
Challenge Yourself
-
Who decides what the name of variable is?
-
Will the code work if you make a typo when using a variable in your code?
-
Do you have to name variables per the WordPress coding standard?
Further Learning
Let me point you to some additional lessons on variables.
You get WET when you swim. Stay DRY when you code.
Episodes
Total Lab Runtime: 03:21:32
- 1 Lab Introductionfree 11:37
- 2 What is PHP? Why use it?free 17:49
- 3 Why and how does WordPress use PHP?free 07:33
- 4 Syntax Basicsfree 18:34
- 5 What's the deal with Variables?free 27:05
- 6 Break Up Code into Logical Partsfree 07:55
- 7 Subroutines - Behold the functionfree 14:11
- 8 Loading Files to Runfree 13:20
- 9 To Run or Not to Run - Making Decisionsfree 19:07
- 10 Sequencing - Yup, code runs in orderfree 06:16
- 11 Repeating code using Loopsfree 13:57
- 12 Building Strings with Dots and Variablesfree 15:10
- 13 What's the deal with scope?free 08:34
- 14 Naming Stufffree 06:18
- 15 Putting it All Togetherfree 08:33
- 16 Where do I go from here?free 05:33