FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


if statement not working





welshsteve
Hi everyone.

Can somebody please tell me why the following code keeps returning the variable $theround as "Final" ?

Code:


if ($round == 1) {
$theround = "1st Round";
}
elseif ($round == 2) {
$theround = "2nd Round";
}
elseif ($round == 3) {
$theround = "3rd Round";
}
if ($round == 4) {
$theround = "Quarter Finals";
}
elseif ($round == 5) {
$theround = "Semi Finals";
}
else $theround = "Final";


If I echo the variable $round, it returns as 1, so $theround should return as "1st Round", but it returns as "Final".

I submitted the form with the value 1 selected, and I get the variable $round returning with a value 1, but the if statement is not setting the $theround variable to "1st Round".

Other values are fine. If I select 4, 5 or 6 as values, the correct value for $theround is returned. Only seems to be a problem when 1,2 or 3 are selected
Peterssidan
Code:
if ($round == 4) {
should be
Code:
elseif ($round == 4) {
welshsteve
GRRR Geez. Sorry man, can't believe I missed that Embarassed Embarassed Embarassed
jmraker
I've found that mistakes like that are easier to see with code indentation where the code in {} blocks are moved to the right with spaces or a tab. I like to use a tab.

Code:
elseif ($round == 3) {
$theround = "3rd Round";
}
if ($round == 4) {
$theround = "Quarter Finals";
}


becomes

Code:
elseif ($round == 3) {
   $theround = "3rd Round";
}
if ($round == 4) {
   $theround = "Quarter Finals";
}


Also that series of conditional statements could be replaced with a switch statement, as long as you remember to end each one with a break; or else php goes into the next case statement which is just as confusing if you don't notice.
Aredon
jmraker wrote:
Also that series of conditional statements could be replaced with a switch statement, as long as you remember to end each one with a break; or else php goes into the next case statement which is just as confusing if you don't notice.

I was going to recommend this! Very Happy A switch would be perfect for this application!
pollux1er
Aredon wrote:
jmraker wrote:
Also that series of conditional statements could be replaced with a switch statement, as long as you remember to end each one with a break; or else php goes into the next case statement which is just as confusing if you don't notice.

I was going to recommend this! Very Happy A switch would be perfect for this application!


Exact Aredon!

This is a good case for a switch statement!
welshsteve
Thanks guys, something like this?

Code:

<?php
switch ($round)
{
case 1:
 $theround = "1st Round";
  break;
case 2:
 $theround = "2nd Round";
  break;
case 3:
 $theround = "3rd Round";
  break;
case 4:
 $theround = "Quarter Finals";
  break;
case 5:
 $theround "Semi Finals";
  break;
default:
$theround ="Final";
}
?>
Aredon
Precisely! Cool
Fire Boar
Well, almost precisely - there's an obvious compile error in case 5, but other than that, yes.
welshsteve
Fire Boar wrote:
Well, almost precisely - there's an obvious compile error in case 5, but other than that, yes.


I spotted that myself Very Happy I left out the equals sign Wink
Aredon
To be honest the more I look at this problem the more I think you should be using an array instead of an if statement. (Sorry I didn't notice your error before)

Code:

$round_name = array(
1 => "1st Round",
2 => "2nd Round",
);
// $round_name[$round];


This would enable you to place that round name anywhere, even use it more than once. Without the need for if statements or switches. It would also save you several lines of code. Granted I may not be seeing the whole picture as to what you're doing, but if you're just matching numbers to names like that... an array would serve you nicely. (or even an sql database if you wanted to get all fancy) This could potentially enable you to carry that information via $_SESSION to other pages as well. Just a thought. Smile

Use the switch if you plan on making more than one thing happen in each case. Otherwise there's no need for conditionals if you're matching a number to a string. Rolling Eyes

Something I personally like to do is cram everything into a multi-dimensional array.

Code:

$round[1]['name'] = "1st Round";
$round[1]['players'][1] = "Aredon";
davidv
Aredon wrote:
To be honest the more I look at this problem the more I think you should be using an array instead of an if statement. (Sorry I didn't notice your error before)

Code:

$round_name = array(
1 => "1st Round",
2 => "2nd Round",
);
// $round_name[$round];


This would enable you to place that round name anywhere, even use it more than once. Without the need for if statements or switches. It would also save you several lines of code. Granted I may not be seeing the whole picture as to what you're doing, but if you're just matching numbers to names like that... an array would serve you nicely. (or even an sql database if you wanted to get all fancy) This could potentially enable you to carry that information via $_SESSION to other pages as well. Just a thought. Smile

Use the switch if you plan on making more than one thing happen in each case. Otherwise there's no need for conditionals if you're matching a number to a string. Rolling Eyes

Something I personally like to do is cram everything into a multi-dimensional array.

Code:

$round[1]['name'] = "1st Round";
$round[1]['players'][1] = "Aredon";


I agree with Aredon. A hash table should definitely be used to minimize the total number of lines, if all you're doing is matching but also it makes for a more efficient snippet in the long run.

I'm not a PHP programmer but I will show you how it would work in Python.

Code:
>>> rounds = {1: '1st Round',
...             2: '2nd Round',
...             3: '3rd Round',
...             4: '4th Round',
...             5: '5th Round',
...             6: '6th Round'}
>>> rounds
{1: '1st Round', 2: '2nd Round', 3: '3rd Round', 4: '4th Round', 5: '5th Round', 6: '6th Round'}
>>> round = 1 # some integer between 1 - len(rounds)
>>> round
1
>>> rounds[round]
'1st Round'
>>>


The key-value pair paradigm avoids the need to have if statements.
Fire Boar
That would be fine, assuming the "default" value is deterministic. Otherwise, you need some kind of edge case.
Aredon
Fire Boar wrote:
That would be fine, assuming the "default" value is deterministic. Otherwise, you need some kind of edge case.
Could always make the zero entry in the array your default value.
Related topics
Yahoo takes on iTunes with new music service
FCC requires VoIP to clean up its 911 act
There's One Born Every Minute
My Subdomain still not working :(
SQL Basics
microsoft
Tutorial: Image Rollovers w/ Javascript
site not working
My subdmain seems to be not working.
phpBB Not Working
[RESOLVED] Cpanel not working?
please provide feedback for http://sanghvitrades.frihost.net
Error trying to display SQL statement in html table
Array Update SQL script not working
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.