0
0
PHPprogramming~15 mins

Array search functions in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Array search functions
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use if ($found) to check the boolean and print() to show the message.