Working with Data Types and Operators
Monday, December 15th, 2008You should be quite familiar with variables at this point in your programming education and recall that variable data types determine what types of operations it supports along with the amount of memory which is allocated to support it.
For example, a Boolean variable typically would not be used to store a user name or password. Its values are binary and these values are either true (1) or false (0). You would use a Boolean variable to determine whether something happened (or not) or whether something is on (true) or off (false).
Another example of a data type is an array. Arrays, as you should recall, are great for collecting data through the use of a single variable for related items. For example, let’s say you were creating a list of automobile models. You could create an array that stores each model for Ford trucks and then refer to this one variable, along with its index rather than defining a named variable for each one. Don’t forget that an array’s index begins with zero (0) and that this identifies the first item in the array. The array is an example of a compound data type. This means that it can hold more than one value. The other type of compound data type is the object, which you worked with in VB (and can be used in PHP).
You’ve spent a lot of time working with what are called primitive data types. A primitive data type is a variable that can hold one value. Examples are strings, integers, floating point numbers, and Booleans.
In PHP, the main things to remember are that variable names (referred to as identifiers) must begin with a dollar sign and that variable names cannot contain any spaces. While some use underscores to separate words in a variable name, I personally don’t like doing that. I prefer to use something like $LastName rather than $last_name because to me the underscore takes longer. But, that’s just me. You can use whichever you like.
A nice online review of PHP data types can be found at: http://www.daaq.net/old/php/index.php?page=php+data+types&parent=php+basics.
Assignment operators (+=, -+, *=, /=, =), comparisons, logical operators (AND, OR, XOR), and math operators work the same in PHP as they do in VB. One operator does need an explanation though, and it is the modulus. The modulus operation differs from simple division and this is easy to see through the use of a math calculation. If we divide 7/3 we get 2.3333333333 (it goes on forever). The modulus is the remainder (.3333333333).
Type casting, or casting, ensures that a data type is the kind a script expects. This is helpful for form input, verifying that data is indeed of the kind the script needs to execute. You can use the gettype() function to view a variable’s data type, and other functions to determine whether a variable is of a specific data type.
It’s probably a good time to review the operator order of precedence. In other words, when you have a math formula such as 7 x (8 + 3) / 4 x 2^3, what is done first? How do we always know the correct answer? I always remember the order in which operations are evaluated by using the sentence: Please Excuse My Dear Aunt Sally. Taking the first letter of each word, we can gleam the order. Here’s the answer: Parentheses, Exponents, Multiplication, Division, Addition, and Subtraction. So, you always add before you subtract, perform calculations inside parentheses before doing anything else, multiply before adding, etc.
Feel free to drop me an email if you’d like the answer to the problem above.