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

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";
        }
    }
}
    
asked by anonymous 01.06.2017 / 16:38

1 answer

1

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

To resolve this problem you can use the trim () function to remove this space when doing Comparation. Try changing this line:

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

by this:

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

Tip: Also use the strtolower () to convert the strings to lowercase before comparing. This will avoid problems when comparing "Active" with "active", for example. Here's how:

// remove espaços e converte $objeto->status para minúsculo antes de comparar
if ($objeto->$mail == false and "ativo" == strtolower(trim($objeto->status))) {
    
01.06.2017 / 17:24