check if GET value exists or not

3

I created an array calling names, inside that array I have 3 names

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

Later in the code I'm trying to create an if where it checks if the value of GET exists inside this array, but I'm not getting it. I tried to do that but it did not work.

if($_GET['pg'] == $nomes){ echo "existe";}else{echo "nao existe";}
    
asked by anonymous 02.08.2014 / 16:38

2 answers

6

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

You can put in_array($_GET['pg'], $nomes) within 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";
    
02.08.2014 / 16:42
4

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

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

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