|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
$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? -- Remove INVALID from e-mail address. Brian Smither Smither Consulting |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
..oO(Jerry Stuckle)
>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. It checks for the boolean TRUE, not for the integer 1. That's a difference, because in this case it determines what the first operand will be converted to if necessary. A non-empty array always evaluates to TRUE, so the above condition will be TRUE as well. >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. Correct, but ... >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. .... it will work for non-empty arrays: <?php $someArray = array(42); var_dump($someArray == 1); // FALSE var_dump($someArray == TRUE); // TRUE ?> Of course an explicit comparison would be much better and more reliable: if (!empty($someArray)) { ... } Micha |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|