Php 从函数中传递变量
因此,每个页面都会查找4个内容,$e、$w、$n和$success $e是设置错误的数组。在每个函数中,错误都设置为$e[]=“错误消息” $w表示警告,$n表示通知,$success表示明显 现在,如果函数有错误,它不应该继续执行它设置的操作,因为通知和警告只是为了通知目的 页面检查所有这些内容,并以可关闭的彩色分区显示每个内容 这是我的问题 我认为这不会像预期的那样奏效 这将返回变量本身,还是仅在函数运行时显示变量的内容Php 从函数中传递变量,php,function,variables,Php,Function,Variables,因此,每个页面都会查找4个内容,$e、$w、$n和$success $e是设置错误的数组。在每个函数中,错误都设置为$e[]=“错误消息” $w表示警告,$n表示通知,$success表示明显 现在,如果函数有错误,它不应该继续执行它设置的操作,因为通知和警告只是为了通知目的 页面检查所有这些内容,并以可关闭的彩色分区显示每个内容 这是我的问题 我认为这不会像预期的那样奏效 这将返回变量本身,还是仅在函数运行时显示变量的内容 function add_member() { global
function add_member()
{
global $dbh;
if (!isset($_POST['username'],$_POST['password'],$_POST['email'])) {
$e[] = 'We need the minimum of a username and password to add a new user';
}
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if (!preg_match($regex, $_POST['email'])) {
$e[] 'The email address is wrong';
}
$stmt = $dbh->prepare("SELECT `username` FROM `1_members` WHERE `username`=? LIMIT 1");
$username = strtolower($_POST['username']);
$username = trim($username);
$stmt->bindValue(1, $username, PDO::PARAM_STR);
$stmt->execute();
if ($stmt->rowCount()) {
$e[] = 'That username is already in use';
}
if (($_POST['password']) !== ($_POST['vpassword'])) {
$e[] = 'The passwords did not match.';
}
if (strlen($_POST['username']) <= '3') {
$e[] = 'Username too short - needs to be 4 or more chars.';
}
if (strlen($_POST['password']) <='5'){
$e[] = 'password not long enough, please make it 6 chars.';
}
if (!isset($_POST['vpassword'])) {
$w[] = 'Warning: You didn\'t verify the password. Incase of typo the password was ['..']';
}
if (!isset($_POST['email'])) {
$w[] = 'Warning: You didn\'t enter an email address the password. This member will not get email alerts without an email address.';
}
if ($e) {
return $e;
}
else {
$password = trim($_POST['password']);
$options = ['cost' => 12,];
$password = password_hash($password, PASSWORD_BCRYPT, $options);
$email = trim($_POST['email']);
$stmt = $dbh->prepare("INSERT INTO `1_members` (`username`, `password`, `email`) VALUES(?,?,?)");
$stmt->bindValue(1,$username,PDO::PARAM_STR);
$stmt->bindValue(2,$password,PDO::PARAM_STR);
$stmt->bindValue(3,$email,PDO::PARAM_STR);
$stmt->execute()
$success = 'added member '.$username.'';
if (isset($w)) {
return $w;
}
if (isset($n)) {
return $n
}
return $success;
}
}
这个函数可能会返回4个不同的内容,除非数据库设置中出现严重错误,否则它不会显示任何内容。相反,它将返回变量: $e阵列,如果由于验证错误或重复而无法创建帐户 $w数组(如果在创建帐户后出现警告) $n数组,如果创建后有通知-除非从未发生$n未声明。 $success字符串,否则为 这个功能无处不在——不可预测。不管怎么说,它都必须做一系列的逻辑来处理它的回报。可能更好的方法是返回更丰富的构造。例如:
function add_member() {
$return_info = array(
"errors" => array(),
"warnings" => array(),
"notices" => array(),
"created" => false # sometimes a simple true / false is good enough
);
// do validation as before
// ...
if( $_POST['password'] !== $_POST['vpassword'] ) {
$return_info["errors"][] = "Passwords didn't match.";
}
// if account IS created, just flip the created switch
// ...
$try_inserting = $stmt->execute();
if( $try_inserting ) { # replace this with an actual error check
$return_info["created"] = true;
}
// now everything comes back: creation status, warnings, errors, etc...
return( $return_info );
}
这个函数重构标准化了它的所有返回,因此在处理它时是可预测的。您可以执行以下简单语句:
$attempt = add_member();
if( $attempt["created"] === true ) {
echo "added member!";
}
无论调用什么,它都可以对错误、警告、通知进行非常简单的检查:
if( !empty($attempt["errors"]) ) {
// display the errors
}
将$e、$w、$n和$success作为引用传递它将返回一个包含错误的数组,或一个包含消息的字符串。考虑在返回子句中使用EndoDE为$E。