![]() |
Value=0 is not a value?
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 |
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.
|
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 |
That manual section explains it all:
http://www.php.net/manual/en/languag...oolean.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. |
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 |
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 |
What you can do to see if a variable contains a zero is use the isset(); function :
PHP Code:
|
| All times are GMT +1. The time now is 06:12 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.