Skip to content

Instantly share code, notes, and snippets.

@yoander
Last active July 19, 2018 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoander/ea8d2f89479e5845a8278d8d317c1e5c to your computer and use it in GitHub Desktop.
Save yoander/ea8d2f89479e5845a8278d8d317c1e5c to your computer and use it in GitHub Desktop.
<?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");
}
}
// Print
// 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