0

I am using a class from somebody else and are getting an unexpected result. I already have an idea how to work around it, but I would like to understand why this happens.

class Pay {
  public function checkStatus {
    $check[0] = "000000 OK"
    return $check[0]
  }
}

$status = $cart->checkStatus ();
$payed = ( $status == "000000 OK" ? true : false);

The problem is that $status is somehow 1 when printing (could be also 'true' (have to check later at home)). Also payed is set to 'true' while I expect 'false' because of the wrong value of $status. Hope somebody can explain to me what is happening.

0

2 Answers 2

4

Try this:

$payed = ( $status === "000000 OK" ? true : false);

=== operator checks if $status and your string are equal and from the same type (string). More information you can find here: http://php.net/manual/en/language.operators.comparison.php

I tested it:

class Pay {
  public function checkStatus() {
    $check[0] = "000000 OK";
    return $check[0];
  }
}

$cart = new Pay();
$status = $cart->checkStatus();
echo $status; // returns "000000 OK"
$payed = ( $status == "000000 OK" ? true : false);
echo $payed; // returns 1
$payed = ( $status === "000000 OK" ? true : false);
echo $payed; // returns 1

If i echo $status it returns 000000 OK as string. I don't know whats your problem.

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

2 Comments

That would then probably solve that $payed is not what I expect, but I still don't understand why $check[0] gets changed from a string to an int or boolean when it gets saved into $status
updated my answer.. i dont know whats your problem. $status variable returns the string 000000 OK as expected.
3

This happens because of your numeric or boolean value in $status. This causes your bit $status == "000000 OK" to evaluate your string to a numeric value (which is 1), thus resulting in true.

Please see this questions accepted answer for further explaination:

Comparing String to Integer gives strange results

1 Comment

I still don't understand why $check[0] gets changed from a string to an int or boolean when it gets saved into $status

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.