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 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
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
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.
(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?)
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
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
..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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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())
{
//...
}
|
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...
|
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...
|
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...
|
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...
|
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;
}
...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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: 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...
| |