...

View Full Version : What Happens if I Pass a non-existed $_GET variable into a function as parameter?



dealmaker
01-05-2006, 11:54 PM
Hi,
What Happens if I Pass a non-existed $_GET variable into a function as parameter like this?

myfunc($_GET['mydata']); // but there is no such $_GET variable

function myfunc($para) {

}

Is $para going to be null or unset when I look at it inside myfunc()? The question is what if I pass an unset variable as parameter into a function?

many thanks.

Kid Charming
01-06-2006, 12:27 AM
Unless you define your function with a default value for you parameter, you should get an error.

GJay
01-06-2006, 12:39 AM
<?php

function test($arg) {
if(!isset($arg))
echo "Not set";
if($arg==null)
echo "is null";
}
test($_GET['test']);
?>

produces:
/web$ php5 test.php
Not set
is null


if that's of any help...

missing-score
01-06-2006, 03:29 AM
Unless you define your function with a default value for you parameter, you should get an error.
It should produce an error anyway...

It depends on your error_reporting setting, if you are at E_ALL reporting it will cause an error if it is not set, lower error reporting levels may cause the error not to be shown but it will still be generated. You should never try to use a variable that doesn't exist or that you are not sure about, its bad coding practise. Use isset() to check for the variable, eg:



if( isset( $_GET['var'] ) ){
myfunc($_GET['var']);
} else {
myfunc( "default" );
}


Or in one line:



myfunc( ( (isset($_GET['var']) ? $_GET['var'] : "default" ) );

Element
01-06-2006, 03:50 AM
It should produce an error anyway...

It depends on your error_reporting setting, if you are at E_ALL reporting it will cause an error if it is not set, lower error reporting levels may cause the error not to be shown but it will still be generated. You should never try to use a variable that doesn't exist or that you are not sure about, its bad coding practise. Use isset() to check for the variable, eg:

Or in one line:

I use something like:






function myFunction ($val = false) {
if($val) {
return false;
} else {
return "Check out this: ".$var;
}
}

if($var = myFunction($_GET['foo'])) {
echo $var;
} else {
die('<b>Fatal : $_GET[\'foo\']</b> : Was not set');
}



I would however have the funcntion maybe return a error, or have the function die().

missing-score
01-06-2006, 03:55 AM
function myFunction ($val = false) {
if($val) {
return false;
} else {
return "Check out this: ".$val;
}
}

if($var = myFunction($_GET['foo'])) {
echo $var;
} else {
die('<b>Fatal: $_GET[\'foo\']</b> : Was not set');
}



I would however have the funcntion maybe return a error, or have the function die().

After fixing your nested single quote (on the die() line), and the unset variable in the else{} section of the first function, Your code produces the following:



Notice: Undefined index: foo in C:\server\htdocs\icdsite\dev\noway.php on line 10
Check out this:


As I stated, you should never use unset variables before checking their existance, and you should always test your scripts in a high reporting state, you can put:


error_reporting(E_ALL);

at the top of your page to make sure.

Element
01-06-2006, 03:57 AM
Thanks, glad I caught this, I was just about to go to cpanel and test the E_ALL error reporting.

dealmaker
01-06-2006, 09:48 PM
a similar question:

if I have do this: isset($object1->object2->object3->data);

But object2 and object3 and data are not allocated yet, so only $object1 exists. Does it produce fatal error or does it just return false?

many thanks.

missing-score
01-06-2006, 09:59 PM
Why dont you try ;)

It should produce an error, I dont know about a fatal one though... Alot of "variable unset errors" are notices. To be honest, its just best to make sure a variable exists rather than trying to trick the parser.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum