06c08-2.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 30 行
PHP
30 行
<?php// Define an function that does lots of type checkingfunction many_hints($int, $flt, $str) { // Ensure that $int is an integer if (!(is_integer($int))) { trigger_error('$int is not of type integer', E_USER_ERROR); } // Ensure that $flt is an float if (!(is_float($flt))) { trigger_error('$flt is not of type float', E_USER_ERROR); } // Ensure that $str is an string if (!(is_string($str))) { trigger_error('$str is not of type integer', E_USER_ERROR); } // Now that we know we have proper variables, multiply the integer & // float together and return that with the string return ($int * $flt) . ' ' . $str;}// Use this first giving it valid parameters// This will print: '83.664 dollars owed'echo '<p>', many_hints(1992, .042, 'dollars owed'), '</p>';// Now try it again, Passing it incorrect data types.// An error will be generated because of the type mismatch.echo '<p>', many_hints('bob', 3, true), '</p>';?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?