0

我想从函数内部获取一些值,然后在外部使用它们进行计算?可能吗?我是 PHP 新手。

假设我有代码:

function snltc($ema)
{ 
    $uemail = $ema;

    $snlc = mysql_query("SELECT * FROM usertree WHERE slot1='$uemail' || slot2='$uemail' || slot3='$uemail' || slot4='$uemail' || slot5='$uemail'");
    $dsnl = mysql_fetch_array($snlc);

    echo $dsnl['supemail'];
    echo "</br>";
    echo $dsnl['snlt'];
    $sup1 = $dsnl['supemail'];
    $snltu = $dsnl['snlt'] + 1;
    echo "</br>";
    echo $snltu;
    mysql_query("UPDATE usertree SET snlt='$snltu' WHERE supemail='$sup1'");
    $newsnl = mysql_fetch_array($snlc);
    echo "</br>";
    $snlval = $newsnl['snlt'];
    echo "</br>";
    return $snlval;
}

echo "</br>";
echo snltc('gami@gmail.com');

现在,我想从这段代码中使用$snlval函数$sup1外部和函数外部。我怎样才能做到这一点?

4

4 回答 4

2

您可以将其作为数组返回:

function snltc($ema){ 
    $uemail = $ema;
    $snlc = mysql_query("SELECT * FROM usertree WHERE slot1='$uemail' || slot2='$uemail' || slot3='$uemail' || slot4='$uemail' || slot5='$uemail'");
    $dsnl = mysql_fetch_array($snlc);
    $sup1 = $dsnl['supemail'];
    $snltu = $dsnl['snlt'] + 1;
    mysql_query("UPDATE usertree SET snlt='$snltu' WHERE supemail='$sup1'");
    $return = array();
    $return['snlval'] = $snltu;
    $return['sup1'] = $sup1;
    return $return;
}

那么你可以使用

$return = snltc('gami@gmail.com');
echo $return['snlval'];
echo $return['sup1'];
于 2012-09-18T11:01:06.370 回答
0

在您的功能中:

return array($snival, $sup1);

调用函数:

$values = snltc($x);
echo $values[0] . $values[1];
于 2012-09-18T11:01:04.503 回答
0

返回数组中的两个值:

function snltc($ema)
{
  // ...
  return array($sup1, $snlval);
}

list($sup1, $snlval) = snltc('gami@gmail.com');
于 2012-09-18T11:01:39.430 回答
0

现在我想从这段代码中使用 $snlval outsideof 函数和 $sup1 outside of the function 我怎么能做到这一点?

假设您要返回 2 个变量..您会将它们放入一个数组并返回该数组,因此您的 return 语句变为

return array('snlval' => $snlval, 'sup1' => $sup1 );

所以当你调用这个函数时......

$result = snltc('gami@gmail.com');
$snlval = $result['snlval'];
$sup1 = $result['sup1'];
于 2012-09-18T11:02:13.090 回答