02c08-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 62 行

PHP
62
字号
<?php// An array of conversion functions to convert any unit to Kelvin$to_K = array(	'K' => create_function('$x', 'return $x;'),	'C' => create_function('$x', 'return $x + 273.15;'),	'F' => create_function('$x', 'return ($x + 459.67) / 1.8;'),	'R' => create_function('$x', 'return $x / 1.8;') );// And an array of conversion functions to convert Kelvin, to any unit$from_K = array(	'K' => create_function('$x', 'return $x;'),	'C' => create_function('$x', 'return $x - 273.15;'),	'F' => create_function('$x', 'return ($x * 1.8) - 459.67;'),	'R' => create_function('$x', 'return $x * 1.8;') );// The conversion function.  Pass it the value, the unit to convert from// and the unit to convert to.function convert_temp($value, $from, $to) {	global $to_K, $from_K;		// First check that we can understand the units passed to us else bail.	if (!(isset($to_K[$from]) && isset($from_K[$to]))) {		return false;	} else {		// Since we found the units all we need to do now is to convert the		//  value to Kelvin using the provided function, then convert it 		//  again to the desired unit.  Two function calls and we are done.		return $from_K[$to]($to_K[$from]($value));	}}// Output a Celsius <-> Fahrenheit comparison table with formatting:?><style>td, th { text-align: center; font-size: 20px; }.extreme { border: 2px solid red; }.hot { border: 2px solid orange; }.comfort { border: 2px solid green; }.cold { border: 2px solid blue; }.freeze { border: 2px solid navy; }</style><table> <tr><th>C</th><th>F</th></tr><?php// Loop from 50 C down to -10 C, by 5'sforeach (range(50, -10, 5) as $c) {	// Convert the temp	$f = convert_temp($c, 'C', 'F');		// Based upon known Farhenheit values, determine the range of heat:	if ($f > 100) { $heat = 'extreme'; }	elseif ($f > 80) { $heat = 'hot'; }	elseif ($f > 55) { $heat = 'comfort'; }	elseif ($f > 32) { $heat = 'cold'; }	else { $heat = 'freeze'; }		// Echo out the row of information, including formatting	echo "<tr><td class=\"{$heat}\">{$c}</td><td class=\"{$heat}\">{$f}</td></tr>";		}?></table>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?