Jade Elizabeth Jade Elizabeth - 2 years ago 95
PHP Question

Syntax error: !in_array not working

My code is telling me the ! is a syntax error and I am having a hard time with it....am I doing this right or horribly wrong? It also doesn't work if there's no space after the !. I just want to know if the current board is NOT in the array so I can display something.

if ($context['current_board'] ! in_array('3', '22', '34', '35'))

For those wanting to close this I ask you not to: This question has been asked around the net and the answer was to put a space after the !, even on this site. For that reason I ask a real answer and explanation be allowed to stand and help struggling newbies like myself.

Answer Source

in_array is a function. It takes two arguments (well, and an optional third, but that's besides the point here) - a needle to look for and a haystack (array) to search in. The boolean negation operator (!), if you want to use it, should come before the function call, and would apply on its result. In your case, you'll have something like this:

if (!in_array($context['current_board'], array('3', '22', '34', '35'))) {
    # do something...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download