Q:

Write a PHP program to check if a given array of integers contains 5 next to a 5 somewhere

0

Write a PHP program to check if a given array of integers contains 5 next to a 5 somewhere

All Answers

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

<?php
function test($nums)
 { 

      for ($i = 0; $i < sizeof($nums) - 1; $i++)
          {
             if ($nums[$i] == 5 && $nums[$i] == $nums[$i + 1]) return true;
           }

            return false;
 }   

var_dump(test([1, 5, 6, 9, 10, 17]));
var_dump(test([1, 5, 5, 9, 10, 17]));
var_dump(test([1, 5, 5, 9, 10, 17, 5, 5]));

Sample Output:

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

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