3v4l.org

run code in 300+ PHP versions simultaneously
<?php class UnitConverter { public $vulgar_to_float = ['½' => 0.5]; public function replaceUnicode( $amount = 0 ){ $half = $this->vulgar_to_float['½']; return ( is_numeric( $amount ) )? $amount : $half; } public function convertAmount($amount, $from, $to){ if($from === 'pound' && $to === 'ounce'){ return $amount * 16; } else if($from === 'cup' && $to === 'tablespoon'){ return $this->replaceUnicode( $amount ) * 16; } else { throw new Exception('Unable to convert ' . $from . ' to ' . $to); } } } function convertIngredients( $arr = null){ $unit_names = array_column($arr, 'unit_name'); foreach($unit_names as $key => $unit){ if( $unit === 'pound'){ $arr[$key]['amount'] = (new UnitConverter())->convertAmount($arr[$key]['amount'], $unit, 'ounce'); $arr[$key]['unit_name'] = 'ounce'; } else if($unit === 'cup'){ $arr[$key]['amount'] = (new UnitConverter())->convertAmount($arr[$key]['amount'], $unit, 'tablespoon'); $arr[$key]['unit_name'] = 'tablespoon'; } else { continue; } } return $arr; } function generateBreakfast(){ $array = [ 0 => array( 'amount' => 1, 'unit_name' => 'cup', 'ingredient_name' => 'almond flour' ), 1 => array( 'amount' => '½', 'unit_name' => 'cup', 'ingredient_name' => 'erythritol' ), 2 => array( 'amount' => 1, 'unit_name' => 'large', 'ingredient_name' => 'egg' ) ]; return convertIngredients($array); } echo '<pre>'; print_r(generateBreakfast()); echo '</pre>';
Output for git.master, git.master_jit, rfc.property-hooks
<pre>Array ( [0] => Array ( [amount] => 16 [unit_name] => tablespoon [ingredient_name] => almond flour ) [1] => Array ( [amount] => 8 [unit_name] => tablespoon [ingredient_name] => erythritol ) [2] => Array ( [amount] => 1 [unit_name] => large [ingredient_name] => egg ) ) </pre>

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:
157.45 ms | 406 KiB | 5 Q