- PHP

How to throw an exception in PHP

To throw an exception, use throw new Exception().

Example:

<?php

$allowedRanks = ['admin', 'user'];
$rank = 'test';

if(!in_array($rank, $allowedRanks)) {
    throw new Exception("Rank [".$rank."] doesn't exist");
}

?>

Output:

Fatal error: Uncaught Exception: Rank [test] doesn't exist in [...]