4.2. Operator ConceptsPHP has many types of operators. The categories
The operators are listed as found on http://www.zend.com/manual/language.operators.php. There are some operators we're not going to discuss in order for you to 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. When working with operators, there are four aspects that are critical:
The easiest place to start is by talking about the operands. 4.2.1. Number of OperandsDepending on which operator you are using, it may take different numbers of operands. Many operators are used to combine two expressions into a more complex single expression; these are called binary Other operators take only one operand; these are called unary A ternary 4.2.2. Types of OperandsYou need to be mindful of the type of operand an operator is meant to work on, because certain operators expect their operands to be 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. But there are times 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. Because the strings don't contain simple numbers, an expression that is converted without an error is "70" * "80". This outputs 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 PHPfirst, 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.
The cast types allowed are:
To use a cast, place it before the variable to cast, as in Example 4-2. The $test_string variable contains the string "1234". Example 4-2. Casting a variable
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 Example 4-3. Lefthand expressions
4.2.2.1. Order of precedenceThe order of precedence If the operators have the same precedence, they are processed in the order they appear in the expression. For example, multiplication and division process in the order they appear in an expression, because they have the same precedence. Operators with the same precedence can occur in any order without affecting the result. Most expressions do not have more than one operator of the same precedence level, or the order in which they process doesn't change the result. As shown in Example 4-4, when adding and subtracting, it doesn't matter whether you add or subtract firstthe result is still 1. Example 4-4. Order of precedence
When using expressions that contain operators of different precedence levels, the order can change the value of the expression. You can use parentheses, ( and ), to override the precedence levels or just to make the expression easier to read. Example 4-5 shows how to change the default precedence. Example 4-5. The multiplication is done last because of the override
PHP has several levels of precedence, enough so that it's difficult to keep track of them without checking a reference. Table 4-2 is a list of operators in PHP sorted by order of precedence from highest to lowest. Operators with the same level number are all of the same precedence.
4.2.2.2. AssociativityAll operators process their operators in a certain direction. This direction is called associativity, Since it has right associativity, the assignment operator is one of the exceptions, since it has right associativity. The expression $a=$b=$c processes by $b being assigned the value of $c, and then $a being assigned the value of $b. This assigns the same value to all of the variables. If the assignment operator is right associative, the variables might not have the same value. If you're thinking that this is incredibly complicated, don't worry. These rules are only enforced if you fail to be explicit about your instructions. Keep in mind that you should always use brackets in your expressions to make your actual meaning very clear. This helps both PHP and also other people who may need to read your code. 4.2.3. Relational OperatorsIn Chapter 3, we discussed assignment and math operators. Relational operators provide the ability to compare two operands and return either trUE or FALSE regarding the comparison. An expression that returns only TRUE or FALSE is called a Boolean expression, which we discussed earlier in this chapter. These comparisons include tests for equality 4.2.3.1. EqualityThe equality operator, If the two operands are equal, TRUE is returned; otherwise, FALSE is returned. If you're echoing your results, TRUE is printed as 1 in your browser. FALSE is 0, which won't display in your browser. It's a simple construct but it also allows you to test for conditions. If the operands are of different types, PHP attempts to convert them before comparing. For example, '1' == 1 is true. Also, $a == 1 is true if the variable $a is assigned to 1. If you don't want the equals operator to automatically convert types, you can use the identity operator, a triple equals sign ===, which checks that the values and types are the same. For example, '1' === 1 is false because they're different types, since a string doesn't equal an integer. Sometimes you might want to check to see whether two things are different. The inequality operator, an exclamation mark before the equals sign (!=), checks for the opposite of equality, which means not equal to.
4.2.3.2. Comparison operatorsYou may need to check for more than just equality. Comparison operators test the relationship between two values. You may be familiar with these from high school math. They include less than (<), less than or equal to (<=), greater than (>), and greater than or equal to (>=). For example, 3<4 is TRUE, while 3<3 is FALSE but 3<=3 is TRUE. Comparison operators are often used to check for something happening up until a set point. For example, a web store might offer free shipping if you purchase five or more items. So the code must compare the number of items to the number five before changing the shipping cost. 4.2.3.3. Logical operatorsLogical operators work with the Boolean results of relational operators to build more complex logical
To test whether both operands are true, use the AND operator, also represented as double ampersands (&&). trUE is returned only if both operands are trUE; otherwise, FALSE is returned. See Table 4-3 for more information. To test whether one operand is trUE, use the OR operator, which is also represented as double vertical bars (||). trUE is returned only if either or both operands are trUE.
To test whether either operand is trUE but not both, use XOR. XOR returns trUE if one and only one operand is TRUE. To negate a Boolean value, use the NOT operator represented as an exclamation point (!). It returns trUE if the operand has a value of FALSE. It returns FALSE if the operand is trUE.
Because they have different precedence levels, AND and OR have two representations. Table 4-4 displays logical statements and their results.
|
Wednesday, October 21, 2009
Section 4.2. Operator Concepts
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment