Last active
July 19, 2018 23:53
-
-
Save yoander/ea8d2f89479e5845a8278d8d317c1e5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function validateAge($age) | |
{ | |
if (!is_int($age)) { | |
throw new InvalidArgumentException('Age must be integer!'); | |
} | |
if (($age < 0) || ($age > 200)) { | |
throw new DomainException('Age must be a value between 0 and 200!'); | |
} | |
return true; | |
} | |
foreach ([3.5, 250] as $age) { | |
try { | |
if (validateAge($age)) { | |
echo 'Valid age!', nl2br("\n"); | |
} | |
} catch (InvalidArgumentException|DomainException $e) { | |
echo $e->getMessage(), nl2br("\n"); | |
} | |
} | |
// Age must be integer! | |
// Age must be a value between 0 and 200! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment