Page 1 of 1

quick question why $p=1 and $p==1 not work?

Posted: Mon Jan 04, 2010 11:15 pm
by scarface222
Hey guys quick question. I have a variable that is defined as 1 or 0 depending on what the database INT type entry is. I have an if statement that should be disabled if $p=0 and enabled if $p=1 however if I write $p==1 or $p=1 the if statement always goes. Here is my code snippet. I echoed the number out to make sure and when it equals 0 the statement still goes and I am completely sure it has nothing to do with the other parameters of the if statement. Any help appreciated.

Code: Select all

$permissqry="SELECT permission FROM scrusersonline WHERE id='$id' AND td='$td'";
    $permissionqry=mysql_query($permissqry);
    while ($row = mysql_fetch_assoc($permissionqry)) {
    $permissionrecheck=$row['permission'];
        }
    echo $permissionrecheck;
    
    
if($session->logged_in and $permissionrecheck=='1'.......

Re: quick question why $p=1 and $p==1 not work?

Posted: Mon Jan 04, 2010 11:17 pm
by wilsonallen01
Nice question and nice answer...
Thanks

Re: quick question why $p=1 and $p==1 not work?

Posted: Mon Jan 04, 2010 11:36 pm
by scarface222
I did not give an answer I simply posed a question. The code in color is how you post code on the forum so it is easier to read.

Re: quick question why $p=1 and $p==1 not work?

Posted: Mon Jan 04, 2010 11:46 pm
by manohoo
what values do these have before the IF statement? Do this in line 7 and post the result:

Code: Select all

 
echo "<pre>".var_dump($session->logged_in);
echo "<br />".var_dump($permissionrecheck);
 

Re: quick question why $p=1 and $p==1 not work?

Posted: Tue Jan 05, 2010 10:04 am
by scarface222
I got
bool(true)
string(1) "0"

for the string you pasted. Could it possible be that the database entry is INT? Would it be recognized if it was VARCHAR?

Re: quick question why $p=1 and $p==1 not work?

Posted: Tue Jan 05, 2010 10:27 am
by manohoo

Code: Select all

<?php 
// I am forcing the values into the 2 conditions
$session->logged_in = TRUE;
$permissionrecheck = 0;
 
if($session->logged_in and $permissionrecheck=='1') {
  echo "The condition is TRUE";
} else {
  echo "The condition is FALSE"; 
}
?>
Output: The condition is FALSE
So, whatever code you have in the IF statement would not execute.
for the string you pasted. Could it possible be that the database entry is INT? Would it be recognized if it was VARCHAR?
I am not sure, let's see what values you are trying to use to populate the field.

Post the entire code, there must be an error somewhere else.

Re: quick question why $p=1 and $p==1 not work?

Posted: Tue Jan 05, 2010 11:48 am
by pickle
$p=1 is an assignment, not a comparison. It will always evaluate to boolean TRUE.

Re: quick question why $p=1 and $p==1 not work?

Posted: Tue Jan 05, 2010 2:08 pm
by scarface222
Thanks guys I was using $p=='1' and it seems to work now, maybe it was a server problem or I didn't update the page or something. Thanks for clearing me up with the equal sign pickles I never knew that before and always just adjusted the equal signs until it worked lol. I just thought == was most precise.

Re: quick question why $p=1 and $p==1 not work?

Posted: Tue Jan 05, 2010 2:20 pm
by pickle
= is assignment
== is a value comparison
=== is a value and type comparison

ie:

Code: Select all

$p = 0;
$p == FALSE; // this will evaluate to true, as 0 evaluates to false
$p === FALSE; // this will evaluate to false, as 0 is not the same type as boolean false

Re: quick question why $p=1 and $p==1 not work solved

Posted: Tue Jan 05, 2010 3:08 pm
by scarface222
thanks you have been quite helpful

Re: quick question why $p=1 and $p==1 not work?

Posted: Tue Jan 05, 2010 3:16 pm
by eFishy
Perfect example there, another important one that you will use is

Code: Select all

$var = "test";
$input = "test";
$var != $input; //This would return FALSE
$input = "changed";
$var != $input; //This would return TRUE
// Basically saying $var doesn't equal $input

Re: quick question why $p=1 and $p==1 not work?

Posted: Tue Jan 05, 2010 6:44 pm
by scarface222
Thanks man appreciate your comment, I have been dealing with that situation a lot lately doing similar database queries where I copy and paste them and forget it redefines the variable. That is a useful property to know.