Q:

Write a PHP program to check if a given array of integers contains no 3 or a 5

0

Write a PHP program to check if a given array of integers contains no 3 or a 5

All Answers

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

<?php
function test($nums)
 { 

        $three = false;
        $five = false;

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

var_dump(test([5, 5, 5, 5, 5]));
var_dump(test([3, 3, 3, 3]));
var_dump(test([3, 3, 3, 5, 5, 5]));
var_dump(test([1, 6, 8, 10]));

Sample Output:

bool(true)
bool(true)
bool(false)
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