-1

I want my function to do the following:

if price is 0, return free message.

If price is positive, show the price.

If price value is unavailable - for example: if the database cell for the price is null, or have a value like unknown, then return unavailable message.

So I came with this code:

function get_rate($foo,$bar) {

  if ($bar== "something") {
    //$test= "testing";
  } elseif ($foo== 0) {
    $message = 'Free';
  }  elseif ($foo> 0) {
    $message = '$'.$foo;
  }  else {
    $message = 'Unavailable';
  }

  return $message;

}

HTML:

<?= get_rate( $price) ?>

But for the values:

$price="unknown"; or if $price is null, I'm still getting "Free" message.

rockyraw
  • 1,125
  • 2
  • 15
  • 36
  • Yu're mixing variables here. The input parameters are `$foo`and `$bar`, but you're checking `$bar` and `$a`. please fix your variables. – Alexander Jan 20 '16 at 15:26
  • already fixed, was typo, thanks. – rockyraw Jan 20 '16 at 15:27
  • 2
    @rockyraw you fixed nothing. Why do you even have 2 parameters ? – Daan Jan 20 '16 at 15:28
  • @Daan I replaced `$a` with `$foo`, my original code checks 2 parameters, for the sake of the question I removed the second one from question. – rockyraw Jan 20 '16 at 15:29
  • I tested two different $price values, see last line in question. – rockyraw Jan 20 '16 at 15:31
  • Loose comparison using `==`.... string `unknown` is cast to integer for the comparison, and equals `0` whih means `'unknown' == 0` is true.... this is pretty standard type juggling in PHP – Mark Baker Jan 20 '16 at 15:33
  • 1
    Possible duplicate of [Why does PHP consider 0 to be equal to a string?](http://stackoverflow.com/questions/6843030/why-does-php-consider-0-to-be-equal-to-a-string) – chris85 Jan 20 '16 at 15:57

1 Answers1

2

Now you have two solution for this:

Solution 1:

else if ($foo === 0) // use === for checking value and datatype

Solution 2:

else if ($foo == 0 && $foo != null) // adding != null

Value "" or null treated as 0, if you also check the data type using === this issue will resolve else use second solution.


After your comment, sharing a basic example you will get the idea:

$foo = 'bla';
var_dump($foo);

It will give you string(3) "bla"

devpro
  • 16,184
  • 3
  • 27
  • 38
  • alright, why does text like `$price="bla"` get treated as 0 too, though? – rockyraw Jan 20 '16 at 15:32
  • @rockyraw it feels like explaining too much but just look up == and ===. Having that said I think you know what to do... – Naruto Jan 20 '16 at 15:34
  • So no matter what I do, `$price="bla"`,`$price=="bla"`,`$price==="bla"`, all are treated as `0`? – rockyraw Jan 20 '16 at 15:38
  • If I use `($foo === 0)`, and `$price="0";` it will return `unavailable` instead of `free`. why? I noticed your #1 solution will work correct only with `=0` rather than `="0"` – rockyraw Jan 20 '16 at 15:43
  • @rockyraw: my friend, i suggest u check this manual link of PHP http://php.net/manual/en/types.comparisons.php (types comparisons) – devpro Jan 20 '16 at 15:49