PHP Set 2 Flashcards

1
Q

What is the difference between the ‘==’ and the ‘===’ comparison operators?

A

== is true iff expr1 is equal to expr2 after type juggling
=== is true iff expr1 is equal to expr2 and they are of the same type

Ex: “1.23e2” == 123 is true but “1.23e2” === 123 is false

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

What is the difference between the != and !== comparison operators?

A

!= is true iff expr1 is not equal to expr2 after type juggling
!== is true iff expr1 is not equal to expr2 or they are not of the same type

Ex: “123” != 123 is false but “123” !== 123 is true

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

Do the < and > comparison operators perform type juggling?

A

Yes, <, <=, >, and >= all perform type juggling before being evaluated

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

How can strings be compared in PHP?

A

Using strcmp(expr1, expr2)
Using the three-way comparison operator only available in PHP 7

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

How does the three-way comparison operator work?

A

expr1 <=> expr2
Returns -1 if expr1 < expr2
Returns 1 if expr1 > expr2
Returns 0 if expr1 == expr2

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

What does strcmp(expr1, expr2) return?

A

< 0 if expr 1 < expr2
> 0 if expr1 > expr2
0 if expr1 == expr2

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

What are the 3 functions available in PHP to test if a value is NAN or INF?

A

is_nan(value) returns TRUE if a value is NAN
is_infinite(value) returns TRUE if a value is INF or -INF
is_finite(value) returns true if a value is neither NAN nor INF/-INF

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

What are the rules for keys and values for an associative array in PHP?

A

Keys can be integers or strings and values can be of any type including arrays

Keys are not necessarily unique

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

What function gives you the size of an array?

A

count(array $value, int $mode)
The mode can be COUNT_NORMAL which it is by default or COUNT_RECURSIVE which is particularly useful for counting elements of a multidimensional array

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

How can you retrieve the keys and values of an array separately?

A

Using the array_keys method you can get the keys in number-indexed array form
Using the array_values method you can get the values in number-indexed array form

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

How can you check if a particular key exists within an array?

A

Using the array_key_exists(key, array) function

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

What is the syntax for a foreach loop?

A

foreach($array as $key=>$value) {
}
Note that $key=> is optional

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

How can you add elements to an array?

A

Using array_push($array, value1, value2, …) you can add one or more values onto the end of an array

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

How can you remove elemtns from an array?

A

Using array_pop($array) you can remove the last element from an array and return it

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

What happens if you try to use array_pop on an empty array or something that is not an array?

A

NULL will be returned

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

How can you remove an element from the front of an array?

A

Using array_shift($array) will remove the first element from the array and return it

17
Q

What happens if you try to use array_shift on an empty array or something that is not an array?

A

NULL will be returned

18
Q

How can you prepend elements to an array?

A

Using array_unshift($array, value1, value2,…) you can prepend one or more values to the beginning of the array

19
Q

What does array_unshift and array_push return?

A

Returns the new number of elements in the array

20
Q

What is variable scope?

A

The context in which a variable is defined and used