Operators Flashcards

1
Q

What is an operator?

A

Something that takes one or more values or expressions and yields another value, so that the construction itself becomes an expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are operators generally grouped?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When operators have equal precedence, what decides how they are grouped?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does 1 < 2 > 1 evaluate to?

A

It’s illegal in PHP, because operators of equal precedence that are non-associative cannot be used next to each other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the non-associative operators?

A

clone, new, instanceof

++, –

< <= > >= <>

==, !=, ===, !==

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Generally, does PHP specify in which order an expression is evaluated?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the right-associative operators?

A

** ++ –

~

(int) (float) (string) (array) (object) (bool)

@ !

= += -= *= **= /= .= %= &= |= ^= >= =>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Review operator precedence here…

A

http://php.net/manual/en/language.operators.precedence.php

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the ** operator?

A

Exponentiation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the division operator return?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Operands of modulus are converted to ____ before processing.

A

Integers (by stripping the decimal part).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What sign does the result of the modulus operator have?

A

The same sign as the dividend. $a % $b will have the same sign as $a.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the value of: $a = 3 ?

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the precedence of the => operator?

A

The same as other assignment operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Are objects assigned by value or reference?

A

Reference in PHP 5. They may be explicitly copied using the “clone” keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you explicitly assign by reference?

A

$var = &$othervar

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does the “new” operator return?

A

It automatically returns a reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What happens if you assign the result of new by reference? $a = &new $b

A

It results in an E_DEPRECATED message in PHP 5.3 and later.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are the bitwise operators in PHP?

A

& And

! Or

^ Xor

~ Not

> Shift right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What happens to bits shifted off either end with the shift left or shift right operators?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What happens if both operands for the &, |, and ^ operators are strings?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Both operands and the result for the bitshift operators are always treated as ____.

A

Integers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What happens if the operand for the ~ operator is a string?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What would you use to show all errors except for notices?

A

E_ALL & ~E_NOTICE

Can also use: E_ALL ^ E_NOTICE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How is a negative number represented in 64-bit?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What happens when you shift integers by values greater than or equal to the system long integer width?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How can you perform bitwise manipulation on numbers beyond PHP_INT_MAX?

A

Use functions from the gmp extension.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Know how to convert between decimal, binary, octal, and hexadecimal.

A

29
Q

What is type juggling?

A

PHP does not require explicit type definition; a variable’s type is determined by the context in which the variable is used.

30
Q

What happens if you compare a number with a string, or the comparison involves numerical strings?

A

Each string is converted to a number, and the comparison is performed numerically. These rules also apply to the switch statement. Type conversion does not take place when the comparison operator involves comparing the type (=== or !==).

31
Q

When NULL is compared with a string, to what is NULL converted?

A

The empty string: “”

32
Q

When a boolean or NULL is compared with anything else, what happens?

A

Both sides are converted to boolean; FALSE

33
Q

What happens when a string/resource/number is compared with a string/resource/number?

A

Strings and resources are translated to numbers.

34
Q

What happens when two arrays are compared?

A

The array with fewer members is smaller. If key from operand 1 is not found in operand 2, then arrays are uncomparable, otherwise compare value by value.

35
Q

When comparing, objects and arrays are generally _____ than other operands.

A

greater

36
Q

Can you test two floats for equality?

A

No.

37
Q

What is the truncated ternary operator?

A

expr1 ?: expr3

This returns expr1 if expr1 evaluates to TRUE. Otherwise, it returns expr3.

38
Q

To what does the ternary operator evaluate?

A

To the result of an expression. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.

39
Q

Can you stack ternary operators?

A

Yes, but it’s not recommended, because the results are non-obvious.

40
Q

Are ternary operators evaluated left-to-right or right-to-left?

A

Left to right.

41
Q

If you set a custom error handler with set_error_handler(), will it still get called if you use the @ error suppression operator?

A

Yes.

42
Q

What is the $php_errormsg variable?

A

If track_errors is enabled, the last error message (even suppressed ones) will be saved in $php_errormsg.

43
Q

The @ operator only works on…

A

…expressions. If you can take the value of something, you can prepend the @ operator to it (variables, function and include calls, constants, etc.). You can’t prepend it to function or class definitions, or conditional structures such as if and foreach.

44
Q

What happens if you use the @ operator to suppress a critical error that will terminate script execution?

A

The script will die right there with no indication as to why.

45
Q

What are the execution operators?

A

Backticks, which are identical to shell_exec().

46
Q

When is the backtick operator disabled?

A

When safe_mode is enabled, or shell_exec() is disabled.

47
Q

What does a backtick mean in a double-quoted string?

A

It has no special meaning.

48
Q

What happens when NULL is incremented or decremented?

A

Decrementing has no effect; incrementing results in 1.

49
Q

What types does incrementing or decrementing affect?

A

Only numbers and strings. Arrays, objects, and resources are not affected.

50
Q

How does the placement of increment/decrement operators work?

A

The operators increment/decrement, and return, depending on operator placement. ++$a //increments $a by one, then returns it $a++ // returns $a, then increments it by one

51
Q

Which character variables can be incremented and decremented?

A

None can be decremented. Only a-z,A-Z,0-9 can be incremented. When you try to increment/decrement others, it has no effect.

52
Q

What happens when booleans are incremented or decremented?

A

No effect.

53
Q

What is the output of: $a = ‘W’; print ++$a;

A

X

54
Q

What is the output of: $a = ‘Z’; print ++$a;

A

AA

55
Q

What is the difference between the ‘&&’ and ‘and’ operators?

A

They have different precedence.

56
Q

What is the xor logical comparison operator?

A

Exclusive or. True if either $a or $b is true, but not both.

57
Q

Why will foo() not be called? $a = (false && foo());

A

Because the logical comparison operators are short-circuit.

58
Q

What do PHP’s boolean operators always return?

A

A boolean value.

59
Q

What are the two string operators?

A

concatenation operator: . concatenating assignment operator: .=

60
Q

What happens when you attempt to add numbers with a concatenation operator?

A

Your result will be the result of adding those numbers as strings.

61
Q

How do the equal and identical operators work on arrays?

A

$a == $b //true if $a and $b have the same key/value pairs. $a === $b //true if $a and $b have the same key/value pairs in the same order and of the same types

62
Q

What does the + operator return when used on two arrays: $a + $b

A

The right-hand array appended to the left-hand array. For keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

63
Q

What is the instanceof operator?

A

It’s used to determine whether a PHP variable is an instantiated object of a certain class.

64
Q

What are two additional uses of the instanceof operator?

A

It can be used to determine whether a variable is an instantiated object of a class that inherits from a parent class. It can also be used to determine whether a variable is an instantiated object of a class that implements an interface. interface MyInterface { } class MyClass implements MyInterface { } $a = new MyClass; var_dump($a instanceof MyClass); //true var_dump($a instanceof MyInterface); //true

65
Q

Can instanceof be used with something besides a literal classname?

A

Yes, it can be used with another object or a string variable.

66
Q

What does instanceof return if the variable being tested is not an object?

A

No error; it simply returns FALSE.

67
Q

When does instanceof throw an error?

A

When it’s given a constant instead of an object instance.

68
Q

Which function is similar to instanceof?

A

is_a()