dsg138
asked on
Numeric if statment not generating desired results
Experts,
When given 2 numbers, I'd like to determine the "favorite" (for betting). The Favorite is the NEGATIVE number. So the team with a spread of "-7" is the favorite and the underdog's spread is "7".
When I output the home and visiting spreads:
echo $vis_spread . "<BR>";
echo $home_spread;
I get:
13.5
-13.5
So clearly, one is negative and one is positive, and therefore less than the other.
When I run the following if statement:
if ($home_spread<<$vis_spread ){
// Option 1
$FAV = $teamname[1];
$UDOG = $teamname[0];
$SPREAD = $home_spread;
}
elseif($vis_spread<<$home_ spread){
// Option 2
$FAV = $teamname[0];
$UDOG = $teamname[1];
$SPREAD = $vis_spread;
}
else{
// Option 3
$FAV = $teamname[1];
$UDOG = $teamname[0];
$SPREAD = '0';
// Spread of 0 means teams are EVEN.
}
}
The above statement Outputs Option 1 EVERY TIME.
In cases when Option 2 should be true, it still results in #1.
I know this is incorrect, because the $SPREAD appears as a positive number
This shouldn't be possible because the negative number should ALWAYS be less than the positive, becoming the $SPREAD.
example of incorrect result:
-3
3
Favorite: Virginia Tech
Underdog: Boise State
Spread: 3
Visiting: Boise State -3
Home: Virginia Tech 3
The Favorite should be Boise State since it's spread is -3.
It's outputting 3 as the spread, telling me it believes option 1 in the if statement is true.
Any ideas?
When given 2 numbers, I'd like to determine the "favorite" (for betting). The Favorite is the NEGATIVE number. So the team with a spread of "-7" is the favorite and the underdog's spread is "7".
When I output the home and visiting spreads:
echo $vis_spread . "<BR>";
echo $home_spread;
I get:
13.5
-13.5
So clearly, one is negative and one is positive, and therefore less than the other.
When I run the following if statement:
if ($home_spread<<$vis_spread
// Option 1
$FAV = $teamname[1];
$UDOG = $teamname[0];
$SPREAD = $home_spread;
}
elseif($vis_spread<<$home_
// Option 2
$FAV = $teamname[0];
$UDOG = $teamname[1];
$SPREAD = $vis_spread;
}
else{
// Option 3
$FAV = $teamname[1];
$UDOG = $teamname[0];
$SPREAD = '0';
// Spread of 0 means teams are EVEN.
}
}
The above statement Outputs Option 1 EVERY TIME.
In cases when Option 2 should be true, it still results in #1.
I know this is incorrect, because the $SPREAD appears as a positive number
This shouldn't be possible because the negative number should ALWAYS be less than the positive, becoming the $SPREAD.
example of incorrect result:
-3
3
Favorite: Virginia Tech
Underdog: Boise State
Spread: 3
Visiting: Boise State -3
Home: Virginia Tech 3
The Favorite should be Boise State since it's spread is -3.
It's outputting 3 as the spread, telling me it believes option 1 in the if statement is true.
Any ideas?
you can check more about php operators at http://php.net/manual/en/language.operators.comparison.php
ASKER
Thanks.
I initially tried that and it didn't work either.
When I use just one < or > like in your example, it results in Option 3 every time which is the "else" clause.
Not sure what it could be...
My other thought was perhaps the problem was with the numbers and not the syntax.
Maybe it didn't think those values were numeric?
So I added this before the loop, but still results in Option 3 for everything.
$vis_spread = 0;
$home_spread = 0;
I initially tried that and it didn't work either.
When I use just one < or > like in your example, it results in Option 3 every time which is the "else" clause.
Not sure what it could be...
My other thought was perhaps the problem was with the numbers and not the syntax.
Maybe it didn't think those values were numeric?
So I added this before the loop, but still results in Option 3 for everything.
$vis_spread = 0;
$home_spread = 0;
try this
if ($home_spread<$vis_spread){
// Option 1
$FAV = $teamname[1];
$UDOG = $teamname[0];
$SPREAD = $home_spread;
}
elseif ($home_spread>$vis_spread){
// Option 2
$FAV = $teamname[0];
$UDOG = $teamname[1];
$SPREAD = $vis_spread;
}
else{
// Option 3
$FAV = $teamname[1];
$UDOG = $teamname[0];
$SPREAD = '0';
// Spread of 0 means teams are EVEN.
}
}
also there is one extra } at the end of the code... not sure if that's for any prev if clause...
ASKER
Thanks. The end } is from a higher loop and should be correct.
I tried your code and it results in Option 3 (the else clause) for EVERY instance.
That's the part that has me confused.
It seems unable to compare the 2 numbers, even though it has no problems outputting them.
I tried your code and it results in Option 3 (the else clause) for EVERY instance.
That's the part that has me confused.
It seems unable to compare the 2 numbers, even though it has no problems outputting them.
try comparing numbers using this way...
http://www.phpbuilder.com/manual/function.gmp-cmp.php
http://www.phpbuilder.com/manual/function.gmp-cmp.php
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks all for the feedback...
I think the issue is with my numbers:
When I hard code the numbers below like in the code above, everything works correctly:
$home_spread = 3;
$vis_spread = -3;
My numbers are coming from here through XML parsing:
$vis_spread = $spread->{"spread_visiting "};
$home_spread = $spread->{"spread_home"};
Here is the XML file:
http://xml.pinnaclesports.com/pinnacleFeed.aspx?sportType=Football&sportsubtype=NCAA
And here's where the "-13" and "13" come from...
<spread>
<spread_visiting>13.5</spr ead_visiti ng>
<spread_adjust_visiting>10 0</spread_ adjust_vis iting>
<spread_home>-13.5</spread _home>
<spread_adjust_home>-110</ spread_adj ust_home>
</spread>
When I echo $home_spread and $vis_spread, I get the following:
13.5
-13.5
So when I hardcode the numbers in they work, but pulling them from the XML doesn't work...
Any other ideas?
I think the issue is with my numbers:
When I hard code the numbers below like in the code above, everything works correctly:
$home_spread = 3;
$vis_spread = -3;
My numbers are coming from here through XML parsing:
$vis_spread = $spread->{"spread_visiting
$home_spread = $spread->{"spread_home"};
Here is the XML file:
http://xml.pinnaclesports.com/pinnacleFeed.aspx?sportType=Football&sportsubtype=NCAA
And here's where the "-13" and "13" come from...
<spread>
<spread_visiting>13.5</spr
<spread_adjust_visiting>10
<spread_home>-13.5</spread
<spread_adjust_home>-110</
</spread>
When I echo $home_spread and $vis_spread, I get the following:
13.5
-13.5
So when I hardcode the numbers in they work, but pulling them from the XML doesn't work...
Any other ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did it! Perfect!
rdonline1, thanks for the code that fixed the issue.
marqusG, thanks for the testing code that led me to realize that it wasn't considering those fields as integers.
Thanks,
-Dan
rdonline1, thanks for the code that fixed the issue.
marqusG, thanks for the testing code that led me to realize that it wasn't considering those fields as integers.
Thanks,
-Dan
thanks dan, gald to help...
Thank you for points, dan. Have a nice day.
so it will be like
if ($home_spread<$vis_spread)
// Option 1
$FAV = $teamname[1];
$UDOG = $teamname[0];
$SPREAD = $home_spread;
}
elseif($vis_spread<$home_s
// Option 2
$FAV = $teamname[0];
$UDOG = $teamname[1];
$SPREAD = $vis_spread;
}
else{
// Option 3
$FAV = $teamname[1];
$UDOG = $teamname[0];
$SPREAD = '0';
// Spread of 0 means teams are EVEN.
}
}