Computer Forums

Search Tech-Forums.net

Member Login

Remember Me? Sign Up! | Forgot Password
 
 
Reply
Old 05-10-2006, 06:39 AM   #1 (permalink)
 
Newb Techie
Join Date: Apr 2006
Posts: 10
beosound
Default problem with PHP

I have a problem with following php-code:

Code:
<? if ($a == 1) 
	echo "a";
	if ($a == 0) {
	echo "";
} else {
	echo "b";
	}
	?>
When "$a = 0", it shows nothing.
When "$a = 1" , it shows "ab".
When "$a = 2" or above it shows "b".

What am I doing wrong, why does it show "ab", when "$a=1"

Hoping for soon answers

Regards
Beosound
beosound is offline   Reply With Quote
Old 05-10-2006, 07:38 AM   #2 (permalink)
office politics's Avatar
 
It's all just 1s and 0s
Join Date: Jan 2004
Location: in the lab
Posts: 4,181
office politics will become famous soon enough
Default

if a = 1 it displays a

then it evaluates the second if else

so since a is not 0, it echo's b

to prevent b from being displayed, put a else after echo a.

Code:
<? if ($a == 1) 
	echo "a";
          else {
	if ($a == 0) 
	      echo "";
                else 
    	     echo "b";
	}
	?>

office politics is offline   Reply With Quote
Old 05-10-2006, 07:51 AM   #3 (permalink)
 
Newb Techie
Join Date: Apr 2006
Posts: 10
beosound
Default

Quote:
Originally posted by csamuels
if a = 1 it displays a

then it evaluates the second if else

so since a is not 0, it echo's b

to prevent b from being displayed, put a else after echo a.

Code:
<? if ($a == 1) 
	echo "a";
          else {
	if ($a == 0) 
	      echo "";
                else 
    	     echo "b";
	}
	?>
Ah... Thanks :)
beosound is offline   Reply With Quote
 
Reply

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On