Option A)
<?php function registerTemplateA() { // loop over every variable defined in the global scope, // such as those you created there when calling this function foreach($GLOBALS as $potentialKey => $potentialValue) { $valueArgs = func_get_args(); if (in_array($potentialValue, $valueArgs)) { // this variable seems to match a _value_ you passed in $args[$potentialKey] = $potentialValue; } } // you now have an associative array in $args print_r($args); } registerTemplateA($name = "my template", $screenshot = "screenshot.png", $description = "nice template"); ?>
Option B)
<?php function registerTemplateB() {
Option C)
<?php function registerTemplateC($args) { // you now have an associative array in $args print_r($args); } registerTemplateC(array('name' => 'my template', 'screenshot' => 'screenshot.png', 'description' => 'nice template')); ?>
Conclusion: option C is the best "for minimal code"
(Note: this answer is valid PHP code with open and closing tags in the right places, verified using PHP 5.2.x and should also work on PHP 4 ... so try it if you need to.)
source share