Q:

Write a PHP program to check a given array (length will be atleast 2) of integers and return true if there are two values 15, 15 next to each other

0

Write a PHP program to check a given array (length will be atleast 2) of integers and return true if there are two values 15, 15 next to each other.

 

All Answers

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

<?php
function test($numbers)
    { 
       for ($i = 0; $i < sizeof($numbers) - 1; $i++)
          {
             if ($numbers[$i + 1] == $numbers[$i] && $numbers[$i] == 15) return true;
          }
       return false;;
    }  
    
var_dump(test([5, 5, 1, 15, 15]));
var_dump(test([15, 2, 3, 4, 15]));
var_dump(test([3, 3, 15, 15, 5, 5]));
var_dump(test([1, 5, 15, 7, 8, 15]));

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