SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Comparing Variables in PHP

    Hello,

    Here is my problem.
    I have a form in which I ask the future user to type his/her password twice.
    I want to compare those two passwords.

    I have:

    PHP Code:
    <form ....>
    <
    input type ="password" name ="password1" >
    <
    input type ="password" name ="password2" 
    Once the form is submitted:

    PHP Code:
    $password1 '$password1';
    $password2 '$password2';

    if (
    '$password1' <> '$password2' ) { 
    error("You have entered 2 different passwords. \\n  Please try again \\n " ); 

    How come it doesn't work?
    Ginie G.

  2. #2
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this instead:
    PHP Code:
    <?php
    // once the form is submitted
    if($password1!=$password2){
         
    // passwords do not match
         
    echo('<p>Passwords do not match!</p>');
    }
    else{
         
    // passwords match
         // continue with submission
    }
    ?>
    The reason your script did not work was because you reassigned both variables ($password1 and $password2) new values, in this case you assigned $password1 the value of "$password1" and $password2 the value of "$password2" instead of preserving the submitted values.

    When 'single quotations' are used, the data they surround is held as a literal value -- meaning even if $variable is inside of single quotes, it is interpreted by PHP as as "$variable" instead of the value that $variable contains. Make sense?

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  3. #3
    SitePoint Addict Clete2's Avatar
    Join Date
    Mar 2002
    Location
    Columbia, South Carolina
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:

    PHP Code:
    <?

    $password1 
    "OOP"
    $password2 "OOP"

    if (
    $password1 == $password2 ) { 
    die(
    "Passwords are the same! :)" ); 
    }
     echo 
    "You have entered 2 different passwords. \n Please try again \n";
    ?>
    My Blog
    My Photography Website
    Linux user #339185

  4. #4
    SitePoint Addict Clete2's Avatar
    Join Date
    Mar 2002
    Location
    Columbia, South Carolina
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what the... I put and it put the img src tag... just in that code, replace the tag w/ : ) or something
    My Blog
    My Photography Website
    Linux user #339185

  5. #5
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remove this part of your code or comment it out:
    Code:
    $password1 = '$password1';
    $password2 = '$password2';
    it's not necessary to reassign the variables since they are available once the form is posted. Also, by putting single quotes around the password vars you're not assigning waht the user entered, but rather you're assigning the string $password1/2 as just that, $password1/2

    Here, change the if condition by removing the quotes too, like so:
    Code:
    if ($password1 <> $password2 ) { 
    error("You have entered 2 different passwords. \n  Please try again \n " ); 
    }
    }


  6. #6
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, it worked!

    So, if I understood well:

    != is for values variables
    <> is for variables

    Right?
    Ginie G.

  7. #7
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The comparison operators != and <> mean the same thing: translated they mean Does Not Equal


  8. #8
    SitePoint Addict Clete2's Avatar
    Join Date
    Mar 2002
    Location
    Columbia, South Carolina
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but == means equals right? what would = mean then?
    My Blog
    My Photography Website
    Linux user #339185

  9. #9
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A double equals ( == ) means is equal to, or in the case of comparing variables $A==$B asks is $A the same as $B

    The single equals ( = ) is called the assignement operator, by which you ASSIGN a value on the right of the = to the variable on the left:

    $X = "George W. Bush";


  10. #10
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally quoted by Clete2
    but == means equals right? what would = mean then?
    Not exactly. == is used when comparing two or more values; = is used when assigning a value.

    Also, here's an operator table with the most common operators in PHP:
    Code:
    $var1=5, $var2=10
    |--------------------------------------------------------------------|
    | operator | what it means              | expression  | evaluates to |
    |--------------------------------------------------------------------|
    | ==       | is equal to                | $var1==$var2| false        |
    |--------------------------------------------------------------------|
    | !=       | is not equal to            | $var1!=$var2| true         |
    |--------------------------------------------------------------------|
    | >        | is greater than            | $var1>$var2 | false        |
    |--------------------------------------------------------------------|
    | <        | is less than               | $var1<$var2 | true         |
    |--------------------------------------------------------------------|
    | >=       | is greater than or equal to| $var1>=$var2| false        |
    |--------------------------------------------------------------------|
    | <=       | is less than or equal to   | $var1<=$var2| true         |
    |--------------------------------------------------------------------|


    -Colin
    Last edited by Aes; Mar 27, 2002 at 00:10.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •