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 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 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
<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>
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.

preferences:
196.42 ms | 407 KiB | 5 Q