gpt4 book ai didi

php - 此递归 PHP 函数未返回任何值

转载 作者:搜寻专家 更新时间:2023-10-31 20:39:08 25 4
gpt4 key购买 nike

<分区>

我正在尝试一个简单的二进制搜索。我希望这个函数返回“假”或“真”,但它似乎没有返回任何东西。我尝试返回整数或字符串,只是想看看它是否会返回另一种数据类型,但它似乎并没有这样做。我做错了什么?

<?php
function binarySearch($item, $first, $last) {

$numbers = array(3, 5, 9, 11, 17, 24, 38, 47, 50, 54, 57, 59, 61, 63, 65);
if ($first > $last) {
return false;
}
else {
$middle = ($first + $last)/2;
$middle = (int)$middle;

if ($item == $numbers[$middle]) {
echo "found the correct value<br/>";
return true;
}
elseif ($item<$numbers[$middle]) {
binarySearch($item, $first, $middle-1);
}
else {
binarySearch($item, $middle+1, $last);
}
}
}

$n = $_GET['txtTarget'];
$first = $_GET['txtFirst'];
$last = $_GET['txtLast'];

echo "the return value is: " . binarySearch($n, $first, $last);
?>

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com