如何从函数中访问返回的变量

时间:2014-02-09 03:59:16

标签: php

这是使用“login”函数的脚本,它返回两个变量,当我尝试将它们从sanitize函数带到“login”函数时它是如何进入的,它不起作用

的index.php

<?php
if(!empty($_POST['submit']))
{
include("check.php");
$class = new check;
$user = $_POST['username'];
$pass = $_POST['password'];
$results = $class->sanitize($user, $pass);
$class->login($results[0], $results[1]);
//$class->login($user, $pass);
}
?>

check.php

function sanitize($string, $string2)
{
    $stringh = htmlentities($string);
    $string1h = htmlentities($string2);
    $stringht = trim($stringh);
    $string1ht = trim($string1h);
    return $stringht;
    return $string1ht;
}

function login($user, $pass)
{
    $result = $this->link->query("SELECT * FROM `login` WHERE `username` = '".$user."'");
    $numbers = mysqli_num_rows($result);
    if($numbers != 0)
    {
        while($row = mysqli_fetch_assoc($result))
        {
            $dbuser = $row['username'];
            $dbpass = $row['password'];
        }

        if($dbuser == $user && $dbpass == $pass)
        {
            echo "You have a match.";
        }
    }
}

1 个答案:

答案 0 :(得分:6)

您只能从函数返回1个变量。如果你需要两个,将它们包装在一个数组中,给你一个返回的对象。

return array($stringht, $string1ht);

然而,一个更好的解决方案可能是让你的清理接受一个输入并有一个输出,然后只调用两次,每个字符串一次。