Q:

Write a PHP program to check if one of the first 4 elements in an array of integers is equal to a given element

0

Write a PHP program to check if one of the first 4 elements in an array of integers is equal to a given element

All Answers

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

<?php
function test($nums, $n)
{
    return sizeof($nums) < 4 ? in_array($n, $nums) : in_array($n, array_slice($nums, 0, 4));
 }

var_dump(test(array(1,2,9,3), 3));
var_dump(test(array(1,2,3,4,5,6), 2));
var_dump(test(array(1,2,2,3), 9));

Sample Output:

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