Jump to content

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...
Link to comment
https://forums.phpfreaks.com/topic/11786-please-help-clarify-an-if-problem/
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.
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.
[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]
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" ?
[!--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?
[!--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'];

}
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.