Linear Search in PHP


Q. Write a PHP program for Linear Search

<?php
// Define an array
$numbers = array(12, 45, 78, 34, 89, 23, 67);

// Define the target element to search
$target = 34;
$found = false;

// Perform linear search
for ($i = 0; $i < count($numbers); $i++) {
    if ($numbers[$i] == $target) {
        echo "Element $target found at index: $i";
        $found = true;
        break;
    }
}

if (!$found) {
    echo "Element $target not found in the array.";
}
?> 

Q. Write an algorithm for Linear Search in PHP

1. Start
2. Define an array numbers[]
3. Define the target element target
4. Loop through the array from i = 0 to length - 1:
    a. If numbers[i] == target, then
5. Print the index i
6. Set found = true
7. Break the loop
8. If found == false, print "Element not found"
9. End



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.