Lesson 3: Introducing Operators - TOP10

Lesson 3: Introducing Operators

Operators are symbols that make it possible to use one or more values to produce a new value. A value that is operated on by an operator is referred to as an operand.  
An operand is a value used in conjunction with an operator. There are usually two operands to one operator.  
Let's combine two operands with an operator to produce a new value:  4 + 5 4 and 5 are operands. They are operated on by the addition operator (+) to produce 9. Operators almost always sit between two operands. The combination of operands with an operator to produce a result is called an expression.  
1.  Assignment Operator( = )  You’ve already seen an assignment operator at work: The equals sign is the basic assignment operator. Burn this into your brain: = does not mean “equal to” Instead, == (two equals signs) means “equal to,” and the single = means “is assigned to”. Example: $x = 10; // the variable $x now contains the number 10.  
2.  Arithmetic Operators Arithmetic operators simply perform basic mathematical tasks. Take a look at Table below:  


Example:  <?php   $x = 10;   $y = 20;   $sum = $x + $y;   echo $sum; // output: 30  ?>  
3.   Concatenation Operator The concatenation operator is represented by a single period. Treating both operands as strings, it appends the right-hand operand to the left. So "PHP"." Programming" returns "PHP Programming" Regardless of the data types of the operands, they are treated as strings, and the result is always a string. Example:  <?php   $x = 2.82;   print "the value of variable x is ". (int)$x . "<br />"; 

 // output: the value of variable x is 2  ?>  
4.  Combined Assignment Operators Although there is really only one assignment operator, PHP provides a number of combination operators that transform the left-hand operand and return a result. A combined assignment operator consists of a standard operator symbol followed by an equals sign. Combination assignment operators save you the trouble of using two operators yourself. For example $x = 5; $x = $x + 8; // $x now equals 13  
may instead be written as  
$x = 5; $x + = 8; // $x now equals 13  
There is an assignment operator for each of the arithmetic operators and one for the concatenation operator. The following table lists some of the most common. 


Each of the examples in the above Table transforms the value of $x using the value of the right-hand operand. 5. Comparison Operators Comparison operators perform tests on their operands. They return the Boolean value true if the test is successful or false otherwise. This type of expression is useful in control structures, such as if and while statements. To test whether the value contained in $x is smaller than 6, for example, you can use the less-than operator: $x < 6 If $x contains the value 4, this expression has the value true. If $x contains 8, the expression resolves to false. The following table lists the comparison operators



6. Logical Operators The logical operators test combinations of Booleans. For example, the or operator, which is indicated by two pipe characters (||) or simply the word or, returns true if either the left or the right operand is true: true || false This expression returns true. The and operator, which is indicated by two ampersand characters (&&) or simply the word and, only returns true if both the left and right operands are true: true && false This expression returns false. It's unlikely that you will use a logical operator to test Boolean constants, however. It makes more sense to test two or more expressions that resolve to a Boolean. For example ($x > 2) && ($x < 15) Returns true if $x contains a value that is greater than 2 and smaller than 15. We include the parentheses to make the code easier to read. The following table lists the logical operators. 


7. Operator Precedence The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18. If operator precedence is equal, left to right associativity is used. The following is a list of the operators precedence with the highest-precedence operators listed at the top of the table. Operators on the same line have equal precedence, in which case their associativity decides which order to evaluate them in. ++, --, (cast) /, *, % +, - <, <=, >=, > ==, ===, != && || =, +=, -=, /=, *=, %=, .= and xor or As you can see, or has a lower precedence than || and and has a lower precedence than &&, so you can use the lower-precedence logical operators to change the way a complex test expression is read. This is not necessarily a good idea. The following two expressions are equivalent, but the second is much easier to read: $x and $y || $z $x && ($y || $z) Taking it one step further, the following example is easier still: $x and ($y or $z) The three examples are all equivalent. The order of precedence is the only reason that both && and and are present in PHP. The same is true of || and or. In most, if not all circumstances, however, use of parentheses will make for clearer code and fewer bugs than code that takes advantage of the difference in precedence of these operators. Throughout this book, we will tend to use the more common || and && operators.


Previous
Next Post »