Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:如何正确使用return语句_Php - Fatal编程技术网

PHP:如何正确使用return语句

PHP:如何正确使用return语句,php,Php,问题:我有一个名为“user”的类和一个名为“registerUser”的函数。我想将成功注册发送到某个页面,但未成功注册到其他页面 我没有填写返回声明,因为我不确定这是否是解决问题的方法。我对php还不熟悉,相信它可以工作,但正如我所说的,我是新手,还在学习 class User { function registerUser($userName, $userPassword) { // Connect to database $dbLink = mysql_conne

问题:我有一个名为“user”的类和一个名为“registerUser”的函数。我想将成功注册发送到某个页面,但未成功注册到其他页面

我没有填写返回声明,因为我不确定这是否是解决问题的方法。我对php还不熟悉,相信它可以工作,但正如我所说的,我是新手,还在学习

class User {
    function registerUser($userName, $userPassword) {
    // Connect to database
    $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    if(!$dbLink) die("Could not connect to database. " . mysql_error());

    // Select database
    mysql_select_db($this->dbName);

    // Insert data
    $query = "insert into $this->dbUserTable values (NULL, \"$userName\", \"$userPassword\")";
    $result = mysql_query($query);

    // Test to make sure query worked
    if(!$result) {
        return(false);
    }

    // Get the user ID
    $this->userID = mysql_insert_id();

    // Close database connection
    mysql_close($dbLink);

    // Assign the values to the data members
    $this->userName = $userName;
    $this->userPassword = $userPassword;
    return(true);
} // End registerUser()
}
我调用这个类的php代码很简单

    <?
// processRegister.php
//////////////////////

// First include the class definition
include('UserClass.php');

// Next, create an instance of the class
$newUser = new User;

// Call the registerUser() method, passing in the required variables
if ($newUser->registerUser($userName, $userPassword)) {
  header('Location: www.google.com');
} 
else {
  header('Location: www.yahoo.com.com');
}
?>
非常感谢您的帮助

我会这样做:

if ($newUser->registerUser($userName, $userPassword)) {
  $newUser->displayUserInfo();
} else {
  echo "Errors, ahoy!"
}
如果需要调试消息,请去掉die语句,只添加return false或使用echo替换die并添加return false


最后,添加return true。

我认为您应该使用PHP函数mysql_insert_id返回最后插入的用户id。基于此,您可以确定注册过程是否成功

class User {
function registerUser($userName, $userPassword) {
        // Connect to database
        $dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
        if(!$dbLink) die("Could not connect to database. " . mysql_error());

        // Select database
        mysql_select_db($this->dbName);

        // Insert data
        // Notice the "$userName" and "$userPassword" - these are the ones
        // passed in from your main script, not the class/object variables!
        $query = "insert into $this->dbUserTable values (NULL, \"$userName\", \"$userPassword\")";
        $result = mysql_query($query);

        return mysql_insert_id();

}
processRegister.php

<?
// processRegister.php
//////////////////////

// First include the class definition
include('UserClass.php');

// Next, create an instance of the class
$newUser = new User;

// Call the registerUser() method, passing in the required variables
$user_id = $newUser->registerUser($userName, $userPassword);

if(intval($user_id) && $user_id>0){
    //redirect to success page
}else{
    //redirect to error page
}

?> 

死而复生!我已经用我的新代码更新了问题,但它不起作用,你看到任何错误吗?我看不到:它正在把我发送到我的404页面。你的重定向中有一个额外的.com。