Corrupted Return Var or Boolean Bungle?

Ok, this one is so simple but it’s got me completely stumped. Can anyone explain this odd behavior?

Simple script runs in response to a web form. At this point I’ve reduced this down to the minimum, I’m actually NOT even doing anything with the form data at this point. The reduced pattern still stumps me,
[php]
echo(‘START…
’);

$bResult = true;
$callscriptOK = false;
$uploadOK = false;

echo(" - bR is [$bResult]
");
if ($bResult) {
echo(’ - Passed $bResult is True
’);
}
echo(“uploadOK = $uploadOK
”);
echo(‘uploadOK = ‘.$uploadOK.’
’);
[/php]

Two conditions. Set, $bResult to true or false. As listed, true, returns,
[tt]
START…

  • bR is [1]
  • Passed $bResult is True
    uploadOK =
    uploadOK =
    [/tt]
    setting $bResult to false,
    [tt]
    START…
  • bR is []
    uploadOK =
    uploadOK =
    [/tt]

The greater problem was, $bResult represents a call to another function, when that was returning false, the value of other variables looked corrupted. $uploadOK appeared corrupted yet, when the function return was true, $uploadOK was all good! I’ve reduced this to the bare minimum, but either way $uploadOK is dead. And $bResult (or $bR for short) only displays when it’s true! I’m stumped.

Thanks a million for any clarification here.

Jim

You seem to not understand exactly how if works. Look at the manual.

Because false equals an empty string. True equals 1.

An experiment to showcase this.

[php]

$values = [
true,
false,
null
];

foreach( $values as $v){
echo "

Without encoding, I display as: " . $v . “

”;
echo "

With encoding, I display as: " . json_encode($v) . “

”;
}[/php]

Wow, I like the language but the C/C++ blend with Visual Basic makes my head hurt!

Ok, so I was reading from here,
http://php.net/manual/en/language.types.boolean.php (sorry can’t post links)

And was specifically assigning True or False to my variable initially so as to insure this wasn’t a typing problem. I guess that was incorrect. So,

When converting to boolean, the following values are considered FALSE

I think that works out now, thanks. Now my minimalist example is too simple, and within the greater context all works as expected.

Funny, didn’t have anything to do with ‘if’!

Jim

Sponsor our Newsletter | Privacy Policy | Terms of Service