More than one return on a method in PHP

0

For example I have the following function:

<?php

public function login($username, $password) {
  if($condicao === 1) {
     return true, 
  } elseif($condicao === 2) {
    return 'qualquer string';
  } else {
    return false;
  }
}

How do I use a function with 3 or more returns in this way, with two users like this:

if($login->login($username, $password)) {
  echo 'foi';
} else {
  echo 'não foi';
}

Did you understand?

EDITED

if (count($cookie) === 12) {
    Session::set('ct0', $ct0[0]);
    return true;
} elseif (count($cookie) === 13) {
    return 'bloqued';
} else {
    return false;
}

Let's suppose that the above example is a function made with curl where if there is a correct login and password it returns true , if the account is locked, it returns 'bloqued', if the inserted data is wrong it returns me false ;

Looking like this:

public function login($username, $password) {
    curl...

    if (count($cookie) === 12) {
        return true;
    } elseif (count($cookie) === 13) {
        return 'bloqued';
    } else {
        return false;
    }
}


// Modo de usar

if ($login->login($username, $password)) {
    echo json_encode([
            'error'     => false,
            'message'   => 'Logado com sucesso, aguarde...'
        ]
    );
} elseif($login->login($username, $password) !== true && $login->login($username, $password) !== false) {
    echo json_encode([
            'error' => true,
            'message' => 'Conta bloqueada',
        ]
    );
} else {
    echo json_encode([
            'error'     => true,
            'message'   => 'Usuário e/ou Senha incorretos'
        ]
    );
}
    
asked by anonymous 07.10.2017 / 02:45

2 answers

1

There are several ways, in fact what you want is not "More than one return", inclusive.

But, you could do:

const ERR_MENOR_QUE_UM = 0;
const ERR_MAIOR_QUE_VINTE = -1;
const ERR_DIVISIVEL_POR_DOIS = -2;

function verificarNumero($n)
{
    if($n < 0){
        return ERR_MENOR_QUE_UM;
    }

    if($n > 20){
        return ERR_MAIOR_QUE_VINTE;
    }

    if($n % 2 === 0){
        return ERR_DIVISIVEL_POR_DOIS;
    }

    return true;
}

This would be enough to do:

if(verificarNumero(2) === true){
    echo 'Nenhum erro ocorreu';
}

If not, as is the case, you would have three possible outcomes:

if(verificarNumero(2) === ERR_DIVISIVEL_POR_DOIS){
    echo 'Número inválido, divisível por dois';
}

You can also use switch for this purpose.

Now if you want to return more than one value you can actually use an array:

function fatorial($n)
{
    $f = array_product(range($n, 1));

    if ($f >= PHP_INT_MAX) {
        return [$f, true];
    }

    return [$f, false];
}

Now you actually return two values, this is not very common in PHP, although this may be extremely common in other languages. You could also do only return [array_product(range($n, 1)), $f >= PHP_INT_MAX]; , but I think this would be more confusing.

To use this you could do:

list($resultado, $erro) = fatorial(10);

if($erro === false){
  echo $resultado;
}else{
  echo 'Algo deu errado';
}
    
07.10.2017 / 10:39
1

Change the condition of:

// Modo de usar

if ($login->login($username, $password))

To:

// Modo de usar

if ($login->login($username, $password) === true)

See this article: When to use == or === in php? on the Blog of Alura.

    
07.10.2017 / 03:32