Jump to content


Photo

Please help clarify an if{} problem


  • Please log in to reply
9 replies to this topic

#1 aaarrrggh

aaarrrggh
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 12 June 2006 - 04:03 PM

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 if 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...

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 12 June 2006 - 04:35 PM

A bit simplified but, VARCHAR fields hold strings. 0 is an integer, '0' is a string.

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 12 June 2006 - 04:42 PM

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 false
if ($league_password_check == 0){

all other code goes here

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

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 12 June 2006 - 04:45 PM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 12 June 2006 - 04:51 PM

[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...
When converting to boolean, the following values are considered FALSE:
...
...

the empty string, and the string "0"


#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 12 June 2006 - 04:54 PM

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
*

the integer 0 (zero)
*

the float 0.0 (zero)
*

the empty string, and the string "0"
*

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" ?

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 aaarrrggh

aaarrrggh
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 12 June 2006 - 05:10 PM

[!--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) View Post[/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?

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 12 June 2006 - 05:47 PM

can you do me a flavor and post your query string, query command, and all that stuff, that leads up to this if statement?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 aaarrrggh

aaarrrggh
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 12 June 2006 - 06:13 PM

[!--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) View Post[/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'];

}

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 12 June 2006 - 06:55 PM

The numeric value of a string is zero (unless it begins with 1 - 9), so
$password = 'f4f5aab345';

if ($password == 0) 
    echo 'true';
else
    echo 'false';    

gives --> true

However
$password = 'f4f5aab345';

if ($password == '0') 
    echo 'true';
else
    echo 'false';    

--> false


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users