📖 Scenario: You are working on a small PHP program to find specific values inside an array of fruits. This is like looking for your favorite fruit in a fruit basket.
🎯 Goal: You will create an array of fruits, set a fruit to search for, use PHP array search functions to find if the fruit is in the basket, and then print the result.
📋 What You'll Learn
Create an array called $fruits with the exact values: 'apple', 'banana', 'cherry', 'date', 'elderberry'
Create a variable called $searchFruit and set it to 'cherry'
Use the PHP function in_array() with $searchFruit and $fruits to check if the fruit is in the array
Print "Found cherry in the basket!" if the fruit is found, otherwise print "cherry is not in the basket."
💡 Why This Matters
🌍 Real World
Searching arrays is common when you want to check if an item exists in a list, like checking if a username is taken or if a product is in stock.
💼 Career
Knowing how to search arrays is a basic skill for PHP developers working on websites, data processing, or any application that handles lists of data.
Progress0 / 4 steps
1
Create the fruits array
Create an array called $fruits with these exact values: 'apple', 'banana', 'cherry', 'date', 'elderberry'.
PHP
Hint
Use square brackets [] to create the array and separate values with commas.
2
Set the fruit to search
Create a variable called $searchFruit and set it to the string 'cherry'.
PHP
Hint
Use the assignment operator = to set the variable.
3
Check if the fruit is in the array
Use the PHP function in_array() with $searchFruit and $fruits to check if the fruit is in the array. Store the result in a variable called $found.
PHP
Hint
The function in_array() returns true if the value is found, otherwise false.
4
Print the search result
Write an if statement to print "Found cherry in the basket!" if $found is true, otherwise print "cherry is not in the basket.".
PHP
Hint
Use if ($found) to check the boolean and print() to show the message.
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.