Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-08-2002, 04:30 PM   PM User | #1
Gil
New Coder

 
Join Date: Jul 2002
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Gil is an unknown quantity at this point
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
Gil is offline   Reply With Quote
Old 07-08-2002, 04:44 PM   PM User | #2
ObiwanJebroni
New Coder

 
Join Date: Jun 2002
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
ObiwanJebroni is an unknown quantity at this point
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.
__________________
-Obiwan Jabroni
May the Schwartz be With You
ObiwanJebroni is offline   Reply With Quote
Old 07-08-2002, 05:49 PM   PM User | #3
Gil
New Coder

 
Join Date: Jul 2002
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Gil is an unknown quantity at this point
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
Gil is offline   Reply With Quote
Old 07-08-2002, 10:06 PM   PM User | #4
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
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.
mordred is offline   Reply With Quote
Old 07-12-2002, 06:29 PM   PM User | #5
IKinsler
New Coder

 
Join Date: Jul 2002
Location: Regina, Saskatchewan, Canada
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
IKinsler is an unknown quantity at this point
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!
__________________
Jared Brandt
IKinsler
IKinsler is offline   Reply With Quote
Old 07-12-2002, 08:07 PM   PM User | #6
Gil
New Coder

 
Join Date: Jul 2002
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Gil is an unknown quantity at this point
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

Last edited by Gil; 07-12-2002 at 08:09 PM..
Gil is offline   Reply With Quote
Old 07-13-2002, 07:14 AM   PM User | #7
Flamerule
New Coder

 
Join Date: Jun 2002
Location: Paris, France
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Flamerule is an unknown quantity at this point
What you can do to see if a variable contains a zero is use the isset(); function :
PHP Code:
<?php 

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



?> 

display this line 

<?php 
//endif 
}?>
SHould work.....
__________________
I don't suffer from insanity, I enjoy every single minute of it!
Flamerule is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:25 PM.


Advertisement
Log in to turn off these ads.