Jump to content

Archived

This topic is now archived and is closed to further replies.

aaarrrggh

Please help clarify an if{} problem

Recommended Posts

Hi,

I've actually managed to 'solve' this problem as in my code now does what I want it to do, but I don't fully understand why it wasn't working in the first place, so I thought I would ask around to see if somebody can explain this to me.

I have created a site that allows people to create their own leagues for the game 'pro evolution soccer'. I wanted league admins to be able to put a password on their league, so only people they know or want to join their league can do so.

This is where the weirdness happened. The code seemed to be working fine for a while, then it suddenly allowed people to join even if a password had been set.

Eventually, I found out where the problem lay and got it working, but I don't understand it properly. It turned out that the problem lay in the start of my [i]if[/i] statement.

Here is the offending code:

if ($league_password_check == 0){

all other code goes here

}

The $league_password_check variable was created by checking inside the database to see whether a password had been set. The field type inside mysql is a Varchar(). When a user creates a league, he gets to pick whether he wants a password setting. If the password is not set, the field is set to 0 inside mysql. If it is set, the password is set to an Md5() hash.

The code was always executed, regardless of the value of the $league_password_variable. I even echoed the value inside the variable, and it was still being executed even when the value wasn't zero.

Anyway, when I changed the code to this:


if ($league_password_check == '0'){

all other code goes here

}

It suddenly worked how I wanted it to. The problem is I don't really understand why. Can somebody help clarify this for me?

Hope I've given all the details you need.

Thanks in advance :D

**Edit:

I know... I've posted this in the wrong part of the forum. That's what having 15 windows open in firefox at the same time can do to you lol.

Sorry about that...

Share this post


Link to post
Share on other sites
A bit simplified but, VARCHAR fields hold strings. 0 is an integer, '0' is a string.

Share this post


Link to post
Share on other sites
Thats because if you use 0 on its own PHP recognises this as FALSE. When you place quotes around 0 and 1 PHP treats as numerical values rather than treating them as true or false.

So basically PHP was asking itself whether $legaue_password_check is equal to [b]false[/b]
[code]if ($league_password_check == 0){

all other code goes here

}[/code]
But your new code now tells PHP to check whether $legaue_password_check is equal to [b]0[/b] rather than false.

Share this post


Link to post
Share on other sites
really? i didn't know that doing if ($blah == 0) / if ($blah == 1) was the same as doing ($blah == false) / ($blah == true) ...

do you have a linkie to the manual for that? I'm not calling you a liar or nothing.. i just wanted to read up on it.

Share this post


Link to post
Share on other sites
[a href=\"http://php.net/bool\" target=\"_blank\"]booleans[/a].

Wildteens comments isn't exactly correct. The string "0" does evaluate to FALSE.

From the manual...
[code]
When converting to boolean, the following values are considered FALSE:
...
...

the empty string, and the string "0"
[/code]

Share this post


Link to post
Share on other sites
okay according to the link:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
When converting to boolean, the following values are considered FALSE:

*

the boolean FALSE itself
*

[b] the integer 0 (zero)[/b]
*

the float 0.0 (zero)
*

[b] the empty string, and the string "0"[/b]
*

an array with zero elements
*

an object with zero member variables (PHP 4 only)
*

the special type NULL (including unset variables)
*

SimpleXML objects created from empty tags
[/quote]

i see the int 0 which would explain $blah==0 but it also says string "0" so doesn't that mean that $blah==0 would do the same thing as what he has now with $blah=="0" ?

Share this post


Link to post
Share on other sites
[!--quoteo(post=382892:date=Jun 12 2006, 11:54 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 12 2006, 11:54 AM) [snapback]382892[/snapback][/div][div class=\'quotemain\'][!--quotec--]
okay according to the link:
i see the int 0 which would explain $blah==0 but it also says string "0" so doesn't that mean that $blah==0 would do the same thing as what he has now with $blah=="0" ?
[/quote]


Hmm.

Yeah, how come my code was always executed to begin with though? I mean,


if ($league_password_check == 0){

all other code goes here

}

Should not be executed if there is something other than '0' inside the $league_password_check variable should it? If I'm holding a md5 hash inside that variable, surely it would test as true in the if statement?

Share this post


Link to post
Share on other sites
can you do me a flavor and post your query string, query command, and all that stuff, that leads up to this if statement?

Share this post


Link to post
Share on other sites
[!--quoteo(post=382918:date=Jun 12 2006, 12:47 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 12 2006, 12:47 PM) [snapback]382918[/snapback][/div][div class=\'quotemain\'][!--quotec--]
can you do me a flavor and post your query string, query command, and all that stuff, that leads up to this if statement?
[/quote]

No probs:

$check_for_password = "SELECT
`teams`.idteams,
`leagues`.league_password
FROM
`leagues`
INNER JOIN `teams` ON (`leagues`.league_id = `teams`.league_id)
WHERE
(`teams`.idteams = $team_id_no)";

$do_check_for_password = mysql_query($check_for_password) or die(mysql_error);

while ($row = mysql_fetch_array($do_check_for_password)){

$league_password_check = $row['league_password'];

}

Share this post


Link to post
Share on other sites
The numeric value of a string is zero (unless it begins with 1 - 9), so
[code]
$password = 'f4f5aab345';

if ($password == 0)
    echo 'true';
else
    echo 'false';    [/code]

gives --> true

However
[code]$password = 'f4f5aab345';

if ($password == '0')
    echo 'true';
else
    echo 'false';    [/code]

--> false

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.