473,890 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing Boolean to a Function

I'm having an issue passing a boolean to the constructor of an object.
For some reason, if I pass false into the constructor, it doesn't
register. If I pass an integer 0, it does. PHP 5 Code below:

---------------------
class Test {
var $testBool;

public function __construct($te stBool) {
print "testBool = $testBool";
}
}

$testing = new Test(false); // this just prints "testBool = "

$testing = new Test(0); // this prints "testBool = 0"

--------------------
Seems to be both methods should print 0 or false. What have I missed?

Jul 9 '06
18 6065
Rik
David Haynes wrote:
Greg Scharlemann wrote:
>David Haynes wrote:
>>Also, var is deprecated in PHP5.
Thanks!
Your test case also worked... but I'm going to make my question a
little more complicated...

That boolean value is going to be written to a MySQL database. I've
set the column to be of type Bool (which appears to turn into a
Tinyint(1)), is that the best approach? Should I use enum('true',
'false') or some other approach?

I would translate the 0/1 from MySQL into true/false at the lowest
level I could. It's not all that hard to unroll it as follows:

$result = mysqli_query($s ql);
while( $row = mysql_fetch_ass oc($result) ) {
$isArchived = $row['is_archived'] ? true : false;
...
}

$isArchived = (bool)$row['isarchived'];
it's shorter, and by using $row['is_archived'] ? you're already evaluating
the boolean value.
http://www.php.net/manual/en/languag...oolean.casting

Grtz,
--
Rik Wasmus
Jul 11 '06 #11
"Greg Scharlemann" <gr************ **@gmail.comwro te in message
news:11******** *************@7 5g2000cwc.googl egroups.com...
Finally, another somewhat semi-related newbie question. I come from a
java background with getter and setter methods. I modified the Test
class David created with what I thought is a getter method, however it
doesn't work... The function just prints: "()" PHP is not as straight
forward as I hoped...
print "testing->getTestBool( ) = $testing->getTestBool()" ;
?>
This is evaluated actually like this would be :
'testing->getTestBool( ) = '.$testing->getTestBool.'( )';

and since you have not property getTestBool, $testing->getTestBool returns
nothing. therefore it's "testing->getTestBool( ) = ".[nothing]."()"; that's
why it prints only the empty parenthesis.

Functions are not executed within strings (you'd have to be using eval but
you don't want that). So you need to execute the getter method outside the
string:

print "testing->getTestBool( ) = " . $testing->getTestBool( );

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi. net | Gedoon-S @ IRCnet | rot13(xv***@bhg byrzcv.arg)
Jul 11 '06 #12
Rik wrote:
David Haynes wrote:
>$result = mysqli_query($s ql);
while( $row = mysql_fetch_ass oc($result) ) {
$isArchived = $row['is_archived'] ? true : false;
...
}


$isArchived = (bool)$row['isarchived'];
it's shorter, and by using $row['is_archived'] ? you're already evaluating
the boolean value.
http://www.php.net/manual/en/languag...oolean.casting

Grtz,
Rik,
$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really the
(bool) cast is a no-op. My bad. You could just use $isArchived =
$row['isarchived']

-david-

Jul 11 '06 #13
Rik
David Haynes wrote:
Rik wrote:
>David Haynes wrote:
>>$result = mysqli_query($s ql);
while( $row = mysql_fetch_ass oc($result) ) {
$isArchived = $row['is_archived'] ? true : false;
...
}


$isArchived = (bool)$row['isarchived'];
it's shorter, and by using $row['is_archived'] ? you're already
evaluating
the boolean value.
http://www.php.net/manual/en/languag...oolean.casting
>>
Grtz,

Rik,
$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really the
(bool) cast is a no-op. My bad. You could just use $isArchived =
$row['isarchived']
What do you mean by no-op? I'm no native speaker, please explain. And
already 0/1? 0/1 is an integer, not a boolean.

<?php
$row['test'] = 1; //so, integer 1, can be cast to true
$istrue = (bool)$row['test'];
echo '$row[test] can be cast to ';
echo ($row['test'])?'true':'false ';
$string = "\n%s is %sa boolean";
printf($string, '$row[test]',($row['test']===true)? '' :'not ');
printf($string, '$istrue',($ist rue===true)? '' :'not ');
?>

Grtz,
--
Rik Wasmus
Jul 11 '06 #14
Rik wrote:
David Haynes wrote:
>>Rik wrote:
>>>David Haynes wrote:

$result = mysqli_query($s ql);
while( $row = mysql_fetch_ass oc($result) ) {
$isArchiv ed = $row['is_archived'] ? true : false;
...
}
$isArchive d = (bool)$row['isarchived'];
it's shorter, and by using $row['is_archived'] ? you're already
evaluating
the boolean value.

http://www.php.net/manual/en/languag...oolean.casting
>>>Grtz,

Rik,
$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really the
(bool) cast is a no-op. My bad. You could just use $isArchived =
$row['isarchived']


What do you mean by no-op? I'm no native speaker, please explain. And
already 0/1? 0/1 is an integer, not a boolean.

<?php
$row['test'] = 1; //so, integer 1, can be cast to true
$istrue = (bool)$row['test'];
echo '$row[test] can be cast to ';
echo ($row['test'])?'true':'false ';
$string = "\n%s is %sa boolean";
printf($string, '$row[test]',($row['test']===true)? '' :'not ');
printf($string, '$istrue',($ist rue===true)? '' :'not ');
?>

Grtz,
Rik,

No-Op is from Assembler - "No Operation", although it is more correctly
referred to as "noop". It's an instruction which does nothing but take
time and memory. Useful when you need to delay a couple of CPU cycles
for something else to occur.

And you are correct - the (bool) is needed if you want the value to be a
boolean. However, if you're only going to test it, you could leave it
as an integer 1/0 instead of true/false; the result would be the same.

Personally I prefer the boolean. But that's only my preference; in most
cases there's no technical reason to choose one over the other.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jul 11 '06 #15
Rik wrote:
>$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really the
(bool) cast is a no-op. My bad. You could just use $isArchived =
$row['isarchived']

What do you mean by no-op? I'm no native speaker, please explain. And
already 0/1? 0/1 is an integer, not a boolean.
Due to the loose type association false (boolean) == 0 (integer).
Similarly, true (boolean) == (not) 0 (integer)

So, given that a Tinyint(1) can only return 0 and 1 this has a direct
1:1 mapping through the implied type casting of PHP to false and true.

That's what I meant by no-op (PHP will handle it for you)

-david-

Jul 11 '06 #16
Jerry Stuckle wrote:
No-Op is from Assembler - "No Operation", although it is more correctly
referred to as "noop". It's an instruction which does nothing but take
time and memory. Useful when you need to delay a couple of CPU cycles
for something else to occur.

And you are correct - the (bool) is needed if you want the value to be a
boolean. However, if you're only going to test it, you could leave it
as an integer 1/0 instead of true/false; the result would be the same.

Personally I prefer the boolean. But that's only my preference; in most
cases there's no technical reason to choose one over the other.
Wow! Now I really feel like a programming fossil. LOL!
Guess all those days of assembler programming are catching up on me. ;-)

RE: using (bool)
I agree. Anything that makes the type/intent clearer should be
encouraged.

-david-

Jul 11 '06 #17
Why are you complicating your life. Just do the following

print (int) false; // prints 0
print (int) true; // prints 1
Greg Scharlemann wrote:
I'm having an issue passing a boolean to the constructor of an object.
For some reason, if I pass false into the constructor, it doesn't
register. If I pass an integer 0, it does. PHP 5 Code below:

---------------------
class Test {
var $testBool;

public function __construct($te stBool) {
print "testBool = $testBool";
}
}

$testing = new Test(false); // this just prints "testBool = "

$testing = new Test(0); // this prints "testBool = 0"

--------------------
Seems to be both methods should print 0 or false. What have I missed?
Jul 11 '06 #18
Rik
David Haynes wrote:
Rik wrote:
>>$row['isarchived'] is already a 0/1 value (Tinyint(1)), so really
the (bool) cast is a no-op. My bad. You could just use $isArchived =
$row['isarchived']

What do you mean by no-op? I'm no native speaker, please explain.
And already 0/1? 0/1 is an integer, not a boolean.

Due to the loose type association false (boolean) == 0 (integer).
Similarly, true (boolean) == (not) 0 (integer)

So, given that a Tinyint(1) can only return 0 and 1 this has a direct
1:1 mapping through the implied type casting of PHP to false and true.

That's what I meant by no-op (PHP will handle it for you)
It does in most cases, however, if one states:"I would translate the 0/1
from MySQL into true/false at the lowest level I could.", one provides an
answer :-).

Grtz,
--
Rik Wasmus
Jul 11 '06 #19

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

Similar topics

12
2903
by: harry | last post by:
I have an object that's passed in to a function as a parameter i.e public boolean getProjectTitle(ProjectHeader_DTO obj) {...} If I then call a method on this object inside the function i.e obj.setTitle("test") then using obj.getTitle() from outside the function it displays the property set correctly! But doing something like this inside the function doesn't - ProjectHeader_DTO x = new ProjectHeader_DTO();
20
2014
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> bool1=True lst1= def func1(arg1): arg1.append(4)
1
1897
by: Eric Ellsworth | last post by:
Hi, I'm having trouble passing an array to a function, like this: Dim sFieldsNotRequired(11), sFieldsWithSpecificValues(5) As String Dim sCaseErrMsgs As String Dim vFieldsSpecificValues(5) As Variant sFieldsNotRequired(0) = "middleName"
2
3030
by: Manish | last post by:
Hi, I m facing a problem in re-writing a code from VB to C #. We are using a third party DLL to develop a application of Fax. The current application is already written in VB and we have to convert it into C#. In VB we are using a function from the DLL whose signature is like : Declare Function RFVB_SendFiles1 Lib "RF2VB.DLL" (ByVal hServer As Long, _ ByVal lFI10Object As
7
2487
by: DareDevil | last post by:
I have written a method that should modify the folder path passed to it into one that exists and is selected by the user. It then returns a boolean depending on whether a folder path was selected by the user It then dawned on me that I was passing in a readonly property into the method yet neither at compile time or runtime was I getting any kind of error or warning. I tested with a simple string field and it alter the string as expected but...
5
3520
by: James Wong | last post by:
Dear all, I've a web service function and it contains a parameter in System.Text.Encoding. I found that the data type of this parameter in caller application becomes MyWebSvcName.Encoding (where "MyWebSvcName" is the name of my web service). I don't know how to assign value to this parameter. (I'm using VB.NET 2003.) Here are some sample codes:
2
1951
by: Jacques Wentworth | last post by:
Hi I'm trying to pass a collection from a .NET DLL to a VB6 app. I get a error 13 (type mismatch) when I do so. I passed back a boolean variable without any problems, but when I try the collection I get the error. As you can see the function does not do anything at the moment, so it must be some issue passing the collection. Thanks
10
13073
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var boldLink=document.getElementById('cmtbold');
4
5946
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh them I just set their datasource. I am guessing this is probably what is causing the problem. Is there a better way to do this? Anyway this all works happily and things show up when the record already exists but I have 2 problems ; 1) When I add...
0
9980
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
9826
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
11236
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
10830
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
10925
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
9641
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...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4682
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.