PHP Decision-Making - Operator Concepts
(Page 2 of 4 )
PHP has many types of operators, including:
- Arithmetic operators
- Array operators
- Assignment operators
- Bitwise operators
- Comparison operators
- Execution operators
- Incrementing/decrementing operators
- Logical operators
- String operators
The operators are listed as found on http://www.php.net/manual/en/language.operators.php. There are some operators we're not going to discuss so you can get up and running with PHP as quickly as possible. These include some of the casting operators that we'll just skim the surface of for now. Each operator has four critical properties in addition to its core functionality:
- Number of operands
- Type of operands
- Order of precedence
- Operator associativity
The easiest place to start is by talking about the operands.
Number of Operands Different operands take different numbers of operands. Many operators are used to combine two expressions into a more complex single expression; these are called binary operators. Binary operators include addition, subtraction, multiplication, and division.
Other operators take only one operand; these are called unary operators. Think of the negation operator (-) that multiplies a numeric value by -1. The preincrement and predecrement operators described in Chapter 3 are also unary operators.
A ternary operator takes three operands. The shorthand for an if statement, which we'll talk about later when discussing conditionals, takes three operands.
Types of Operands You need to be mindful of the type of operand on which an operator is meant to work because certain operators expect their operands to be of particular data types. PHP attempts to make your life as easy as possible by automatically converting operands to the data type that an operator is expecting. There are times, however, that an automatic conversion isn't possible.
Mathematical operators are an example of where you need to be careful with your types. They take only numbers as operands. For example, when you try to multiply two strings, PHP can convert the strings to numbers. While "Becker" * "Furniture" is not a valid expression, it returns zero. On the other hand, an expression that is converted without an error is
"70" * "80". This evaluates to 5600 . Although 70 and 80 are strings, PHP is able to convert them to the number type required by the mathematical operator.
There will be times when you want to explicitly set or convert a variable's type. There are two ways to do this in PHP: first, by using settype to actually change the data type; or second, by casting, which temporarily converts the value. PHP uses casting to convert data types. When PHP does the casting for you automatically, it's called implicit casting. You can also specify data types explicitly, but it's not something that you'll likely need to do.
PHP uses implicit casting to cast to the type that the operator requires.
The cast types allowed are:
(int), (integer)
Cast to integer, whole numbers without a decimal part.
(bool), (boolean)
Cast to Boolean.
(float), (double), (real)
Cast to float, numbers that may include a decimal
part.
(string)
Cast to string.
(array)
Cast to array.
(object)
Cast to object.
To use a cast, place it before the variable to cast, as shown in Example 4-2. The $test_string variable contains the string 1234.
Example 4-2. Casting a variable
$test=1234;
$test_string = (string)$test;
Keep in mind that it may not always be obvious what will happen when casting between certain types. You might run into problems if you don't watch yourself when manipulating variable types.
Some binary operators, such as the assignment operators, have further restrictions on the lefthand operand. Because the assignment operator is assigning a value to the lefthand operator, it must be something that can take a value, such as a variable. Example 4-3 demonstrates good and bad lefthand expressions.
Example 4-3. Lefthand expressions
3 = $locations; // bad - a value cannot be assigned to the literal 3
$a + $b = $c; //bad - the expression on the left isn't one variable
$c = $a + $b; //OK
$stores = "Becker"." "."Furniture"; // OK
There is a simpler way to remember this. The lefthand expression in assignment operations is known as an
L-value. L-values in PHP are variables, elements of an array, and object properties. Don't worry about object properties.
Next: Order of precedence >>
More Programming Basics Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Learning PHP and MySQL, Second Edition, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Check it out today at your favorite bookstore. Buy this book now.
|
|