Operators Flashcards
What is an operator?
Something that takes one or more values or expressions and yields another value, so that the construction itself becomes an expression.
How are operators generally grouped?
According to the number of values they take. Unary operators take one value (! and ++). Most PHP operators are binary. There is just one ternary operator.
When operators have equal precedence, what decides how they are grouped?
Their associativity. For example, the minus sign is left-associative, so 1-2-3 is grouped as (1-2)-3. Equals is right-associative, so $a = $b = $c is grouped as $a = ($b = $c).
What does 1 < 2 > 1 evaluate to?
It’s illegal in PHP, because operators of equal precedence that are non-associative cannot be used next to each other.
What are the non-associative operators?
clone, new, instanceof
++, –
< <= > >= <>
==, !=, ===, !==
Generally, does PHP specify in which order an expression is evaluated?
No. Code that assumes a specific order of evaluation should be avoided, because the behavior can change depending on the version of PHP, or depending on the surrounding code.
What are the right-associative operators?
** ++ –
~
(int) (float) (string) (array) (object) (bool)
@ !
= += -= *= **= /= .= %= &= |= ^= >= =>
Review operator precedence here…
http://php.net/manual/en/language.operators.precedence.php
What is the ** operator?
Exponentiation.
What does the division operator return?
A float, unless the two operands are integers (or strings that get converted to integers) or the numbers are evenly divisible, in which case an integer will be returned.
Operands of modulus are converted to ____ before processing.
Integers (by stripping the decimal part).
What sign does the result of the modulus operator have?
The same sign as the dividend. $a % $b will have the same sign as $a.
What is the value of: $a = 3 ?
3
What is the precedence of the => operator?
The same as other assignment operators.
Are objects assigned by value or reference?
Reference in PHP 5. They may be explicitly copied using the “clone” keyword.
How can you explicitly assign by reference?
$var = &$othervar
What does the “new” operator return?
It automatically returns a reference.
What happens if you assign the result of new by reference? $a = &new $b
It results in an E_DEPRECATED message in PHP 5.3 and later.
What are the bitwise operators in PHP?
& And
! Or
^ Xor
~ Not
> Shift right
What happens to bits shifted off either end with the shift left or shift right operators?
They’re discarded. Left shifts have zeros shifted in on the right while the sign bit is shifted out on the left, meaning the sign of an operand is not preserved. Right shifts have copies of the sign bit shifted in on the left, meaning the sign of an operand is preserved.
What happens if both operands for the &, |, and ^ operators are strings?
The operation will be performed on the ASCII values of the characters that make up the strings, and the result will be a string. In all other cases, both operands will be converted to integers and the result will be an integer.
Both operands and the result for the bitshift operators are always treated as ____.
Integers.
What happens if the operand for the ~ operator is a string?
The operation will be performed on the ASCII values of the characters that make up the string, and the result will be a string, otherwise the operand and the result will be treated as integers.
What would you use to show all errors except for notices?
E_ALL & ~E_NOTICE
Can also use: E_ALL ^ E_NOTICE
How is a negative number represented in 64-bit?
The leftmost bit is set to 1, and the integer represented by that bit is negative. So, -1 would be the leftmost bit set to 1, and then every other bit set to 1.
What happens when you shift integers by values greater than or equal to the system long integer width?
It results in undefined behavior. In other words, don’t shift more than 31 bits on a 32-bit system, and don’t shift more than 63 bits on a 64-bit system.
How can you perform bitwise manipulation on numbers beyond PHP_INT_MAX?
Use functions from the gmp extension.