Page 1 of 1

Which of the two is better? (page validation)

Posted: Fri Apr 04, 2008 9:30 pm
by Sequalit
What is better to use, the isset function or the in_array function? And why?

isset

Code: Select all

 
$myModule;
private $moduleArray=array(                 
        'home'          => 'news.php',              
        'news'          => 'news.php',              
        'transactions'  => 'transactions.php',
        'login'         => 'login.php',
        );
 
if(!isset($moduleArray[$_POST['module']]))
               $myModule = $moduleArray['home'];
else
               $myModule = $moduleArray[$_POST['module']];
 
echo $myModule;
 
 
in_array

Code: Select all

 
$myModule;
private $acceptableParams=array(                    
        'home' ,            
        'news'  ,       
        'transactions'  ,
        'login'         
        );
 
if(!in_array($_POST['module'], $acceptableParams))
                $myModule = "home". ".php";
else
                $myModule = $_POST['module'].".php";
 
echo $myModule;
 

Re: Which of the two is better? (page validation)

Posted: Fri Apr 04, 2008 9:36 pm
by John Cartwright
The perfomance difference in your case is negligable. So you answer your question either is fine.

However, you still need to check that $_POST['module'] exists before using it.. personally I'd do

Code: Select all

 
if (isset($_POST['module']) && in_array($_POST['module'], $moduleArray)) {
 
}