3v4l.org

run code in 300+ PHP versions simultaneously
<?php function mysweetcode($argument){ $required=['first'];//specify required parameters here $default=['first'=>0,'second'=>1,'third'=>2];//define all parameters with their default values here $missing=[]; if(!is_array($argument)) return false; $argument=array_intersect_key($argument,$default); foreach($required as $k=>$v){//check for missing required parameters if(!isset($argument[$v])) $missing[]=$v; } if(!empty($missing)){// if required are missing trigger or throw error according to the PHP version $cm=count($missing); if (version_compare(PHP_VERSION, '7.0.0') < 0) { trigger_error(call_user_func_array('sprintf', array_merge(array('Required '.(($cm>1)?'parameters:':'parameter:'). str_repeat('%s,',$cm).(($cm>1)?' are':' is').' missing'),$missing)), E_USER_ERROR); }else{ throw new Error(call_user_func_array('sprintf',array_merge( array('Required '.(($cm>1)?'parameters:':'parameter:'). str_repeat('%s',$cm).(($cm>1)?' are':' is').' missing'),$missing))); } } $default=array_merge($default,$argument);//assign given values to parameters extract($default);/*extract the parameters to allow further checking and other operations in the function or method*/ unset($required,$missing,$argument,$default,$k,$v);//gain some space //then you can use $first,$second,$third in your code return $first+$second+$third; } var_dump(mysweetcode(['first'=>9,'third'=>8])); var_dump(mysweetcode(['third'=>8]));
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
int(18) Fatal error: Uncaught Error: Required parameter:first is missing in /in/HZ58e:21 Stack trace: #0 /in/HZ58e(39): mysweetcode(Array) #1 {main} thrown in /in/HZ58e on line 21
Process exited with code 255.
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 5.6.38
int(18) Fatal error: Required parameter:first, is missing in /in/HZ58e on line 19
Process exited with code 255.

preferences:
135.59 ms | 408 KiB | 5 Q