If statement

This is a discussion on If statement within the PHP Language forums, part of the PHP Programming Forums category; Hi, I use a sample code showed in php.net website manual with my conditions, and the result is always ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-11-2004
Gilles Girard
 
Posts: n/a
Default 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="myusername" AND $password="mypassword" ) { ?>

True

<?php } else { ?>

False

<?php } ?>

Where's the bug?

Thanks in advance

Gilles Girard

Reply With Quote
  #2 (permalink)  
Old 08-11-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: If statement

Gilles Girard wrote:
>
> I use a sample code showed in php.net website manual with my
> conditions, and the result is always true.
>
> <?php if ( $username="myusername" AND $password="mypassword" ) { ?>
>


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


JW



Reply With Quote
  #3 (permalink)  
Old 08-11-2004
Gilles Girard
 
Posts: n/a
Default Re: If statement

On 2004-08-11 17:22:08 -0400, "Janwillem Borleffs" <jw@jwscripts.com> said:

> Gilles Girard wrote:
>>
>> I use a sample code showed in php.net website manual with my
>> conditions, and the result is always true.
>>
>> <?php if ( $username="myusername" AND $password="mypassword" ) { ?>
>>

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


It's work.

Thank you very much

GG

Reply With Quote
  #4 (permalink)  
Old 08-11-2004
kingofkolt
 
Posts: n/a
Default Re: If statement

"Gilles Girard" <ggirard@blowupgalerie.com> wrote in message
news:2004081117093680432%ggirard@blowupgaleriecom. ..
> Hi,
>
> I use a sample code showed in php.net website manual with my
> conditions, and the result is always true.
>
> <?php if ( $username="myusername" AND $password="mypassword" ) { ?>
>
> True
>
> <?php } else { ?>
>
> False
>
> <?php } ?>
>
> Where's the bug?
>
> Thanks in advance
>
> Gilles Girard
>


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

<?php if ( $username="myusername" AND $password="mypassword" ) { ?>

.... 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=="myusername" AND $password=="mypassword" ) { ?>


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:03 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0