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. 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?
Aarray_search()
Bin_array()
Carray_key_exists()
Darray_keys()
✗ 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?
A10
Bfalse
Ctrue
Dnull
✗ 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?
Afalse
B-1
Cnull
D0
✗ Incorrect
array_search() returns false when the value is not found.
Which parameter controls strict type checking in in_array()?
AFirst parameter
BNo parameter controls this
CSecond parameter
DThird parameter
✗ Incorrect
The third parameter is a boolean that enables strict type checking.
What will array_search(5, ['5'], true) return?
A0
Bfalse
C1
D5
✗ 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.
Practice
(1/5)
1. Which PHP function returns the key of a value in an array if found, or false if not found?
easy
A. in_array
B. array_search
C. array_key_exists
D. array_keys
Solution
Step 1: Understand function purpose
array_search looks for a value and returns its key if found.
Step 2: Compare with other functions
in_array returns true/false, not key; array_key_exists checks keys; array_keys returns all keys.
A. array_search returns false but condition treats 0 as false
B. array_search returns key but 30 is not in array, so condition fails correctly
C. array_search should be replaced with in_array for this check
D. No error, code works as expected
Solution
Step 1: Analyze array_search return and condition
array_search returns key or false. If key is 0, condition fails because 0 is falsey.
Step 2: Check searched value presence
30 is not in array, so array_search returns false, condition is false, prints 'Not Found'. But if value was at key 0, condition would fail incorrectly.
Step 3: Suggest correct function for boolean check
Use in_array which returns true/false directly for presence check.
Final Answer:
array_search should be replaced with in_array for this check -> Option C
Quick Check:
Use in_array for boolean presence check [OK]
Hint: Use in_array for true/false presence checks [OK]
Common Mistakes:
Using array_search in if without strict check
Ignoring 0 key treated as false
Not using === to check false
5. Given the array $data = ['a' => 0, 'b' => false, 'c' => null, 'd' => '0'];, which function call correctly checks if the value 0 exists in the array considering type?
hard
A. in_array(0, $data, true);
B. array_search(0, $data);
C. in_array(0, $data);
D. array_search('0', $data, true);
Solution
Step 1: Understand strict checking
Using third parameter true enforces type checking in in_array and array_search.
Step 2: Analyze array values and types
Values are 0 (int), false (bool), null, and '0' (string). Only strict check distinguishes 0 (int) from others.
Step 3: Choose correct function and parameters
in_array(0, $data, true) returns true only if exact int 0 found. array_search without strict may confuse types.
Final Answer:
in_array(0, $data, true); -> Option A
Quick Check:
Use in_array with strict=true for exact type match [OK]
Hint: Use in_array with strict=true to check exact value and type [OK]
Common Mistakes:
Ignoring strict parameter causing false positives
Using array_search without strict for type-sensitive check
Confusing '0' string with 0 integer
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.