strcmp()未显示正确的输出(PHP)

时间:2018-11-11 12:30:35

标签: php strcmp

这是我的代码,问题是当我在两个字段中均输入明显为0的密码“ malik”时,我得到strcmp()输出为5, 我会附上一张图片以使自己清晰。另外,我尝试使用var_dump($ upassword)和var_dump($ ucpassword),并且两个都得到了String(5),所以没有空格。

enter image description here

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Sign-UP</title>
</head>
<body>
  <form action="signup.php" method="post">
  <input type="text" name="uname" placeholder="enter the username">
  <input type="email" name="uemail" placeholder="enter the email" id="">
  <input type="password" name="upassword" placeholder="Password">
  <input type="password" name="ucpassword" placeholder="confirm password">
  <button type="submit" name="registor">Registor</button>
  </form>
</body>
</html>
<?php
if (isset($_POST['registor'])) {
  $uname = $_POST['uname'];
  $uemail = $_POST['uemail'];
  $upassword = (string)$_POST['upassword'];
  $ucpassword = (string)$_POST['ucpassword'];
  echo($uname."<br>");
  echo($uemail."<br>");
  echo($upassword."<br>");
  echo($ucpassword."<br>");
  if($uname == '' || $uemail == '' || $ucpassword = '' || $ucpassword = ''){
    die("Please fill all the fields");
  }
  echo(strcmp($upassword,$ucpassword));
  if(strcmp($upassword,$ucpassword) != 0){
    die("Passwords are not the same");
  }
 }

1 个答案:

答案 0 :(得分:5)

问题在这里:

if($uname == '' || $uemail == '' || $ucpassword = '' || $ucpassword = ''){
  die("Please fill all the fields");
}

您正在将$ucpassword设置为空字符串。另外,不要只使用$ucpassword

来代替第一个$upassword

应该是这样的:

if($uname == '' || $uemail == '' || $upassword == '' || $ucpassword == ''){
  die("Please fill all the fields");
}