Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Unless you define your function with a default value for you parameter, you should get an error.

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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...

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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" ) ); 

  • #5
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts

    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.

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •