It might be worth checking out variable scoping in PHP to get a handle on why this error is coming up. There are three variables operating in the piece of code above.
In the first line, you set $test
to be the returned value of the isValid()
function, to which you provide the value of $_GET["id"]
as an argument. When there is no $_GET[]
variable provided with the name id
, however, PHP throws a Notice
saying exactly that - that the index is
is undefined - but since this is just a notice, and your error reporting is set to tolerate notices, the script just carrys on, and assumes the value of $_GET["id"]
to be empty (i.e., ""
== false
== 0
== null
etc.).
In the isValid()
function, you have a new variable defined ($var
) which is local in its scope, existing only inside the function. Thus, $var
will always be defined inside this function, and thus isset($var)
, inside the function, will always be true
. Even when $_GET["id"]
is empty, $var
will still be defined with the empty value PHP automatically gave to it.
If you wanted to determine whether $_GET["id"]
exists, you would have to use the isset()
function on this variable, not a locally scoped variable which has been assigned with its value (or lack thereof). Thus, you could get rid of the notice, and have the same effect as before, by using this line of code:
$test = (isset($_GET["id"]) && !empty($_GET["id"]));
Hope this helps, I think the link below might help you out!
http://php.net/manual/en/language.variables.scope.php