Solved

Numeric if statment not generating desired results

Posted on 2010-08-25
13
306 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:dsg138
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33528152
for less than checking it will be one < and no <<

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_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.
   }
  }  
0
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33528156
you can check more about php operators at http://php.net/manual/en/language.operators.comparison.php
0
 

Author Comment

by:dsg138
ID: 33528192
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;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33528205
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.
   }
  }  
        

Open in new window

0
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33528208
also there is one extra } at the end of the code... not sure if that's for any prev if clause...
0
 

Author Comment

by:dsg138
ID: 33530412
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.
0
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33530481
try comparing numbers using this way...
http://www.phpbuilder.com/manual/function.gmp-cmp.php
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 100 total points
ID: 33530784
I followed the discussion and I tested the code posted by rdonline1 (ID:33528205). To test I've lttle changed, adding manually variables value and maybe I don't understood your explanation, dsg138, but it seems to work: my output is not 3 but -3. The code I've tested is posted in the snippet below: perhaps this test can help you to find the problem...
<?php
$teamname[0] = 'Virginia Tech';
$teamname[1] = 'Boise State';
$home_spread = 3;
$vis_spread = -3;
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.
   }
   
   echo "Favourite is $FAV<br>";
   echo "Underdog is $UDOG<br>";
   echo "Spread is $SPREAD<br>";
   echo "Option n. $Option";
?>

Open in new window

0
 

Author Comment

by:dsg138
ID: 33538180
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</spread_visiting>
<spread_adjust_visiting>100</spread_adjust_visiting>
<spread_home>-13.5</spread_home>
<spread_adjust_home>-110</spread_adjust_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?
0
 
LVL 11

Accepted Solution

by:
Rajesh Dalmia earned 400 total points
ID: 33538707
try as

$vis_spread = $spread->{"spread_visiting"} + 0;
$home_spread = $spread->{"spread_home"} + 0;

0 addition will make sure that the variables are in integer...
0
 

Author Closing Comment

by:dsg138
ID: 33538739
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


0
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33538742
thanks dan, gald to help...
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33539280
Thank you for points, dan. Have a nice day.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question