Operators

only for RuBoard - do not distribute or recompile

Operators

The types of operators are arithmetic, comparison, logical, execution, and string. Operators are typically defined by special characters .

If you don't use parentheses PHP will evaluate expressions left to right, unless operator precedence dictates a different evaluation order. Multiplication has a higher precedence than addition, and will be evaluated first. (In other words, a multiplication will precede an addition.) For example, 3 + 4 * 5 is equal to 23, not 35. If you use parentheses, the innermost set of parentheses is evaluated first, then the next innermost, and so forth, and then the normal order of precedence is used.

The following table gives the operator's precedence, with the highest priority item listed first in the table, left to right. The evaluation direction shows you where the parser first looks when evaluating, with neither indicating the statement is taken as a whole:

Operator Evaluation Direction
new neither
[ right
! ~ ++ -- (int) (double) (string) (array) (object) @ right
* / % left
+ - . left
<<= >>= == != neither
& ^ && ?: neither
= += -= *= /= .= %= &= != ~= <<= >>= right
print right
and xor or , left

Arithmetic Operators

Arithmetic operators are the operators used in algebraic expressions. These are the standard operators learned in math class, with slightly different symbols used. They are listed here:

  • + ” Addition

  • - ” Subtraction

  • * ” Multiplication

  • / ” Division

  • % ” Modulo (this returns the remainder of a division operation)

  • ++ ” Increment by one. This can show before or after a variable. If before a variable (++$a),the variable is incremented before it is used in an arithmetic expression. If after a variable ($a++), the variable is used in the arithmetic expression, and then its value is incremented.

  • -- ”Decrement by one. This can show before or after a variable. If before a variable (--$a),the variable is decremented before it is used in an arithmetic expression. If after a variable ($a--), the variable is used in the arithmetic expression, and then its value is decremented.

Comparison Operators

All comparison operators return a true or false answer to the code and are used in statements to control code execution. When used with the if statement, a true will cause the code immediately after the if to be executed; a false will cause the code after the else to be executed. The following table shows the groupings. If a result is not true, it is false:

Operator Name Result
$l == $r is equal to (=) true if $l is equal to $r
$l <= $r is less than true if $l is less than $r
$l <= $r is less than or equal to true if $l is less than or equal to $r
$l != $r is not equal to true if $l is not equal to $r
$l >= $r is greater than true if $l is greater than $r
$l >= $r is greater than or equal to true if $l is greater than or equal to $r

Execution Operators

Some operators that I classify as execution operators modify normal execution of code. In other words, they can cause PHP to execute script differently depending on current conditions.

  • @ ” Suppresses function error or warning. If a warning would have been printed by PHP, this will suppress the warning. The error_reporting() function can also be used.

  • ?: ” This is a logic comparison similar in effect to if (a) /*then*/ b else c; In the following statement, if $a is 5, then $b is set to 6, otherwise it is set to 7:

     $a == 5 ? $b = 6 : $b = 7 ; 
  • $funcName() ” Dynamic function call. If the value of $funcName evaluates to an existing function, that function is called. In the following code, the function write() is called:

     function write() { echo "hello"; } $miscFunc = "write"; $a = 3; if ($a == 3)    $miscFunc(); // write() is called and prints hello 

Assignment Operators

Assignment operators borrow heavily from the C language syntax. They can perform operations as the assignment occurs. The assignment operators are as follows :

Operator Operation
= Set left side to value of right side expression
+= Add right side to left side, set left to result
-= Subtract right side from left, set left to result
*= Multiply right side by left, set left to result
/= Divide left side by right, set left to result
%= Get remainder of left side divided by right, set left to result
&= Bitwise AND left and right, set left to result
= Bitwise OR left and right, set left to result
^= Bitwise XOR left and right, set left to result
.= Concatenate string on right to string on left
=> Assign Array Element Index values. (see Arrays section in this Appendix).

Logic Operators

Logic operators exist in two different forms. One form deals with bitwise operators. The other form deals with logical truth statements.

When you use bitwise operators, you convert the number you are dealing with to binary and whenever the 1 or 0 is in the exact same position in the two numbers , the bitwise operator has an effect.

For example, if you bitwise OR two the numbers 8 and 4, you get an answer in this fashion:

 1000  0100 = 1100 

If you do the math conversion you will find that the answer is 12. In many cases, adding numbers together appears to give you the result of an OR. But this is not quite true. The OR function does not carry binary numbers across, so 1 OR 1 is still one, not two. Each number column in binary is called a bit.

When using logical operators, always use the binary system to determine what will happen. The results of the logical operators are always calculated one bit at a time.

EXCURSION: A Quick Lesson on Number Systems

We count from 0 “9, with 10 being ten. That is why our number system is called a base ten system. In base eight (octal), you count from 0 “7, with 10 being eight. For base 2, you count from 0 “1, with 10 being 2. In base 16, it is 0 “F (15), with 10 being 16.

In each base, the first column on the left is ones. For example, in base 10, it is one, two, three, nine. In base 8, it is one, two, three, seven.

In each base, the numbers in the next column to the left have a value equal to that base times the column value to the right. For example, in base ten, 1 is equal to one, 10 is equal to ten times one, 100 is equal to ten times ten, 1000 is equal to ten times one hundred. In base eight (octal), 1 is one, 10 is eight times one, 100 is eight times eight, 1000 is eight times sixty four. In base 2 (binary), 1 is one, 10 is two times one, 100 is two times two, 1000 is two times four.

To convert a number from one base to another, divide by the largest number in the base you are going to that results in an integer answer greater than 0. Then take the remainder and repeat.

To convert 19 to binary, divide it by the largest power of two you can. I start by counting by the base: 1, 2, 4, 8, 16, 32. Okay, 16 is the largest number. 19 “16 = 3. That puts a 1 in the 16 spot. Now, for 8 and 4, we have zeros, because 3 is less than both. 3 “2 = 1. There is a 1 in the 2 spot, and a 1 in the 1 spot. Now we have the information needed to convert to binary. Let's put it together in a binary number: 10011. To put it together backwards , it is 1 times 1 plus 1 times 2 plus zero times 4 plus zero times 8 plus 1 times 16 equals 19.

The truth table for bitwise operations is shown in the following table. In the table, X means any value of 1 or 0 (we don't care). The -- indicates that only one side is used in the operation. The operator used is in parentheses:

Bit Operation Bit Result
1 OR ( ) X 1
OR ( )
1 AND ( & ) 1 1
AND ( & ) X
1 XOR ( ^ ) 1
1 XOR ( ^ ) 1
XOR ( ^ )
1 NOT ( ~ ) --
NOT ( ~ ) -- 1

Logical truth operators deal with the logical answer from an expression. Generally, if an expression evaluates to zero, it is false. If it evaluates to any other number, it is true. The following table gives examples and results of the logical truth operations. In all cases the result is false unless the conditions described are met:

Operation Description
$a and $b Returns true if both $a and $b are true
$a or $b Returns true if either $a or $b are true
$a xor $b Returns true if either $a or $b is true, but false if both $a and $b are true.
!$a Returns true if $a is false
$a && $b Returns true if both $a and $b are true
$a $b Returns true if either $a or $b are true

Note that the precedence of and and or is different from the precedence of && and .

Reference Operators

Reference operators are used by the programmer to make PHP look further before determining what to do. They are usually used on objects to reference the function or method within that object. The reference operator for objects is ->.

Assume you have an object named mydog with an attribute called color , and a method called wag_tail(). To set the color to red, and call the wag_tail() function within the object, you would write code like this:

 $mydog->color = "red"; $mydog->wag_tail(); 

The reference operator & is also used on variables within function calls. Normally, PHP makes a copy of a variable and gives the copy to the function call. Changes to that variable change only the copy. If you use the & operator, the variable is not copied . Any changes you make to the variable in the function are made to the variable that was listed in the function call. This is called passing arguments by reference. For example:

 $a = 4; $b = 3; function foo($c, $d) {   $c = 5;   $d = 6; }  foo($a,&$b); printf("a is %d\n", $a); // will print a is  4 printf("b is %d\n", $b)_; // will print b is 6 

NOTE

PHP 4 introduced assignment references. You can make a variable essentially become another name for another variable. The statement

 $a = &$b; 

causes $a and $b to point to the same variable. If you change $a, $b is changed, and vice versa. The variables $a and $b are identical in all respects.


String Operator

There is only one operator for a string. It is the concatenation operator. This operator is a period ( . ). Two strings are joined together with no intervening spaces when this operator is used:

 $a = "This is a test"; $b = "of joining two strings"; echo $a.$b; // this prints "This is a testof joining two strings" 
only for RuBoard - do not distribute or recompile


MySQL and PHP From Scratch
MySQL & PHP From Scratch
ISBN: 0789724405
EAN: 2147483647
Year: 1999
Pages: 93
Authors: Wade Maxfield

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net