gpt4 book ai didi

php - 不能通过调用返回函数在 php 中返回 2 个变量?

转载 作者:行者123 更新时间:2023-12-04 03:47:40 25 4
gpt4 key购买 nike

1st 我不得不说我不是 php 专业人士,这是我第一次使用 return()。

所以这是代码。我需要返回 false 和函数剩余的分钟数。

if(!checkIpn())
$err[]='you need to wait'.$nresting.'minutes before you send another request';
function checkIpn()
{
$useripe = 0;
if ( isset($_SERVER["REMOTE_ADDR"]) ) { $useripe = $_SERVER["REMOTE_ADDR"] ; }
else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) { $useripe = $_SERVER["HTTP_X_FORWARDED_FOR"] ; }
else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) { $useripe = $_SERVER["HTTP_CLIENT_IP"] ; }


$query = "SELECT * FROM table WHERE ip = '$useripe' AND status = 'pending' ORDER BY id ASC";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);

if ( $num_rows > 0 )
{
$str_today = date("Y-m-d H:i:s");
$i=1;
while($row = mysql_fetch_assoc($result))
{
$str_start = $row['date'];
$str_start = strtotime($str_start);
$str_end = strtotime($str_today);
$nseconds = $str_end - $str_start;
$nminutes = round($nseconds / 60);
if ( $nminutes > 120 )
{ return true; }
else {
$nresting = 120 - $nminutes;
return false; }
$i++;
}
}
else { return true; }

基于下面的 Tadeck 回答。我是这样做的:

$result = checkIpn();
if($result[0]==false)
$err[]='you need to wait '.$result[1].' minutes before you send another request';

谢谢塔德克。

最佳答案

如果你想使用一次调用返回两个不同的变量,你可以使用类似的东西:

return array(true, 0);

return array(true);

第一种情况(返回成功时)和

return array(false, $minutes);

第二种情况(返回失败)

然后你可以这样检查:

$result = checkIpn();

if ($result[0]) {
echo 'Success.';
} else {
echo 'Failure. Minutes to wait: '.$result[1];
}

希望对您有所帮助。

编辑:

如果我没理解错的话,你会返回 true 或分钟数 > 0(大于零)。因此,您可以在成功的情况下使用这样的返回:

return true;

在失败的情况下:

return $minutes;

然后你就可以通过以下方式在代码中使用它了:

$result = checkIpn();

if ($result === true) {
echo 'Success';
} else {
echo 'Failure. Minutes to wait: '.$result;
}

而且我想建议您正确记录此类函数,这样当它返回整数而不是 bool 值时,没有人会感到惊讶;)

关于php - 不能通过调用返回函数在 php 中返回 2 个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5915645/

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