Operators Flashcards

1
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
2
Q

What are the right associative operators?

A

**
!
++ – ~ (int) (float) (string) (array) (object) (bool) @
= += -= *= **= /= .= %= &= |= ^= >= =>

All of the others are either non-associative or left associative.

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

What is the ** operator?

A

Exponentiation.

2 ** 3 = 8

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

What does the division operator return?

A

The division operator (“/”) returns a float value unless the two operands are integers (or strings that get converted to integers) and the numbers are evenly divisible, in which case an integer value will be returned.

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

How are modulus operands handled?

A

Operands of modulus are converted to integers (by stripping the decimal part) before processing.

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

What is the sign of the result of a modulus operator?

A

The result of the modulus operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a.

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

What is the value of $a = 3?

A

3

The value of an assignment operator is the value assigned.

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

Are objects assigned by value or reference?

A

Reference. However, they may be explicitly copied using the ‘clone’ keyword.

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

Are objects assigned by value or reference?

A

Reference. However, they may be explicitly copied using the ‘clone’ keyword.

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

What happens when you assign the result of new by reference, like this:

class C{}
$o = &new C;
A

It results in an E_DEPRECATED error message, because the new operator returns a reference automatically.

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

What happens when you assign the result of new by reference, like this:

class C{}
$o = &new C;
A

It results in an E_DEPRECATED error message, because the new operator returns a reference automatically.

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

What are these bitwise operators?

$a & $b
$a | $b
$a ^ $b
~ $a
$a > $b
A

$a & $b And Bits that are set in both $a and $b are set.
$a | $b Or (inclusive or) Bits that are set in either $a or $b are set.
$a ^ $b Xor (exclusive or) Bits that are set in $a or $b but not both are set.
~ $a Not Bits that are set in $a are not set, and vice versa.
$a > $b Shift right Shift the bits of $a $b steps to the right (each step means “divide by two”)

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

What happens to bits shifted off either end with bitwise operators?

A

They are 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
14
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
15
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
16
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
17
Q

How are operands and the result for&raquo_space; and

A

They are always treated as integers.

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

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

What is the operator?

A

Not equal. Same as: !

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

What are the === and !== operators?

A

Identical (equal and of the same type) and not identical.

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

What is the “” operator?

A

Spaceship operator.

0 if $a and $b are equal, 1 if $a is greater than $b, -1 if $b is greater than $a.

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

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

24
Q

What happens when a null is compared with a string?

A

The null is converted to “”.

25
Q

What happens when a boolean is compared to anything?

A

Both sides are converted to boolean.

26
Q

What happens when an object is compared with an object?

A

Built-in classes can define its own comparison, different classes are uncomparable, same class - compare properties the same way as arrays.

27
Q

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

A

Strings and resources are translated to numbers.

28
Q

What happens when two arrays are compared?

A

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.

29
Q

What happens when an object is compared with a non-object?

A

The object is always greater.

30
Q

What happens when an array is compared with a non-array?

A

The array is always greater.

31
Q

What happens when an array is compared with a non-array?

A

The array is always greater.

32
Q

Can the middle part of the ternary operator be omitted?

A

Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

33
Q

Can the middle part of the ternary operator be omitted?

A

Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

34
Q

What does the ternary operator evaluate to?

A

It evaluates to the result of an expression.

35
Q

In what direction are ternary operators evaluated?

A

Left to right.

36
Q

What is the error control operator?

A

@

When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. It will even disable error reporting for critical errors that will terminate script execution.

37
Q

What does set_error_handler() do?

A

It allows you to set a custom error handler.

38
Q

What is the track_errors feature?

A

When enabled, any error message generated by the expression will be saved in the variable $php_errormsg. This variable will be overwritten on each error.

39
Q

The @ operator only works on…

A

…expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it.

40
Q

What is the execution operator?

A

Backticks. PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned.

41
Q

What is the one execution operator that PHP supports?

A

Backticks. PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned. The backticks are identical to shell_exec().

42
Q

When is the backtick operator disabled?

A

When safe mode is enabled or shell_exec() is disabled.

43
Q

Can backticks be used within double quoted strings?

A

No.

44
Q

Can character variables be incremented or decremented?

A

Character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.

45
Q

Can character variables be incremented or decremented?

A

Character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.

46
Q

What happens when you increment or decrement booleans?

A

It has no effect.

47
Q

What happens when you increment or decrement booleans?

A

It has no effect.

48
Q

What is the xor operator?

A

$a xor $b

TRUE if either $a or $b is TRUE, but not both.

49
Q

What does it mean that the logical operators are short-circuit?

A

The left side is analyzed, and if the condition can’t be met, the right side isn’t analyzed. For example:

//foo() will never get called
$a = (false && foo())
50
Q

What is the difference between || and or?

A

|| has greater precedence.

51
Q

What is the difference between || and or?

A

|| has greater precedence.

52
Q

What are the two string operators?

A

The concatenation operator: .

The concatenating assignment operator: .=

53
Q

What is the array union operator?

A

+
$a + $b; //returns the union of $a and $b; 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.

54
Q

What is the array equality operator?

A

$a == $b

True if $a and $b have the same key/value pairs.

55
Q

What is the array identity operator?

A

$a === $b

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

56
Q

What is the instanceof type operator?

A

It’s used to determine whether a PHP variable is an instantiated object of a certain class, or an instantiated object of a class that inherits from a parent class, or of a class that implements an interface. It returns true or false

57
Q

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

A

It returns FALSE and does not throw an error. Constants, however, are not allowed.