Speed up development with full-stack environments for every branch.

Learn More

How to implement the InvalidArgumentException class [PHP and spl]

140 Runs 1325 Views 223 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php
ini_set('display_errors',1);
/*
 * PHP SPL - How to implement the InvalidArgumentException class
 */

function doubleInt($int) {
    if (!is_int($int))
        throw new InvalidArgumentException('doubleInt function only accepts integers. Input was: ' . $int);
    return $int * 2;
}

//the first call will run just fine
echo doubleInt(4) . "<br />"; 
//exception will be thrown as 2.5 is a float
echo doubleInt(2.5) . "<br />"; 
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).