Skip to content

Latest commit

 

History

History
374 lines (215 loc) · 14.6 KB

The Zend Certification-02.md

File metadata and controls

374 lines (215 loc) · 14.6 KB

Operators

Arithmetic

You should recognize the arithmetic functions:

Operation Example Description
Addition 1 + 2.3
Subtraction 4 – 5
Division 6 / 7
Multiplication 8 * 9
Modulus 10 % 11 Gives the remainder of dividing 10 by 11
Power 12 ** 13 Raises 12 to the power of 13

These arithmetic operators take two arguments and so are called binary.

The unary operators following take only one argument and their placement before or after the variable changes how they work. There are two unary operators in PHP, namely prefix and postfix. They are named for whether the operator appears before or after the variable that it affects.

  • If the operator appears before the variable (prefix), then the interpreter will first evaluate it and then return the changed variable.
  • If the operator appears after the variable (postfix), then the interpreter will return the variable as it was before the statement executed and then increment the variable.

Let’s show their effects on a variable $a that we initialize to 1 and then operate on:

Command Output Value of $a Afterwards Description
$a = 1; 1
echo $a++; 1 2 Postfix
echo ++$a; 3 3 Prefix
echo $a--; 3 2 Postfix
echo --$a; 1 1 Prefix

Logic Operators

PHP uses both symbol and word form logic operators. The symbol form operators are C-based.

Operator Example True When
and $a and $b Both $a and $b evaluate true
and $a && $b
or $a or $b Either $a or $b evaluate true
or $a
xor $a xor $b One of (but not both) $a or $b is True
xor $a ^ $b
not !$a $a is not true (false)

It is best practice not to mix the word form (e.g., and) and the symbol (e.g., &&) in the same comparison, as the operators have different precedence. It’s safest to stick to using the symbol form exclusively.

In this example, we see that operator precedence[14](file:///home/antonio/Escritorio/PHP 7 Zend Certification Study Guide - Andrew Beak.htmlz_FILES/index.html#calibre_link-529) results in the variables $truth and $pravda not being the same even though we’re performing the “same” logical operator to derive them.

This happens because the logical operators and and or have lower priority than the equality operator =.

0 | 1 | | 1 <=> 1 | 0 | | 1 <=> 2 | -1| | 'apples' <=> 'Bananas' | 1 | | 'Apples' <=> 'bananas' | -1| The spaceship operator uses the standard PHP comparison rules. We’ll see why there is this surprising difference in the string comparison in the section on “Strings” later. ### Bitwise ​Bitwise operators work on the bits of integers represented in binary form. Using them on a different variable type will cause PHP to cast the variable to integer before operating on it. There are three standard logical bitwise operators: | Operator | Operation | Description| | -------- | ----------- | ------------------------------------------------------------ | | & | Bitwise AND | The result will have a bit set if both of the operands bits were set | | \| | Bitwise OR | If one or both of the operands have a bit set then the result will have that bit set | | ^ | Bitwise XOR | If one and only one of the operands (not both) has the bit set then the result will have the bit set. | The result of a bitwise operator will be the value that has its bits set according to these rules. In other words, the bit in each position of the operands is evaluated against the corresponding bit in the same position of the other operand. It’seasier to consider the binary representations of numbers when using these operators. You can calculate the binary representation of the result by comparing bits (from right to left) and then converting to decimal when you’re done. Let’s look at 50 & 25 as an example. I’ve put the binary representations in comments in the three rows. You can see that I calculated the binary representation of $c by checking whether the bit in that position is set in $a and in $b. In this case, only one such bit is true in both positions. > 1 | Shift Right | 00011001 | 25 | | 50 << 1 | Shift Left | 01100100 | 100 | I’ve included enough leading zeroes in the binary forms to make it easier to see the pattern of what’s happening. You can see that when we shifted to the right, the right-hand bit was“lost”. When we shift left, we insert new bits that are set to 0 on the right. It’s important to be cautious when using bitwise operations to perform calculations, as the integer overflow size may vary between the different environments that PHP is deployed on. For example, although a 64-bit system will have the same result for both operations, on a 32-bit integer system they will not: | Greater than | | >= | Greater than or equal to | | < | Less than | | <= | Less than or equal to | | <> | Not equal | | == | Equivalence; values are equivalent if cast to the same variable type | | ===| Identity; values must be of the same data type and have the same value | | != | Not equivalent | | !==| Not identical | It is important to understand the difference between an equivalent comparison and an identity comparison: - Operands are equivalent if they can be cast to a common data type and have the same value. - Operands are identical if they share the same data type and have the same value. Arrays are equivalent if they have the same key and value pairs. They are identical if they have the same key and value pairs, in the same order, and the key-value are of the same type. When using comparison operators on arrays, the count of their keys is used to determine which is greater or lesser. When compared to a scalar variable, both an object and an array will be considered greater than the scalar. $b; // 1 Be careful when using comparison operators on strings, or when using them on mismatching variable types. See the section on “Casting Variables” for more information. ### Two More Operators PHP provides an operator to suppress error messages. This will work only if the library that the function is based on uses PHP standard error reporting.