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.
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.
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.
Who decides what the name of variable is?
The variable name is coded by the code’s author.
If you are writing code, then you are declaring that a given variable has a specific name. You can name whatever you want as long as it complies to the PHP syntax and naming rules.
If you are using a variable, then you have to use the exact variable name. Period.
Will the code work if you make a typo when using a variable in your code?
It will not work as intended. If the name you accidentally typed does not exist, then you’ll get a PHP Notice to let you know that variable is “Notice: Undefined variable.” The variable doesn’t exist.
For example, if the variable’s name is
$wp_querybut you accidentally type
$wpquery, what will happen? You’ll get a PHP Notice error. The name you typed is not the same as the one you intended.
Naming matters! You have to use it exactly the why it’s named. Othewise, it’s not the same variable.
Do you have to name variables per the WordPress coding standard?
Nope. If you are contributing to WordPress Core, then yes, you do. Otherwise, you can use a naming standard that your company and you define.
It’s prevalent in the programming world outside of WordPress to use different naming conventions. Laravel and Symfony both use camelCase.
Let me point you to some additional lessons on variables.
Code. Eat. Code. Sleep. Dream about Code. Code.
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