要查找给定的数字序列中是否存在数字,代码如下-
<?php function contains_in_sequence($val_1, $val_2, $val_3) { if ($val_1 == $val_2) return true; if (($val_2 - $val_1) * $val_3 > 0 && ($val_2 - $val_1) % $val_3 == 0) return true; return false; } $val_1 = 11; $val_2 = 99; $val_3 = 2; print_r("Is the number present in the sequence? "); if (contains_in_sequence($val_1, $val_2, $val_3)) echo "Yes, it is present in the sequence"; else echo "No, it is not present in the sequence"; ?>
输出结果
Is the number present in the sequence? Yes, it is present in the sequence
名为“ contains_in_sequence”的函数检查两个值是否相同,如果相等,则该函数返回true。如果两个值之间的差乘以第三个值大于0,并且两个值之间的差除以第三个值,则提示为0,则该函数返回true,否则返回false。将值分配给三个变量,然后通过将这些值传递给函数来调用函数。相关输出将显示在控制台上。