Recall & Review
beginner
What does the PHP function
in_array() do?It checks if a value exists in an array and returns
true if found, otherwise false.Click to reveal answer
beginner
How does
array_search() differ from in_array()?array_search() returns the key of the found value in the array, or false if not found, while in_array() returns only true or false.Click to reveal answer
beginner
What is the return value of
array_search() if the searched value is not found?It returns
false to indicate the value is not present in the array.Click to reveal answer
intermediate
Explain the use of the third parameter in
in_array() and array_search().The third parameter is a boolean for strict checking. If
true, the functions check both value and type (===). If false or omitted, only value is checked (==).Click to reveal answer
intermediate
What will
array_search('5', [1, 2, 3, 5]) return with strict mode off and on?With strict mode off (default), it returns the key
3 because '5' loosely equals 5.<br>With strict mode on, it returns false because string '5' is not identical to integer 5.Click to reveal answer
Which PHP function returns the key of a value in an array?
✗ Incorrect
array_search() returns the key of the found value. in_array() returns true/false only.What does
in_array('10', [10, 20, 30]) return by default?✗ Incorrect
By default,
in_array() does loose comparison, so string '10' matches integer 10.If
array_search() does not find the value, what does it return?✗ Incorrect
array_search() returns false when the value is not found.Which parameter controls strict type checking in
in_array()?✗ Incorrect
The third parameter is a boolean that enables strict type checking.
What will
array_search(5, ['5'], true) return?✗ Incorrect
With strict mode on, integer 5 is not identical to string '5', so it returns
false.Describe how to check if a value exists in an array and get its key in PHP.
Think about two functions: one for yes/no, one for key.
You got /5 concepts.
Explain the difference between loose and strict checking in PHP array search functions.
Consider how '5' and 5 compare with and without strict mode.
You got /5 concepts.