note 117882 deleted from functions.arguments by cmb

From: Date: Sun, 10 Nov 2019 13:17:39 +0000
Subject: note 117882 deleted from functions.arguments by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213022@lists.php.net to get a copy of this message
Note Submitter: d_maley at hotmail dot com ---- If you define your functions in the following way, you can call them whilst only specifying the default parameters you need 1. Define your function with its mandatory parameters, and an optional array 2. Declare your optional parameters as local variables 3. The crux: replace the value of any optional parameters that you have passed via the array, using PHP's facility to interpret variable variable names. This line is identical for every function 4. Call the function, passing its mandatory parameters, and only those optional parameters that you require For example, function test_params($a, $b, $arrOptionalParams = array()) { $c = 'sat'; $d = 'mat'; foreach($arrOptionalParams as $key => $value) ${$key} = $value; echo "$a $b $c on the $d"; } and then call it like this test_params('The', 'dog', array('c' => 'stood', 'd' => 'donkey')); test_params('The', 'cat', array('d' => 'donkey')); test_params('A', 'dog', array('c' => 'stood')); Results: The dog stood on the donkey The cat sat on the donkey A dog stood on the mat

« previous php.notes (#213022) next »