duanlieshuang5330 2011-08-02 15:55
浏览 110
已采纳

php strcmp函数

I have the following code:

if(strcmp($_POST['password'], $_POST['password-rpt']) == 0) {  
    $password_field = $_POST['password'];  
    echo "Password created" . "<br />";  
} else {  
    echo "blarg! Password mismatch!!!";  
}

I know that like C/C++ strcmp is supposed to return 0 when the two strings are the same but when I test it with an html file, entering two identical strings or two different ones, both cases go to the error case and my output is "blarg! Password mismatch!!!"

Here is the form I used:

<form method="post" action="register.php">
        Name: <input type="text" name = "name" id="name" /><br />
        Password: <input type="password" name ="password" id = "password" /> <br />
        Confirm Password: <input type="password" name="password_rpt" id="password_rpt" /> <br />
        email:    <input type="text" name = "remail" id = "remail" /><br />

        <input type = "submit" name="register" id="register"  value = "Sign Up" />
    </form>

What am I missing?


Update: I changed the condition to if($_POST['password'] == $_POST['password-rpt']). Hasn't helped.

  • 写回答

4条回答 默认 最新

  • douyun1860 2011-08-02 15:57
    关注

    You can simply use:

    $_POST['password'] === $_POST['password-rpt']
    

    Also, in your form, you used the name password_rpt, while in your code, you refer to password-rpt. You need to change one of them so that the names match.

    Take a look at the Strings manual page to learn more about string handling in PHP. I also recommend having a look at the Language Reference for general guidance.

    Do keep in mind that PHP is a high level scripting language, and is very different from C++, even if they might look the same. There are things that are very feasible in C++ but not in PHP, and vice versa.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探