...

View Full Version : Value=0 is not a value?



Gil
07-08-2002, 04:30 PM
Hi there,

<?php

if ($b03 && $b04 && $b05 && $b07 && $b07 && $b09 && $b10)

{

?>

display this line

<?php
//endif
}?>

$b03 has a value of "0" - Why does php treat "0" as no value? No value would be if nothing is entered in this case. 0 is a possible value.

Can someone explain this to me? Also, if I'm coding things the wrong way here please tell me!

Thanks,

Gil

ObiwanJebroni
07-08-2002, 04:44 PM
What's the error? I'm not that familiar with the 0 thing, but if its a string, it should be handled correctly. When you set $B03, was it with quotes or just the value. Actually, that shouldn't be the problem but...maybe its treated as a null value since 0 does nothing in PHP. Try making it a string.

Gil
07-08-2002, 05:49 PM
Hi There,

There's no error. It's quite a simple little script with if/else statements.

$bo3 is a textfield without a default value. I'm checking 5 textfields here for a value and "0" is treated as empty/no value. It doesn't matter if I leave this field empty or enter "0".

Unfortunately, I'm not too good with strings yet, just my guitar strings.

Thanks,

Gil

mordred
07-08-2002, 10:06 PM
That manual section explains it all:
http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

So if you have a string "0" and do an implicit cast to boolean (in the if statement), you've got bad luck. Try to use strlen() to determine if a string length is greater than 0 - which indicates that the user has set a value for this input field.

IKinsler
07-12-2002, 06:29 PM
Try this:

Do not create a variable for $test (as in, do not give it a value( and enter this:

if ($test == '0') {
echo "weird!";
}

Notice, it prints weird. 0 is given to any variable without a value. Thus, I guess it got confused! :D

Gil
07-12-2002, 08:07 PM
Hi,
You are right but the example below shows that "" is not always handled as 0 nor is zero handled as "". I can use this.


<?php

$test1 ="0";
if ($test1=="0"){
$test1=true;
}
if ($test1){
echo "<p>true";
}
else {
echo "<p>false";
}

$test2 ="";
if ($test2==""){
$test2=true;
}
if ($test2){
echo "<p>true";
}
else {
echo "<p>false";
}

$test3 ="";
if ($test3=="0"){
$test3=true;
}
if ($test3){
echo "<p>true";
}
else {
echo "<p>false";
}

?>

Bye bye

Gil

Flamerule
07-13-2002, 07:14 AM
What you can do to see if a variable contains a zero is use the isset(); function :


<?php

if (isset($b03) && isset($b04) && isset($b05) && isset($b07) && isset($b07) && isset($b09) && isset($b10))

{

?>

display this line

<?php
//endif
}?>


SHould work.....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum