note 117882 added to functions.arguments

From: Date: Wed, 26 Aug 2015 09:12:17 +0000
Subject: note 117882 added to functions.arguments
Groups: php.notes 
Request: Send a blank email to php-notes+get-203472@lists.php.net to get a copy of this message
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 ---- Server IP: 72.52.91.14 Probable Submitter: 194.80.47.147 ---- Manual Page -- http://php.net/manual/en/functions.arguments.php Edit -- https://master.php.net/note/edit/117882 Del: integrated -- https://master.php.net/note/delete/117882/integrated Del: useless -- https://master.php.net/note/delete/117882/useless Del: bad code -- https://master.php.net/note/delete/117882/bad+code Del: spam -- https://master.php.net/note/delete/117882/spam Del: non-english -- https://master.php.net/note/delete/117882/non-english Del: in docs -- https://master.php.net/note/delete/117882/in+docs Del: other reasons-- https://master.php.net/note/delete/117882 Reject -- https://master.php.net/note/reject/117882 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203472) next »