Verify that a given value exists within an array [duplicate]

0

Colleagues,

We have a menu with 11 items and each item will open a page containing their respective PDF files. To avoid creating 11 different pages, we created only one page called pdf.php and put it in the links of this menu as follows:

<a href='?pag=1'>Orçamentário</a>
<a href='?pag=2'>Dados</a>
<a href='?pag=3'>Contas</a>

Up to link number 11 and we are rescuing within the same page that contains the menu this way:

<?php if(isset($_REQUEST['pag'])){ include("pdf.php?pag=".$_REQUEST['pag']);} ?>

However this will generate a querystring in the browser. Ex .: www.site.com.br?pag=1. So to prevent the user from entering the numbering directly in the wrong browser. Eg: www.site.com.br?pag=12, we would like a message to appear for it Select a menu item .

To avoid giving an if () or switch () 11 times, we thought of creating an array with fixed values from 0 to 11 and if there were no such values, the message would appear.

How could we do this? We thought about using in_array, but we could not. See:

$array = array("0","1","2","3");

if(in_array(array(), $array)){
    echo "ok";
}else{
    echo "ops";
}
    
asked by anonymous 13.02.2017 / 14:14

1 answer

1

Although it is an issue duplicated by the requested content, the error is specific to how you are mounting your if , which should be as follows:

$array = array("0","1","2","3");

if(in_array($_GET['pag'], $array)){
    echo "ok";
}else{
    echo "ops";
}
    
13.02.2017 / 14:20