2

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.

2
  • First of all, your code is missing a ) Commented Aug 2, 2011 at 15:57
  • Probably a typo in your form that we can't see. This isn't your actual code since it wouldn't execute with the missing parentheses. Commented Aug 2, 2011 at 15:57

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I didn't know that. C++ can't do that and my book didn't mention it.
@Yitzchak, updated answer to reflect changes in the question.
yes. I realized that while I was looking over my edits before saving. I am officially an idiot.
What if '1e3' == '1000'?
2

For passwords you really want to be using === (identical) as == (equal) will return true even if case does not match...

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

3 Comments

Thanks for the advice. Where can I read more about php string operations?
I stand corrected on my case comment it was incorrect. I also, however, stand behind on my use of the === operator as things like $pass1 = true, $pass2 = "mypassword", ($pass1 == $pass2) will evaluate to true. Basically because of php's loose and dynamic typing there are a lot of ways that == can yield unwelcome results.
+1 for the answer. you should never use the equal operator for strings. correct awenser is the identical operator ===.
0

Keep in mind that strcmp() returns:

  • -1 if str1 is less than str2;
  • 1 if str1 is greater than str2;
  • 0 if they are equal;

Similar to == (equal operator) as in strcmp('1e3', '1000') (return value 0 ), or '1e3'=='1000' (true).

Note, PHP 7 have a similar operator, spaceship operator (<=>) and have the same behaviour than strcmp().

In your case you should use :

if ($_POST['password'] === $_POST['password-rpt'])

Comments

-1

Why are you using strcmp()? You'd get the desired result if you did

if ($_POST['password'] == $_POST['password-rpt'])

2 Comments

That's because he probably is used to C in which == won't work on strings, since it returns only true if the char pointers are equal.
wrong answer. '1e3' == '1000' returns true, and its not the intention

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.