473,588 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array as boolean

$result = $class->method(); //returns an array of associative arrays
if($result==TRU E) $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 2000
Brian Smither wrote:
$result = $class->method(); //returns an array of associative arrays
if($result==TRU E) $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==TRU E) 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*******@attgl obal.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==TRU E) 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($someA rray == 1); // FALSE
var_dump($someA rray == TRUE); // TRUE
?>

Of course an explicit comparison would be much better and more reliable:

if (!empty($someAr ray)) {
...
}

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==TR UE) $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==TRU E) 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
5966
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 been reading this ng all semester, and found it very helpful. I just have a stupid problem going on right now. Here's the project: We have to make a little frame with a text field, where the user inputs a number, and a text area where a...
1
1078
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 ListenAddress As String Public MaxConnections As Integer Public EncAES256 As Boolean
6
22035
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. Is it possible to have an array of
2
1844
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 the data is in the array? Button clicks tell the array the rank and suit of the card chosen. The output should be a prompt telling the user what they have in their hand if anything at all. Here is what I have thus far: ****
4
4518
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 = True bNcur = True bN0 = True bN1 = True
5
19576
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 having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
2
2217
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: System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---System.FormatException: Invalid length for a Base-64 char array. at
5
2508
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 int; <= the problem seem like happen at here? private int arrayB = new int; <= the problem seem like happen at here?
2
3743
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 when i do the following. if you add a contact with up to 13 fields it will be stored in a data structure. i have a tabbed pane that will show six of the 13 fields for that contact. when you double click the contact i want it to pop up and show all 13...
0
7929
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7860
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7984
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8223
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6634
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3883
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2371
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.