Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Pass array into function

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #31796
    jacorre
    Participant

    Playing around with a plugin for WordPress. I’m trying to figure out how to pass multiple values into a function and then use those values within the function.

    For example:

    So if someone uses that template tag and passes in the values of 1, 2, and 3, I want to grab all those values they pass in and do something with them in the function I’m writing.

    How would I go about doing that? I believe I need a loop to to store each value into an array right?

    #57919
    Bob
    Member

    Let’s say you want to add up three numbers, which are the variables $var1, $var2 and $var3.
    The following code would be the code for your function:

    
    
    function myfunction($var1, $var2, $var3) {
    $total = $var1 + $var2 + $var3;
    echo $total;
    }

    ?>

    That adds them up and will output the total, if the function is used.

    To use the function, you can use the code you posted above, and it will output 6. Hope this helps and is what you mean.

    #57921
    jacorre
    Participant

    Thanks Bob. I think that’s fine if we know only three values are being passed in. If I don’t know how many values will be passed in, how should I approach it?

    For example, if the values being passed in are category ids like 1,2,3,4, I want to take those values and plug them into a function that uses exclude=1,2,3,4.

    #57890
    TT_Mark
    Member

    The you want to pass an array


    callSomeFunction( array('1', '2', '3') ) ;

    function callSomeFunction( $parameters=array() )
    {
    // Check array is not empty
    if ( !is_empty( $parameters ) )
    {
    // Loop through array values
    foreach ( $parameters AS $variable )
    {
    // Do Something
    }
    }
    }

    ?>
    #57891
    jacorre
    Participant

    Thanks TT_Mark. I think it’s a bit more complicated because of what I need to pass it into.

    So if someone used , then I wanted the values of 1,2,3 to be passed into get_categories('exclude=1,2,3')

    #57871
    richtestani
    Member

    While I’m not sure what you want to do, there a re a few ways to handle an unknown number of arguments.

    You could use the func_get_args() function which is used like:


    function my_function() {
    $args = func_get_args();
    foreach($args as $val) {
    echo $val;
    }
    }

    my_function(1, 2, 3, 4); --> 1234

    Another way is restricting to associative array, so you have named values.


    function my_function($array) {
    extract($array);
    if(isset($exclude)) {
    get_categories('exclude='.$exclude);
    }
    }

    $array = array('exclude'=>'1, 2, 3');
    my_function($array);

    I’m not much of a WordPress person but it seems many of the arguments are passed name/value pairs as string, so you’d need to build some of those strings.

    I have no idea if I helped here.

    #57874
    TT_Mark
    Member

    I have never heard of fun_get_args() before! Cheers for that @richtestani

    #57716
    Chris Coyier
    Keymaster

    Super helpful thread guys, love it.

    #57728
    jacorre
    Participant

    Thanks everyone. I think I may have been overcomplicating it. I can actually pass in ‘exclude=1,2,3’ as the argument and it works! So for example:

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Back End’ is closed to new topics and replies.