Friday 15 March 2013

php - array_key_exists always returning false -



php - array_key_exists always returning false -

i have code, written in php homecoming foo if bar given , bar if foo given. have attempted, , seemingly failed add together grab if other foo or bar input "unknown output".

<?php echo("<html><body bgcolor='#ffffff'><h1>welcome</h1>"); $input = $_get["foobar"]; $array = array( "foo" => "bar", "bar" => "foo", ); function getvalue($value) { if(array_key_exists($value, $array)) { homecoming $array[$value]; } else { homecoming "unknown"; } } echo ("input: ". $input .", output: ". getvalue($input)); echo("<br><br>"); print_r($array); echo("</body></html>"); ?>

however, appears array_key_exists returning false when go page either page.php?foobar=foo or page.php?foobar=bar this:

welcome input: bar, output: unknown array ( [foo] => bar [bar] => foo )

or opposite input switched foo output remains "unknown".

basic php: variables defined in "parent" scope not visible in "child" scopes:

$array = array(...); // global scope, top-level of script function getvalue($value) { if(array_key_exists($value, $array)) { ^^^^^^^---undefined local variable, function scope

you should have @ least

global $array;

at start of getvalue function.

php

No comments:

Post a Comment