If statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gilles Girard

    If statement

    Hi,

    I use a sample code showed in php.net website manual with my
    conditions, and the result is always true.

    <?php if ( $username="myus ername" AND $password="mypa ssword" ) { ?>

    True

    <?php } else { ?>

    False

    <?php } ?>

    Where's the bug?

    Thanks in advance

    Gilles Girard

  • Janwillem Borleffs

    #2
    Re: If statement

    Gilles Girard wrote:[color=blue]
    >
    > I use a sample code showed in php.net website manual with my
    > conditions, and the result is always true.
    >
    > <?php if ( $username="myus ername" AND $password="mypa ssword" ) { ?>
    >[/color]

    That's because '=' is an assignment and '==' is what you are looking for.


    JW



    Comment

    • kingofkolt

      #3
      Re: If statement

      "Gilles Girard" <ggirard@blowup galerie.com> wrote in message
      news:2004081117 093680432%ggira rd@blowupgaleri ecom...[color=blue]
      > Hi,
      >
      > I use a sample code showed in php.net website manual with my
      > conditions, and the result is always true.
      >
      > <?php if ( $username="myus ername" AND $password="mypa ssword" ) { ?>
      >
      > True
      >
      > <?php } else { ?>
      >
      > False
      >
      > <?php } ?>
      >
      > Where's the bug?
      >
      > Thanks in advance
      >
      > Gilles Girard
      >[/color]

      The PHP equals (=) operator assigns variables. So, when your first line is
      this...

      <?php if ( $username="myus ername" AND $password="mypa ssword" ) { ?>

      .... you are assigning $username the value "myusername " and $password the
      value "mypassword ". In PHP, any string that does not equal "" or "0" is
      considered to be true. You must use the comparison operator (==) instead to
      compare the two variables to their respective strings. So your first line
      should be changed to this:

      <?php if ( $username=="myu sername" AND $password=="myp assword" ) { ?>


      Comment

      • Gilles Girard

        #4
        Re: If statement

        On 2004-08-11 17:22:08 -0400, "Janwillem Borleffs" <jw@jwscripts.c om> said:
        [color=blue]
        > Gilles Girard wrote:[color=green]
        >>
        >> I use a sample code showed in php.net website manual with my
        >> conditions, and the result is always true.
        >>
        >> <?php if ( $username="myus ername" AND $password="mypa ssword" ) { ?>
        >>[/color]
        >
        > That's because '=' is an assignment and '==' is what you are looking for.
        >
        >
        > JW[/color]

        It's work.

        Thank you very much

        GG

        Comment

        Working...