Friday, 26 June 2015

PHP: Use of Default Arguments in PHP

Use of default arguments is very common and helpful in programming. Default arguments provide a useful way for the smooth and correct execution of algorithms in the logic. I am going to share a professional usage of "php default argument array" in the codes.

I really love this method as it helps me a lot for merging user and default arguments in the different codes. You can say, it is one of Must Have Method, for every developer.

Let’s write a method "chip_print" which will print our output. I use this function regularly in my classes to get a quick print of string, variables and arrays.

Function chip_print

function chip_print( $var ) {
    echo "<pre>";
    print_r($var);
    echo "</pre>";
}

Function chip_parse_args

Now i am going to write the main method which will merge default arguments with user arguments.
function chip_parse_args( $args = array() ) {

    $defaults = array (
        'arg1'  =>   'default',
        'arg2'  =>   'default'
    );

    $result = array_merge( $defaults, $args );

    return $result;

}

You have noticed that i have used built-in function of PHP, array_merge.

Usage Example 1

You can use this function without passing any argument.

$result = chip_parse_args();
chip_print($result);

Output

Array
(
    [arg1] => default
    [arg2] => default
)

You have noticed that function has returned default values as we have passed not input to the method.

Usage Example 2

We will pass one argument to the method.
   
$args = array (
    'arg1'  =>   'modified1'
);
$result = chip_parse_args($args);
chip_print($result);

Output
   
Array
(
    [arg1] => modified1
    [arg2] => default
)

You have noticed that function has replaced argument 1 with the passed value.

Usage Example 3

We will pass two arguments as a test to the method.
   
$args = array (
    'arg1'  =>   'modified1',
    'arg2'  =>   'modified2'
);
$result = chip_parse_args($args);
chip_print($result);

Output

   
Array
(
    [arg1] => modified1
    [arg2] => modified2
)

You have noticed that function has replaced both arguments with the passed values.

Complete Tutorial

   
<?php

/*
|--------------
| Chip Print
|--------------
*/

function chip_print( $var ) {

    echo "<pre>";
    print_r($var);
    echo "</pre>";

}

/*
|--------------
| Chip Parse Arguments
|--------------
*/

function chip_parse_args( $args = array() ) {

    $defaults = array (
        'arg1'  =>   'default',
        'arg2'  =>   'default'
    );

    $result = array_merge( $defaults, $args );

    return $result;

}

/*
|--------------
| Example 1
|--------------
*/

$result = chip_parse_args();
chip_print($result);

/*
|--------------
| Example 2
|--------------
*/

$args = array (
    'arg1'  =>   'modified1'
);
$result = chip_parse_args($args);
chip_print($result);

/*
|--------------
| Example 3
|--------------
*/

$args = array (
    'arg1'  =>   'modified1',
    'arg2'  =>   'modified2'
);
$result = chip_parse_args($args);
chip_print($result);

?>

0 comments:

Post a Comment