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 this way a function with 3 or more returns, with two users like this:
if($login->login($username, $password)) {
echo 'foi';
} else {
echo 'não foi';
}
I could tell?
EDITED
if (count($cookie) === 12) {
Session::set('ct0', $ct0[0]);
return true;
} elseif (count($cookie) === 13) {
return 'bloqued';
} else {
return false;
}
Let’s assume that the above example is a function made with curl
where if there is correct login and password returns me true
, if the account is blocked, returns me 'bloqued', if the entered data is wrong returns me false
;
Thus remaining:
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'
]
);
}
The variable
$condicao
exists ?– NoobSaibot
is just an example I tried to explain, I want to know how I use a function that returns more than three values. Understand?
– user93101
No. Does it make sense to have three different returns in the role, especially when dealing with different types? And what exactly do you want to check with the
if
?– Woss
There’s an error in the first line
return
, 'cause you’re using,
which has to be;
– NoobSaibot
@Wéllingthonm.Souza, yes it was typo I made the code right here, Anderson Carlos Woss, I will edit the topic.
– user93101
@Crazy Dev, you can see it works with more returns ideone.. It would be good to put more information about the code.
– NoobSaibot
I edited the question, @Wéllingthonm.Souza I tried with a similar example that you put in the comments and could not.
– user93101
$cookie
is a Array ?– NoobSaibot
Exact. Yes is an array, only $cookie[4] removed some strings.
– user93101