treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Pass array into function

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

    <?php myfunction(1,2,3); ?>


    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?
  • 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:

    <?php 

    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.
  • 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.
  • The you want to pass an array


    <?php
    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
    }
    }
    }

    ?>
  • Thanks TT_Mark. I think it's a bit more complicated because of what I need to pass it into.

    So if someone used <?php myfunction(array(1,2,3)) ?>, then I wanted the values of 1,2,3 to be passed into get_categories('exclude=1,2,3')
  • 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.
  • I have never heard of fun_get_args() before! Cheers for that @richtestani
  • Super helpful thread guys, love it.
  • 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:

    <?php myfunction('exclude=1,2,3') ?>