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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts

    problem with function

    i have written a function all over my site which is used to display an error.

    $input= "error msg";
    error($input);

    then that function will display the error msg and exit();. The problem is, i need to get a new variable into the function but i don't want to have to go through ever error(); function and change it to error($new, $error);

    So is there anyway else to get a variable(that has already been set mind you) into the function.

    The variable i'm trying to get in is the users ID, which is set at the top of the page.

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You can set a default value for the parameter.

    function bleh($arg = "something", $arg2)
    bleh("test");
    bleh("test1", "test2");

    Good luck;

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    There are two things you can do. You can use the func_num_args() function in your error function to determine how many arguments have been passed to the function, and then the func_get_args() function to retrieve those arguments. This frees you from requiring a specific number of arguments to be passed to your error function.

    The second thing you can do is assign a default value to your new second argument.
    PHP Code:
    function error($msg$flag NULL) {} 
    This technique makes the second argument optional, as it will default to NULL if it's missing.

    (EDIT) Brandoe85 beat me to it; but a note about his code snippet; You must default the second argument rather than the first, as the way he's written it will still require a second argument (and thus require you to change all of your code).

  • #4
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hmm well, i want to do function error($userid = $userid2, $input) {

    }

    but that gives me an error. Any other way?

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can't default to a variable. You can do global $userid2; but that's sloppy. (It will work for you though)

  • #6
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts
    $userid2 is already global. But it still won't work inside my function. I even echo'ed it outside the function to see if it contained a value and it did. But inside the function it always comes up as 0...

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Post your code. If it's global and has a different value then it's being overwritten.


  •  

    Posting Permissions

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