473,800 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'0' passed to function treated as 'null'

I have a lengthy 'markup.php' file which consists of functions which
take as input the various attribute values of HTML tags and spit out
the HTML tag. My problem is that the function for <inputis
interpreting '0' as null and because I've written the functions so
that they don't write out the attributes which have null value, it
doesn't generate a 'value="0"' attribute for <input>.

Simplified example:

function input($value=nu ll) {

$input = '<input';
if ($value)
$input .= ' value="' . $value . '"';
else
if ($type == 'checkbox' || $type == 'radio')
echo('you must specify a value for the attribute "value" of
<input>');
$input .= ' />';
return $input;
}

So... my question is, how - when passing the argument '0' for $value -
do I make PHP understand that 0 is not null, and so it should go ahead
and write 'value="0"' instead of throwing my error msg?

thx in adv
Dec 28 '07 #1
7 1710
rynato wrote:
So... my question is, how - when passing the argument '0' for $value -
do I make PHP understand that 0 is not null, and so it should go ahead
and write 'value="0"' instead of throwing my error msg?
RTFM about type juggling and the === operator. That should clear things up.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

MSN:i_********* *************** *@hotmail.com
Jabber:iv****** ***@jabber.org ; iv*********@kde talk.net
Dec 28 '07 #2
rynato said:
I have a lengthy 'markup.php' file which consists of functions which
take as input the various attribute values of HTML tags and spit out
the HTML tag. My problem is that the function for <inputis
interpreting '0' as null and because I've written the functions so
that they don't write out the attributes which have null value, it
doesn't generate a 'value="0"' attribute for <input>.

Simplified example:

function input($value=nu ll) {

$input = '<input';
if ($value)
$input .= ' value="' . $value . '"';
else
if ($type == 'checkbox' || $type == 'radio')
echo('you must specify a value for the attribute "value" of
<input>');
$input .= ' />';
return $input;
}

So... my question is, how - when passing the argument '0' for $value -
do I make PHP understand that 0 is not null, and so it should go ahead
and write 'value="0"' instead of throwing my error msg?

thx in adv
if ($value || $value == 0)

~A!

--
Anthony Levensalor
an*****@mypetpr ogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #3
uh, nevermind. I figured it out. For posterity's sake here's the
solution:

instead of:

if ($value)

I changed that conditional to:

if ($value != null || $value === 0)

the value was passed into the function correctly (it still equalled 0)
but for some reason 'if ($value)' was not sufficient for PHP to
distinguish between a value of 0 and no value at all. Can someone
explain this distinction to me? Thanks.
Dec 28 '07 #4
(that is to say, *I* understand the difference between 0 and null. Why
was ($value) insufficient for PHP to distinguinsh between a value of 0
and no value for $value?)
Dec 28 '07 #5
rynato said:
uh, nevermind. I figured it out. For posterity's sake here's the
solution:

instead of:

if ($value)

I changed that conditional to:

if ($value != null || $value === 0)

the value was passed into the function correctly (it still equalled 0)
but for some reason 'if ($value)' was not sufficient for PHP to
distinguish between a value of 0 and no value at all. Can someone
explain this distinction to me? Thanks.
Binary notation: 1 is true, zero is false.

Extrapolated into most programming languages, 0 = false, all other
numbers usually = true when evaluated in boolean expressions

~A!

--
Anthony Levensalor
an*****@mypetpr ogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #6
rynato wrote:
if ($value != null || $value === 0)
Wrong. The correct solution is:

if ($value !== null)

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Trying to make bits uncopyable is like trying to make water not wet.
-- Bruce Schneier
Dec 28 '07 #7
..oO(rynato)
>uh, nevermind. I figured it out. For posterity's sake here's the
solution:

instead of:

if ($value)

I changed that conditional to:

if ($value != null || $value === 0)
if (!is_null($valu e)) {
...
} else {
...
}
>the value was passed into the function correctly (it still equalled 0)
but for some reason 'if ($value)' was not sufficient for PHP to
distinguish between a value of 0 and no value at all. Can someone
explain this distinction to me? Thanks.
It's explained in the manual (type juggling).

The 'if' statement always expects a boolean expression. If you just pass
a single variable to it like in your case, its type will automatically
be converted to a boolean (also explained in the manual in more detail).
In short: Zero values, empty strings, empty arrays and NULL always
evaluate to FALSE, anything else to TRUE:

0 == 0.0 == '0' == '' == array() == NULL == FALSE

Micha
Dec 28 '07 #8

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

Similar topics

1
3838
by: Adam Dyga | last post by:
Hi, How to create function with optional parameter passed by reference? I've tried sth like this: function (&$param=NULL) // or: function (&$param=new array()) { //... }
4
4527
by: _ivan | last post by:
Hello, So I think the answer to this question is going to be rather simple. Basically I have a pointer to a class, and I want to have a function to call to initialize it. After this function is called, the pointer is still NULL. Here is a simplified version, but basically the same:
4
6185
by: Gerry Abbott | last post by:
Hi All, Im trying to use thie combination but have not had success. Below is the function It tried the following myriskLevel(2,2) myrisklevel(0,0,2) and the ismissing(Three) alwasy returns false.?
6
2698
by: Ruben | last post by:
I'm trying to pass an array of string to a function without knowing how many strings I have beforehand. I've defined one functions as char * insert(char table,int cols, char values); out of deperation because when I defined it as char * insert(char table,int cols, char values)
24
2632
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
5
7708
by: Aman Sura | last post by:
:confused: This is just an absurd piece of code. Can anyone please explain the reason? //a function that nulls the array passed as parameter function Nullify(arrRef) { arrRef = null; } var arr = new Array();
89
6086
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
50
3341
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to pass a value to f()? "None" doesn't seem to work.. Thanks in advance.
4
3218
by: Nathan Baker | last post by:
I have a JavaScript environment using IActiveScript and friends (via COM interop). At one point, the JavaScript passes me a function as a parameter to an object that has been inserted into the JavaScript using AddNamedItem. When I look at this object and ask it for its type, it says it is a System.__ComObject. What I want to do is invoke this function in the environment that I have using the engine I have instantiated. Is there a way to...
0
9555
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
10287
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
10260
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
9099
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
7588
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
6826
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();...
0
5479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4156
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
2
3770
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.