Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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