473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Jun 2 '08 #1
3 1987
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.
js*******@attglobal.net
==================

Jun 2 '08 #2
..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
Jun 2 '08 #3
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
Jun 2 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
1
by: OpticTygre | last post by:
I have a structure: Structure SSHConnection Public EventLogErrors As Boolean Public EventLogWarnings As Boolean Public EventLogInformation As Boolean Public Port As Integer Public...
6
by: chech | last post by:
Possible to have array of booleans? Dim b1 As Boolean, b2 As Boolean, b3 As Boolean Dim obj() As Object = {b1, b2, b3} dim v As Object For Each v In obj v = True Next This does not work. ...
2
by: Len via DotNetMonster.com | last post by:
I have to set up a 2d array(13, 4) containing a deck of playing cards. What I am wondering is how to correctly set up the array in the first place, and then how do you access the information once...
4
by: Rich | last post by:
Dim bNcd, bNcm, bNqa, bNcur, bN0, bN1, bN2, bN3, bN4 As Boolean Dim arrBool As Boolean() = {bNcd, bNcm, bNqa, bNcur, bN0, bN1, bN2, bN3, bN4} Dim i As Integer bNcd = True bNcm = True bNqa =...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
2
by: Wayne Sepega | last post by:
I'm receiving the above error from time to time with one of our web pages. What would cause this? Thanks Wayne The following is the stacktrace from the exception: ...
5
by: xirowei | last post by:
public class Result { private int countA = 0; private int countB = 0; private int statement; private boolean statusA = false; private boolean statusB = false; private int arrayA = new...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.