I am playing around with this:
$sort = array('t1','t2');
function test($e){
echo array_search($e,$sort);
}
test('t1');
and get this error:
Warning: array_search(): Wrong datatype for second argument on line 4
if I call it without function like this, I got the result 0;
echo array_search('t1',$sort);
What goes wrong here?? thanks for help.
Variables in PHP have function scope. The variable
$sortis not available in your functiontest, because you have not passed it in. You’ll have to pass it into the function as a parameter as well, or define it inside the function.You can also use the
globalkeyword, but it is really not recommended. Pass data explictly.