On Sun, 27 Apr 2008 09:16:02 -0400, Jerry Stuckle wrote:
Brian Smither wrote:
>$result = $class->method(); //returns an array of associative arrays
if($result==TRUE) $cat=$result[0]['element1'];
I've been converting a class from an original and the original class
*apparently* returned an array that could be boolean tested. My conversion
seems to have lost this ability.
if($result)
seems to be testing for non-NULL which gets me what I need, but I don't
want to change these lines in the program. I *can* change the class coding.
Is there something I can add to what the class method returns (the array)
that answers straightaway as a boolean?
If what you have here is correct, I don't see how it could have worked
in the past.
if($result) tests for non-null or non-zero. if($result==TRUE) checks
for 1 or not 1.
An array is non-null/non-zero, so if ($result) returns true. But an
array is not equal to 1, so if ($result == 1) will always be false.
Many routines in this case return the array or false. In this case,
if($result) would work. But if($result == TRUE) won't work unless you
return 1 or not 1.
'zactly.
It's worthwile, especially since so many people reading about php
here are actually looking for results from mysql functions which may
correctly return no data, to rememeber that such a test isn't REALLY
testing whether the result is successful or unsucessful, but rather
whether the test returned any results.
----------------------
$ cat foo.php
<?php
$my_array = array();
if ($my_array) {
print "Array is true\n";
} else {
print "Array is false\n";
}
$my_array = array("first value");
if ($my_array) {
print "Array is true\n";
} else {
print "Array is false\n";
}
?>
$ php foo.php
Array is false
Array is true
$
----------------------
Something using this method may *seem* like it's failing when in fact
it's working perfectly fine, it's just not returning any data. And
that's not the same thing as not working.
--
Better to teach a man to fish than to give him a fish. And if he can't
be bothered to learn to fish and starves to death, that's a good enough
outcome for me.
-- Steve VanDevender