0
0
PhpProgramBeginner · 2 min read

PHP Program for Linear Search with Example and Explanation

A PHP program for linear search uses a foreach loop to check each element in an array and returns the index if the target is found, like foreach ($arr as $index => $value) { if ($value === $target) return $index; }.
📋

Examples

Input[1, 3, 5, 7, 9], target=5
OutputElement found at index 2
Input[10, 20, 30, 40], target=25
OutputElement not found
Input[], target=1
OutputElement not found
🧠

How to Think About It

To do a linear search, start from the first item in the list and check if it matches the target. If it does, return its position. If not, move to the next item and repeat until you find the target or reach the end of the list.
📐

Algorithm

1
Get the array and the target value.
2
Start from the first element of the array.
3
Compare the current element with the target.
4
If they match, return the current index.
5
If not, move to the next element.
6
If the end of the array is reached without a match, return -1.
💻

Code

php
<?php
function linearSearch(array $arr, $target) {
    foreach ($arr as $index => $value) {
        if ($value === $target) {
            return $index;
        }
    }
    return -1;
}

$numbers = [1, 3, 5, 7, 9];
$target = 5;
$result = linearSearch($numbers, $target);
if ($result !== -1) {
    echo "Element found at index $result";
} else {
    echo "Element not found";
}
Output
Element found at index 2
🔍

Dry Run

Let's trace the search for target 5 in the array [1, 3, 5, 7, 9].

1

Check index 0

Value is 1, target is 5, no match.

2

Check index 1

Value is 3, target is 5, no match.

3

Check index 2

Value is 5, target is 5, match found.

IndexValueTargetMatch?
015No
135No
255Yes
💡

Why This Works

Step 1: Loop through array

The foreach loop goes through each element one by one to check for the target.

Step 2: Compare elements

Each element is compared with the target using === to ensure exact match.

Step 3: Return index or -1

If a match is found, the index is returned immediately; otherwise, after checking all elements, -1 is returned.

🔄

Alternative Approaches

Using for loop with index
php
<?php
function linearSearchFor(array $arr, $target) {
    for ($i = 0; $i < count($arr); $i++) {
        if ($arr[$i] === $target) {
            return $i;
        }
    }
    return -1;
}

$numbers = [1, 3, 5, 7, 9];
$target = 7;
$result = linearSearchFor($numbers, $target);
echo $result !== -1 ? "Element found at index $result" : "Element not found";
This uses a classic for loop with index, which is useful if you need the index explicitly and want to control iteration.
Using array_search function
php
<?php
$numbers = [1, 3, 5, 7, 9];
$target = 9;
$index = array_search($target, $numbers, true);
if ($index !== false) {
    echo "Element found at index $index";
} else {
    echo "Element not found";
}
This uses PHP's built-in <code>array_search</code> which is simpler but less educational about the search process.

Complexity: O(n) time, O(1) space

Time Complexity

The search checks each element once until it finds the target or reaches the end, so it takes linear time proportional to the array size.

Space Complexity

No extra space is needed besides a few variables, so space complexity is constant.

Which Approach is Fastest?

Using PHP's built-in array_search is usually fastest and simplest, but manual loops give more control and clarity.

ApproachTimeSpaceBest For
Manual foreach loopO(n)O(1)Learning and control
For loop with indexO(n)O(1)Index control and iteration
array_search functionO(n)O(1)Quick and simple searches
💡
Use strict comparison === in PHP to avoid unexpected matches during linear search.
⚠️
Beginners often forget to return -1 when the element is not found, causing incorrect results.