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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post 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'.......
Last edited by scarface222 on Mon Jan 04, 2010 11:37 pm, edited 1 time in total.
wilsonallen01
Forum Newbie
Posts: 1
Joined: Mon Jan 04, 2010 11:10 pm

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

Post by wilsonallen01 »

Nice question and nice answer...
Thanks
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post 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.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

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

Post 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);
 
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post 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?
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post by pickle »

$p=1 is an assignment, not a comparison. It will always evaluate to boolean TRUE.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post by scarface222 »

thanks you have been quite helpful
eFishy
Forum Newbie
Posts: 9
Joined: Tue Jan 05, 2010 12:26 pm

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

Post 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
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

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

Post 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.
Post Reply