Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-05-2006, 10:54 PM   PM User | #1
dealmaker
Regular Coder

 
Join Date: Jul 2005
Posts: 342
Thanks: 0
Thanked 0 Times in 0 Posts
dealmaker has a little shameless behaviour in the past
Question What Happens if I Pass a non-existed $_GET variable into a function as parameter?

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.
dealmaker is offline   Reply With Quote
Old 01-05-2006, 11:27 PM   PM User | #2
Kid Charming
Regular Coder

 
Join Date: Jun 2005
Posts: 804
Thanks: 0
Thanked 0 Times in 0 Posts
Kid Charming is an unknown quantity at this point
Unless you define your function with a default value for you parameter, you should get an error.
Kid Charming is offline   Reply With Quote
Old 01-05-2006, 11:39 PM   PM User | #3
GJay
Senior Coder

 
Join Date: Sep 2005
Posts: 1,791
Thanks: 5
Thanked 36 Times in 35 Posts
GJay is on a distinguished road
PHP Code:
<?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...
GJay is offline   Reply With Quote
Old 01-06-2006, 02:29 AM   PM User | #4
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
Quote:
Originally Posted by Kid Charming
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:

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

Or in one line:

PHP Code:
myfunc( ( (isset($_GET['var']) ? $_GET['var'] : "default" ) ); 
missing-score is offline   Reply With Quote
Old 01-06-2006, 02:50 AM   PM User | #5
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Exclamation

Quote:
Originally Posted by missing-score
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:
PHP Code:

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().

Last edited by Element; 01-06-2006 at 02:54 AM..
Element is offline   Reply With Quote
Old 01-06-2006, 02:55 AM   PM User | #6
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
Quote:
Originally Posted by Element
PHP Code:

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:

Code:
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:

PHP Code:
error_reporting(E_ALL); 
at the top of your page to make sure.
missing-score is offline   Reply With Quote
Old 01-06-2006, 02:57 AM   PM User | #7
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Thanks, glad I caught this, I was just about to go to cpanel and test the E_ALL error reporting.

Last edited by Element; 01-06-2006 at 03:01 AM..
Element is offline   Reply With Quote
Old 01-06-2006, 08:48 PM   PM User | #8
dealmaker
Regular Coder

 
Join Date: Jul 2005
Posts: 342
Thanks: 0
Thanked 0 Times in 0 Posts
dealmaker has a little shameless behaviour in the past
Question

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.
dealmaker is offline   Reply With Quote
Old 01-06-2006, 08:59 PM   PM User | #9
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
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.
missing-score is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:07 AM.


Advertisement
Log in to turn off these ads.