PHP Set 2 Flashcards
What is the difference between the ‘==’ and the ‘===’ comparison operators?
== 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
What is the difference between the != and !== comparison operators?
!= 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
Do the < and > comparison operators perform type juggling?
Yes, <, <=, >, and >= all perform type juggling before being evaluated
How can strings be compared in PHP?
Using strcmp(expr1, expr2)
Using the three-way comparison operator only available in PHP 7
How does the three-way comparison operator work?
expr1 <=> expr2
Returns -1 if expr1 < expr2
Returns 1 if expr1 > expr2
Returns 0 if expr1 == expr2
What does strcmp(expr1, expr2) return?
< 0 if expr 1 < expr2
> 0 if expr1 > expr2
0 if expr1 == expr2
What are the 3 functions available in PHP to test if a value is NAN or INF?
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
What are the rules for keys and values for an associative array in PHP?
Keys can be integers or strings and values can be of any type including arrays
Keys are not necessarily unique
What function gives you the size of an array?
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 can you retrieve the keys and values of an array separately?
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 can you check if a particular key exists within an array?
Using the array_key_exists(key, array) function
What is the syntax for a foreach loop?
foreach($array as $key=>$value) {
}
Note that $key=> is optional
How can you add elements to an array?
Using array_push($array, value1, value2, …) you can add one or more values onto the end of an array
How can you remove elemtns from an array?
Using array_pop($array) you can remove the last element from an array and return it
What happens if you try to use array_pop on an empty array or something that is not an array?
NULL will be returned