Compare "Active" == $object->status in PHP

Asked

Viewed 77 times

1

$matricula=$_POST['matricula'];

foreach ($aluno as $objeto){

    if ($objeto->matricula==$matricula ) {

        echo $objeto->status;

        if ($objeto->$mail == false and "Ativo" == $objeto->status ) {
            echo "Sua conta foi criado com sucesso";
        } elseif ($objeto->$mail == true) {
            echo "VocÊ já possui email";
        }else{
            echo "Voce está inativo";
        }
    }
}
  • This $object->$mail really is a Boolean value?

  • 1

    What error are you getting? Give more details about your problem so we can help you more accurately and easily.

  • Yes, the comparison that doesn’t work is this "Active" == $object->status

  • If possible place the return of the $object variable

  • Comparison "Active" == $object->status always from false, even if equal.

  • So echo '<pre>'; var_dump($object); die; and put here the return.

  • Object(Students)#7 (6) { ["name"]=> string(23) "Gabriela Santos Ribeiro" ["matricula"]=> string(6) "109647" ["telephone"]=> string(10) "99999-9955" ["mail"]=> string(15) "[email protected]" ["status"]=> string(7) "Active " Email n is Boolean.

  • The status value is set to 7 characters, while active only has 5, so there are unwanted characters there.

Show 3 more comments

1 answer

1


For his comment on his question, the value $objeto->status has a space at the end ("Active"). So, you are comparing the string "Active" with the string "Active " and so it has been receiving false as a result of the expression.

To solve this problem you can use the function Trim() to remove this space when making the comparison. Try changing this line:

if ($objeto->$mail == false and "Ativo" == $objeto->status ) {

for this:

if ($objeto->$mail == false and "Ativo" == trim($objeto->status)) {

Tip: also use the function strtolower() to convert the strings to lower case before comparing. This avoids problems when comparing "Active" to "active", for example. Do so:

// remove espaços e converte $objeto->status para minúsculo antes de comparar
if ($objeto->$mail == false and "ativo" == strtolower(trim($objeto->status))) {
  • Thank you very much , !!!

  • @You’re welcome! I’m glad it worked. Please, if this answer has solved your problem remember accept it to inform other users that your issue has been resolved and help future viewers of your issue. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.