473,394 Members | 1,645 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.

'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=null) {

$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 1679
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*********@kdetalk.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=null) {

$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*****@mypetprogrammer.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*****@mypetprogrammer.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($value)) {
...
} 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
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
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...
4
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...
6
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...
24
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...
5
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; } ...
89
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...
50
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
0
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...

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.