5 Replies - 984 Views - Last Post: 18 June 2011 - 05:14 PM Rate Topic: -----

#1 dylan.mann  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 26
  • Joined: 03-December 10

If() works, If() Else doesn't.

Posted 17 June 2011 - 11:31 PM

Anytime I use an else in an if statement my page won't load.

$x = 10;
if($x < 100) {
   echo 'True.';
}


Works fine but if I try:
$x = 10;
if($x < 100) {
   echo 'True.';
   else
      echo 'False.';
   }
}


My page won't load (HTTP 500 Error) or it will download the php file instead. Any ideas? Thanks in advance.

This post has been edited by dylan.mann: 17 June 2011 - 11:32 PM

Is This A Good Question/Topic? 0
  • +

Replies To: If() works, If() Else doesn't.

#2 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: If() works, If() Else doesn't.

Posted 18 June 2011 - 12:31 AM

the else block you have used have no corresponding if code block, thats why generating error.

$x = 10;
if($x < 100) {
   echo 'True.';
}
else {
   echo 'False.';
}



You can use nested if-else, but every time an else block must be followed by a corresponding if code block.
Was This Post Helpful? 2
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2998
  • View blog
  • Posts: 10,380
  • Joined: 08-August 08

Re: If() works, If() Else doesn't.

Posted 18 June 2011 - 03:56 AM

You must close braces. You can do as aitk97 has shown or because you've only got one line inside the if/else you can do this:

$x = 10;
if($x < 100) 
   echo 'True.';
else 
   echo 'False.';


or this
$x = 10;
if($x < 100) {
   echo 'True.';
}
else 
   echo 'False.';


or even this
$x = 10;
if($x < 100) 
   echo 'True.';
else {
   echo 'False.';
}


But you can't break up a pair of braces.

This post has been edited by CTphpnwb: 18 June 2011 - 03:57 AM

Was This Post Helpful? 1
  • +
  • -

#4 dylan.mann  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 26
  • Joined: 03-December 10

Re: If() works, If() Else doesn't.

Posted 18 June 2011 - 03:21 PM

Thanks all. Did notice that I had a typo in my original post. Meant for the second (non-working) example to be:

$x = 10;
if($x < 100) {
   echo 'True.';
   else {
      echo 'False.';
   }
}


Was This Post Helpful? 0
  • +
  • -

#5 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: If() works, If() Else doesn't.

Posted 18 June 2011 - 03:26 PM

As noted previously, your code is wrong.

You have an external block that says
if($x < 100){
  [some code]
}



Then you have an internal block that says
echo 'True.';
else{
     echo 'False.';
}


This is invalid, since you can't have an else without a corresponding if first. The problem is your use of braces. The opening brace after the if is supposed to surround only the if statement... the else statement is separate. so, as noted previously:

$x = 10;
if($x < 100) {
   echo 'True.';
}
else {
   echo 'False.';
}


Was This Post Helpful? 0
  • +
  • -

#6 dylan.mann  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 26
  • Joined: 03-December 10

Re: If() works, If() Else doesn't.

Posted 18 June 2011 - 05:14 PM

Thanks. Got that, I just wanted to clarify that I had messed up what I was trying to say originally.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1