PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > array as boolean
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
array as boolean

Réponse
 
LinkBack Outils de la discussion
Vieux 27/04/2008, 06h41   #1
Brian Smither
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut array as boolean

$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
  Réponse avec citation
Vieux 27/04/2008, 14h16   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array as boolean

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
==================

  Réponse avec citation
Vieux 27/04/2008, 15h08   #3
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array as boolean

..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
  Réponse avec citation
Vieux 27/04/2008, 16h58   #4
Peter H. Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array as boolean

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13495 seconds with 12 queries