I have the following string:
$cadena = "lucia,carolina,roberto,carlos,carolinaramos";
$datos = explode(",", $cadena);
And I use this method to condition:
$idUsuario = "carolina";
foreach ($cadena as $nombre_campo) {
if ($nombre_campo != $idUsuario) {
echo "No estas en el grupo";
}
else{ echo "Ya estás en este grupo"; }
}
The problem is that if there are 5 words in the string, 5 times it will display the messages .
What I need is that if your username is not in that chain, it shows only once " You are not in the group " and if not " You are already in this group " I appreciate your answers, if there is a way to make this process more effective let me know, Thank you very much in advance.
With a variable initialized
false
and inside the loop set it equal totrue
in case of matching,$idUsuario
you could then outside the loop print one sentence or another depending on what that boolean variable contains.Or you could use
in_array()
a specific PHP function that by passing it a value to look for and aarray
where to do it, returnstrue
orfalse
depending on whether it finds said value or not: