sam
sam

Reputation: 10094

Compare variables (strings) and find larger number using if/else

Ive got two Variables :

$wh_odds_attrib
$lad_odds_attrib

if i preform var_dump on them i get

string(4) "1.36"
string(4) "2.00"

respectively.

I want to use these vairbales in an if statement, but if i do this what will it be evaluating, the value ie. 1.36 or the string(4) part ? (the part i need to evaluate is the 1.36)

the if statement im using is

  if ($wh_odds_attrib['oddsDecimal'] > $lad_odds_attrib['oddsDecimal']) {
      echo $wh_odds_attrib['oddsDecimal'];

  } else {
      echo $lad_odds_attrib['oddsDecimal'];
  }

Upvotes: 0

Views: 102

Answers (1)

ficuscr
ficuscr

Reputation: 7053

You can use type casting to cast the values to doubles if for some reason your comparison fails with strings.

if ((double) $wh_odds_attrib['oddsDecimal'] > (double) $lad_odds_attrib['oddsDecimal']) {
     echo $wh_odds_attrib['oddsDecimal'];
 } else {
     echo $lad_odds_attrib['oddsDecimal'];
 }

This is probably not needed though. Suggest reading: http://php.net/manual/en/language.types.type-juggling.php

Upvotes: 1

Related Questions