0

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'];
  }

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

that works, cheers. Im not sure what the issue was exactly, would my previous statement if statement be able to evaluate string numbers ? Or would they need to be made into floats before hand ?
Read the bit on String Conversion to Numbers should remove any doubt about how it works. Ideally when you assign those you would assign them as numeric values, not strings. If not an option then it will just depend what you need to do with them as to rather you cast or not. Loosely typed language...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.