faqts : Computers : Programming : Languages : PHP : kms : Functions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 18 people (94%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Can I call a function with an optional argument referenced by name?

Jul 10th, 1999 09:16
Nathan Wallace, Chad


If your function looks like:

    function myfunction (
        $req_prm1, 
        $req_prm2, 
        $opt_prm1 = "", 
        $opt_prm1 = "", 
        $opt_prm2 = "", 
        $opt_prm3 = "") {

        // .. some code ...

    }

You can call it as follows to only specify the third optional argument:

    // legal
    $result = myfunction($somedata,$somemore, "", "","an option");

You *cannot* specify the optional argument by name.  The following is
illegal in PHP:

    // illegal
    $result = myfunction($somedata,$somemore, $opt_prm3 = "an option");

The closest you'll get is passing an array to the function. The array
can then have an arbitrary number of elements, and you can use only the
ones set.