strcmp()没有显示正确的输出(PHP)
问题描述:
So here is my code, the problem is that i get strcmp() output as 5 when i enter the password 'malik' in both the fields when it should obviously be 0, i will attach an image to make myself clear. Also, I tried using var_dump($upassword) and var_dump($ucpassword) and I got String(5) for both of them, so there are no whitespaces.
<!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");
}
}
所以这是我的代码,问题是当我输入密码时我得到strcmp()输出为5 malik'在这两个领域显然应该是0, i会附上一张图片让自己清楚。 另外,我尝试使用var_dump($ upassword)和var_dump($ ucpassword),我得到了两个字符串(5),所以没有空格。 p>
&lt;!DOCTYPE html&gt;
&lt; html lang =”en“&gt;
&lt; head&gt;
&lt; meta charset = “UTF-8”&gt;
&lt; meta name =“viewport”content =“width = device-width,initial-scale = 1.0”&gt;
&lt; meta http-equiv =“X-UA-Compatible” content =“ie = edge”&gt;
&lt; title&gt;注册&lt; / title&gt;
&lt; / head&gt;
&lt; body&gt;
&lt; form action =“signup.php”method =“post” &gt;
&lt; input type =“text”name =“uname”placeholder =“输入用户名”&gt;
&lt; input type =“email”name =“uemail”placeholder =“输入电子邮件”id = “”&gt;
&lt; input type =“password”name =“upassword”placeholder =“Password”&gt;
&lt; input type =“password”name =“ucpassword”placeholder =“confirm password”&gt; \ n&lt;按钮典型 e =“submit”name =“registor”&gt; Registor&lt; / button&gt;
&lt; / form&gt;
&lt; / body&gt;
&lt; / html&gt;
&lt;?php
if(isset($ _ POST [' registor'])){
$ uname = $ _POST ['uname'];
$ uemail = $ _POST ['uemail'];
$ upassword =(string)$ _ POST ['upassword'];
$ ucpassword =(string)$ _ POST ['ucpassword'];
echo($ uname。“&lt; br&gt;”);
echo($ uemail。“&lt; br&gt;”);
echo($ upassword) 。“&lt; br&gt;”);
echo($ ucpassword。“&lt; br&gt;”);
if($ uname ==''|| $ uemail ==''|| $ ucpassword =''|| $ ucpassword =''){
die(“请填写所有字段”);
}
echo(strcmp($ upassword,$ ucpassword));
if(strcmp($ upassword,$ ucpassword)! = 0){
die(“密码不相同”);
}
}
code> pre>
div>
答
The problem is here:
if($uname == '' || $uemail == '' || $ucpassword = '' || $ucpassword = ''){
die("Please fill all the fields");
}
You are setting $ucpassword
to an empty string. Also instead of first $ucpassword
use just $upassword
It should be like this:
if($uname == '' || $uemail == '' || $upassword == '' || $ucpassword == ''){
die("Please fill all the fields");
}