Array as parameters in a PHP function

Posted on May 24, 2017 at 11:05 pm

Sample PHP function that uses an array to handle arguments:

function testFunction($args) {
    // $args contains the arguments of the function
    print_r($args);
    // Validate and assign params to variables
    $name = $args['name'] ? $args['name'] : "None";
    $file = $args['file'] ? $args['file'] : "None";
    $title = $args['title'] ? $args['title'] : "None";
}

Here is an example usage:

testFunction(array('name' => 'Andrew', 'file' => 'sample.png', 'title' => 'Example Title'));

I would use array as args if the function needs more than 2 params.

Other Posts

Updated Posts