3v4l.org

run code in 300+ PHP versions simultaneously
<?php function namedParams (array $params, array $defaults){ $params += $defaults; $allowedKeys = array_keys($defaults); $isUnexpected = array_filter($params, function ($k) use ($allowedKeys){ return !in_array($k, $allowedKeys); }, ARRAY_FILTER_USE_KEY); if(!empty($isUnexpected)){ throw new InvalidArgumentException('Unexpected parameters: '.implode(',',array_keys($isUnexpected))); } return $params; } function test (array $params){ //we can try ... and catch any problems if we want, but to keep it simple, we can just do this. //These and only these params are expected. Plus, we give them default values. $paramsProcessed = namedParams($params,[ 'name' => 'Unknown', 'sex' => 'man', 'something' => NULL, ]); var_dump($paramsProcessed); } test(['name' => "Konrad"]);
Output for git.master, git.master_jit, rfc.property-hooks
array(3) { ["name"]=> string(6) "Konrad" ["sex"]=> string(3) "man" ["something"]=> NULL }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
170.17 ms | 405 KiB | 5 Q