Jump to content

Am I Tired? Please help


bitagenda

Recommended Posts

Just a question

the PHP operators && as and are the same? I do understand Yes they are, only with different precedence.

 

so Why?

$dat = true and true;  // results in true  // OK

$dat = true and false  // results in true????????? shouldn't it be false?

 

$dat = true && true  // results in true // OK

$dat = true && false  // results in false // OK

 

Done a test with each combination ...

 

  $dat = $dat?'yes';'no';

  echo $dat;

 

 

myKey: ofdl

Link to comment
https://forums.phpfreaks.com/topic/39867-am-i-tired-please-help/
Share on other sites

Can you show the actual code, because what you've posted doesn't make much sense...

 

I did not write the code, because it is a long function

but it is something like this

$secure =  isset($_SESSION['login']) and ($_SESSION['login']['loginuser']==$SiteAdmin or $_SESSION['login']['loginuser']==$SiteMaster);

 

I did ckeck value by value, for example they were true and (false or false);

Surprisse!! results in true???

So I did a small test, the one posted

$secure = true and (false or false)  Zas the same result

then

$secure = true and false  Zas the same result of true

then

$secure = true && false  the results is ok

 

So please, help me what I do not understand?

Is it a bug?

to me the result of true and false should be as FALSE as true && false

Thanks

 

Order of operations.

 

Here:

 

<?php
$a = true;
$b=false;
$secure = ($a and $b);
$res = $secure?'yes':'not';
echo $res;
?>

 

Best,

 

Patrick

 

RIGHT PATRICK !!!! I am tired, no doubt about it.

 

Just if others read about this ... take care with an error like this.

 

The reason the result is TRUE

 

With NO parenthesis PHP assigns first $secure = $a, which is true, so it will remain true.

$secure = $a and $b;

then PHP does 'and b' with result in false but assigned to nothing.

so to get the right answer

$secure = ($a and $b)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.