|
SimpleExample
The example.php file: <?php
/**
* A simple function that multiply two int or float and return a float number.
* It throws an exception if arguments given have a wrong type.
*
* <code>
*
* printf("%01.2f\n", multiply(3, 4));
* printf("%01.2f\n", multiply(3.2, 4));
* printf("%01.2f\n", multiply(3.2, 4.2));
* try {
* multiply('foo', 4.2);
* } catch (Exception $exc) {
* echo $exc->getMessage() . "\n";
* }
* try {
* multiply(3.2, 'foo');
* } catch (Exception $exc) {
* echo $exc->getMessage() . "\n";
* }
* // expects:
* // 12.00
* // 12.80
* // 13.44
* // Wrong type for first argument.
* // Wrong type for second argument.
*
* </code>
*
* @param mixed $a an int or a float
* @param mixed $b an int or a float
*
* @return float the result of the multiplication
* @throws Exception if arguments given have a wrong type
*/
function multiply($a, $b)
{
// check first arg type
if (!is_int($a) && !is_float($a)) {
throw new Exception("Wrong type for first argument.");
}
// check second arg type
if (!is_int($b) && !is_float($b)) {
throw new Exception("Wrong type for second argument.");
}
return (float)($a * $b);
}
?>And to run the tests in this example file: $ phpdt example.php [PASS] function multiply in file "/home/izi/example.php" Total time : 0.0446 sec. Passed tests : 1 Skipped tests : 0 Failed tests : 0 |
Sign in to add a comment