Q:

Write a PHP program to check a given array of integers and return true if there is a 3 with a 5 somewhere later in the given array

0

Write a PHP program to check a given array of integers and return true if there is a 3 with a 5 somewhere later in the given array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($numbers)
 { 
    $three = false;

    for ($i = 0; $i < sizeof($numbers); $i++)
            {
               if ($three && $numbers[$i] == 5) return true;
               if ($numbers[$i] == 3) $three = true;
            }
        return false;
 }   

var_dump(test([3, 5, 1, 3, 7]));
var_dump(test([1, 2, 3, 4]));
var_dump(test([3, 3, 5, 5, 5, 5]));
var_dump(test([2, 5, 5, 7, 8, 10]));

Sample Output:

bool(true)
bool(false)
bool(true)
bool(false)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now