コード例 #1
0
<?php

function array_front9($nums)
{
    for ($i = 0; $i <= 3; $i++) {
        if ($nums[$i] == 9) {
            return "true";
        }
    }
    return "false";
}
print array_front9([1, 2, 9, 3, 4]);
print array_front9([1, 2, 3, 4, 9]);
print array_front9([1, 2, 3, 4, 5]);
コード例 #2
0
//Quiz: Write function array_front9($nums) that returns 'true' if one of the first
//four numbers in the array is 9, and 'false' otherwise

function array_front9($nums) {
    for ($k=0; $k<4; $k++){
        if ($nums[$k] == 9){
            return 'true';
        }
    } return 'false';
}
$nums = array(0,0,0,0,9);
$num = array(0,0,9,9);
$n = array (0,9);
echo array_front9 ($nums)."<br>";
echo array_front9 ($num)."<br>";
echo array_front9 ($n)."<br>";
?>

<?php
//Matt's answer

function front9($numb) {
    for($a=0; $a<4; $a++){
        if ($a<count($numb)
            && $numb[$a]==9)
            return "true";
    }
    return "false"; //put this outside the loop to test out the whole loop first
}

$result = front9(array(9,0,0,0));