Page 1 of 1

Little help with booleans please...

Posted: Mon Jun 10, 2002 1:52 am
by Kadina
I can't figure out why this simple piece of code wont work....can someone clue me in please?

Code: Select all

echo $userLevel;
   if($userLevel=="7"){
$isAdmin = True;
   echo("isAdmin: " . $isAdmin);
}else{
   $isAdmin = False;
   echo("isAdmin: " . $isAdmin);
}

....some other stuff here....

if($isAdmin){
  echo("Welcome to the Admin Area");
  ....buncha otherstuff...
}
Now, if $userLevel = 7, I get this for output:

7isAdmin: 1

but I never get the "Welcome to the Admin Area" and the stuff below it.

If $userLevel = (anything else, usually 5 in my case), I get this for output:

5isAdmin:

but I also never get into the "Welcome to the Admin Area", which is correct.
My question is, why doesnt if($isAdmin) evaluate to true when $isAdmin is set to true, and why is $isAdmin empty when I try to set it to false?

Posted: Mon Jun 10, 2002 2:31 am
by volka
it's because of the representation/interpretation of boolean values, that FALSE is printed as <nothing>.

why not change if($isAdmin){
to if($userLevel==7){
?

Posted: Mon Jun 10, 2002 3:32 am
by Wayne
Try

Code: Select all

$isAdmin = "True";

Posted: Mon Jun 10, 2002 3:42 am
by twigletmac
Wayne wrote:Try

Code: Select all

$isAdmin = "True";
That will make it into a string instead of a boolean.

Mac

Posted: Mon Jun 10, 2002 3:45 am
by Wayne
Looking at the way it was coded that is what Kadina was trying to do?

Posted: Mon Jun 10, 2002 3:55 am
by twigletmac
Yes they were trying to set $isAdmin to a value of true or false but no they weren't trying to do that with a string value but with a boolean. Basically a boolean can only have a value of TRUE or FALSE and is set by doing:

Code: Select all

$my_bool = True;
// or
$my_bool = False;
You can then test for its value by using,

Code: Select all

if ($my_bool) &#123;
// if true
&#125; else &#123;
// if false
&#125;
Mac