0
$modules                = array(
'home'                  => 'home',
'login'                 => 'login',
'forum'                 => 'forum',
'topic'                 => 'topic',
'post'                  => 'post',
'profile'               => 'profile',
'moderate'              => 'moderate',
'search'                => 'search',
'ucp'                   => 'usercp',
'ucc'                   => 'usercp',
'pm'                    => 'pm',
'members'               => 'members',
'boardrules'            => 'boardrules',
'groups'                => 'groupcp',
'help'                  => 'help',
'misc'                  => 'misc',
'tags'                  => 'tags',
'attach'                => 'attach'
);

if (in_array($modules, $_GET['module'])) {
include $_GET['module'].'.php';
}

gives:

Warning: in_array() [function.in-array]: Wrong datatype for second argument in d:\public_html\forte.php on line 24

What's wrong?

1
  • 2
    You've got the order of the function parameters swapped. Should be in_array ( $needle, $haystack )
    – jason
    Jul 17, 2009 at 15:04

3 Answers 3

10

You've got the arguments mixed up - see in_array():

if (in_array($_GET['module'], $modules)) {
    include $_GET['module'].'.php';
}
4
  • yet another case of people getting bitten by php being inconsistent with needle/haystack ordering. Jul 17, 2009 at 15:09
  • 4
    @kent: yet another case of people not using excellent php docs? Jul 17, 2009 at 15:12
  • 2
    @SilentGhost While I agree the PHP docs are phenomenal, consistent needle/haystack ordering would help those of us who have the functions committed to memory keep the arg order straight.
    – ceejayoz
    Jul 17, 2009 at 15:32
  • PHP can't retroactively fix the ordering.. so get used to it. Use an IDE that tells you what the param order is and you'll never run into this problem again.
    – Mike B
    Jul 17, 2009 at 20:22
1

wrong order of variable passed to in_array

 bool in_array  ( mixed $needle  , array $haystack  [, bool $strict  ] )
0

From PHP.NET:

bool in_array ( mixed $needle , array $haystack [, bool $strict ] )

Make sure you're needle is: $modules and where you're looking for it is: $_GET['module']. I feel like you mixed those two up. It really should be written like this:

in_array($_GET['module'], $modules);

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.