SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating functions

    I have two questions about functions the first.

    1 - In javascript there is a way to put in any number of variables within the function so

    function (var1,var2,var3)

    and then next time have

    function (var1, var2, var3, var4)

    Is there a way to not define the variables in the function definition and only 'catch' the variables as they are defined. Or in PHP do you'HAVE' to define all variables.

    This allows many different possibilities. But every time I have more or less variables within PHP I get an wrong number of variations or something like that. Which leads to my second question

    2 - Can you create a 'switch' variable like most native object have. Basically if it is defined it will use it. If left undefined no error occurs. I want to use it for a boolean switch within a function and sometimes need it and sometimes I don't.

    Thanx guys.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  2. #2
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for using multiples of variables in functions i would suggest passing an array to it
    PHP Code:
    function parse_this($array){
      foreach(
    $array as $key => $value){
        
    //do something
      
    }

    And as for using or not using passed or unpassed variables, just test it inside the function. ie:
    PHP Code:
    function boolean_thing($setme){
      if(!
    $setme) {
        
    //no variable, just leave this blank or return an error...whatever
      
    } else {
        
    //you have the variable set, do something with it.
      
    }


    Hope that helps.
    -Jeff Minard | jrm.cc - Battlefield 2 Stats

  3. #3
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Milamber
    for using multiples of variables in functions i would suggest passing an array to it
    PHP Code:
    function parse_this($array){
      foreach(
    $array as $key => $value){
        
    //do something
      
    }

    duh...I should kick myself...yeah I could do that...Javascript just does it for you...thanx.


    And as for using or not using passed or unpassed variables, just test it inside the function. ie:
    PHP Code:
    function boolean_thing($setme){
      if(!
    $setme) {
        
    //no variable, just leave this blank or return an error...whatever
      
    } else {
        
    //you have the variable set, do something with it.
      
    }


    Hope that helps.
    Here is an example...

    function boolean_thing($name,$setme){
    if(!$setme) {
    //no variable, just leave this blank or return an error...whatever
    } else {
    //you have the variable set, do something with it.
    }
    }

    I want to be able to call it like this
    boolean_thing("mark",true);

    or

    boolean_thing("mark");

    What is happening is I get an error of wrong number of variables. Or undefined variables.

    Could be me though...I do appreciate the help

    Could you tell me what passing a variable like this does.

    boolean_thing(&$variable);

    thanx again...
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  4. #4
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a cheap workaround but you could set a default for the parameters or switch the parameter order... like how we used to learn in programming class

    PHP Code:
    function boolean_thing($name,$setme=0){
      if(!
    $setme) {
        
    //no variable, just leave this blank or return an error...whatever
      
    } else {
        
    //you have the variable set, do something with it.
      
    }


  5. #5
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by redemption
    just a cheap workaround but you could set a default for the parameters or switch the parameter order... like how we used to learn in programming class

    PHP Code:
    function boolean_thing($name,$setme=0){
      if(!
    $setme) {
        
    //no variable, just leave this blank or return an error...whatever
      
    } else {
        
    //you have the variable set, do something with it.
      
    }

    Then when you pass a variable that changes it...thanx. Its kind of cheating but that should do what I want.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    lalal
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by tress
    have a look at http://www.php.net/manual/en/function.func-get-args.php
    Thanx...that wa exactly what I was looking for
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.


Bookmarks

Posting Permissions

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