check whether GET exists or not

Asked

Viewed 836 times

3

I created an array by calling names, within this array I have 3 names

$nomes = array ('carlos', 'maria', 'jose')

Later on in the code I’m trying to create an if where I check if the GET value exists inside this array, but I’m not getting it. I tried to do it only it didn’t work.

if($_GET['pg'] == $nomes){ echo "existe";}else{echo "nao existe";}

2 answers

6


You can use the in_array() PHP, which checks whether this string exists in a given array.

Can put in_array($_GET['pg'], $nomes) within the if() to check if there is an array member equal to the value of $_GET['pg'].

$nomes = array('carlos', 'maria', 'jose');
if(in_array($_GET['pg'], $nomes))  echo "existe";
else echo "nao existe";

4

Use the function in_array to make this comparison, the first argument is the value to be found and the second is in which variable the search should be made in the case $nomes.

$nomes = array ('carlos', 'maria', 'jose');

$_GET['pg'] = 'carlos';
if(in_array($_GET['pg'], $nomes)){
    echo 'nome '. $_GET['pg']  .' encontrado';
}

Browser other questions tagged

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