Chapter 1. Variables
The foundation of a flexible application is variability—the capability of the program to serve multiple purposes in different contexts. Variables are a common mechanism to build such flexibility in any programming language. These named placeholders reference a specific value that a program wants to use. This could be a number, a raw string, or even a more complex object with its own properties and methods. The point is that a variable is the way a program (and the developer) references that value and passes it along from one part of the program to another.
Variables do not need to be set by default—it is perfectly reasonable to define a placeholder variable without assigning any value to it. Think of this like having an empty box on the shelf, ready and waiting to receive a gift for Christmas. You can easily find the box—the variable—but because nothing is inside it, you can’t do much with it.
For example, assume the variable is called $giftbox. If you were to try to check the value of this variable right now, it would be empty, as it has not yet been set. In fact, empty($giftbox) will return true, and isset($giftbox) will return false. The box is both empty and not yet set.
Note
It’s important to remember that any variable that has not been explicitly defined (or set) will be treated as empty() by PHP. An actually defined (or set) variable can either be empty or non-empty depending on its value, as any real value that evaluates to false will be treated as ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access