[UPHPU] pass variables into a function

Walt Haas haas at xmission.com
Sat Jan 10 08:14:20 MST 2009


The usual approach would be to write

function do_something_awesome( $params = array() )
{
    if ( array_key_exists( 'a', $params ) ) {
        $param_a = $params( 'a' );
    } else {
        $param_a = 'a';
    }
    if ( array_key_exists( 'b', $params ) ) {
        $param_b = $params( 'b' );
    } else {
        $param_b = 'b';
    }
}

Then you can write calls like

do_something_awesome( );
do_something_awesome( array( 'a' => 'foo' ) );
do_something_awesome( array( 'b' => 'bar' ) );
do_something_awesome( array( 'a' => 'foo', 'b' => 'bar' ) );

-- Walt

Wade Preston Shearer wrote:
> Consider the following code:
>
> function do_something_awesome($a = 'a', $b = 'b') {}
>
>
> I have set defaults for the variables that the function accepts. I can 
> override the defaults, like this:
>
> do_something_awesome('c', 'd');
>
>
> …or…
>
> $a = 'c';
> $b = 'd';
> do_something_awesome($a, $b);
>
>
> And, I can passing in the first while letting the second use the 
> default, like this:
>
> do_something_awesome('c');
>
>
> But what if I only want to pass in the second variable? Is that possible?
> ------------------------------------------------------------------------
>
>
> _______________________________________________
>
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net



More information about the UPHPU mailing list