Treehouse: Grow your CSS skills. Land your dream job.

Pass array into function

  • # February 26, 2011 at 11:26 pm

    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?

    # February 27, 2011 at 8:32 am

    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.

    # February 27, 2011 at 11:16 am

    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.

    # February 27, 2011 at 1:19 pm

    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

    # February 27, 2011 at 4:27 pm

    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')

    # February 27, 2011 at 10:43 pm

    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) {
    if(isset($exclude)) {

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

    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.

    # February 28, 2011 at 4:00 am

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

    # February 28, 2011 at 8:42 am

    Super helpful thread guys, love it.

    # February 28, 2011 at 11:54 am

    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') ?>

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.