Learning PHP – Part 3: Variables
Gen. Jose Hernandez (from The Commons)
Well, somehow I managed to let more than a month slip by without getting this post up. Between being quite busy for a lot of the time, and a bit of procrastination it was easy to fall behind. I've been making progress off and on through PHP and MySQL for Dynamic Web Sites, but with the extremely hot weather in Toronto lately, it has been difficult to get any reading done on the subway sometimes.
Scalar Variables
Variables are the subject of this long delayed post. Variables are used in PHP in much the same way that they are in virtually any other major scripting or programming language. Variables use in PHP is fairly informal. A variable's name must start with a dollar sign and be followed with a letter or underscore (e.g. $variable); beyond that it can be called more or less whatever you want. Unlike in some other languages, variables can be created on the fly, and don't have to be declared or initialized beforehand.
The equals sign (=) is used to assign a value to a variable. So $first_name = 'Mike' would assign the value Mike to the variable $first_name. A double equals (==) is used to check if two values match, an important distinction to remember, as mixing the two up will cause all sorts of issues and may not be that obvious at first.
At the most basic level, variables can contain 4 types of scalar variables: string, float (floating point number), integer or boolean.
A string can be any set of characters, like Mike in in the example earlier, or the entire text of this post, or a date among many other examples. Two strings can be concatenated (stuck together) using a period. Also, there are many functions related to working with strings in PHP.
A variable that contains only numbers can be treated as a string as well, but it differs from a variable containing a string in that there are also many number specific functions and operators as well. The obvious example is basic arithmetic, the basic operators for which are +, -, *, /, % (modulus), ++ (increment) and -- (decrement). Depending on the type of number it will either be stored as an integer or a floating point number.
Finally, boolean variables store a simple true/false value. This has many useful applications, such as storing whether or not a form has been filled out on a page that either displays or handles a form.
Some other notes about Variables
Another important basic variable to be aware of is constants. Once defined, these variables cannot have their value changed within a script. Examples of information that would be stored in constants are the site name/domain in WordPress or PHP's internal system information. Constants are differentiated from regular variables by not using a dollar sign at the start of their name.
One final aspect to mention is arrays. An array is a variable with more than one value assigned to it. Again, this is a concept that is present in other popular scripting/programming languages. I'll leave more explanation of arrays for when I'm actually working with them.
Obviously, this is a very basic explanation of variables. I'll talk more about them as I go over working through the exercises and projects I'm working on to learn PHP.
Related posts:
