【问题标题】:strcmp() not showing proper output (PHP)strcmp() 没有显示正确的输出 (PHP)
【发布时间】:2018-11-11 12:30:34
【问题描述】:

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

<!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");
  }
 }

【问题讨论】:

  • 检查两个密码字符串中的任何一个尾随空格字符(例如制表符、空格等)。
  • 不,没有字符,我尝试了 var_dump($uppassword) 和 var_dump($ucpassword) 他们都给出了输出 String(5)。
  • 请在发布之前从您的代码中提取minimal reproducible example。这里的代码太多了,解释“strcmp() 不起作用”也无济于事。作为新用户,请务必阅读How to Ask 并使用tour
  • 您真的应该测试零(0),因为这是 strcmp() 在比赛中返回的...if(strcmp($upassword, $ucpassword) == 0){echo "All cool";}else{echo "Passwords do not Match"}
  • @Jamie_D 这部分没问题 - 如果密码不同,则终止脚本

标签: php strcmp


【解决方案1】:

问题出在这里:

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");
}

【讨论】:

  • 非常感谢,这成功了。非常感谢您的帮助,先生,我也将此标记为答案。
猜你喜欢
  • 2012-05-22
  • 1970-01-01
  • 2018-01-17
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
  • 2021-04-27
  • 1970-01-01
  • 2021-05-19
相关资源
最近更新 更多