Skip to content

Instantly share code, notes, and snippets.

@BenMorel
Last active March 25, 2022 09:45
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 BenMorel/15878add393617a36486fbcb48805b71 to your computer and use it in GitHub Desktop.
Save BenMorel/15878add393617a36486fbcb48805b71 to your computer and use it in GitHub Desktop.
Returns constants matching an error level
<?php
/**
* Examples:
*
* errorLevelToConstants(E_WARNING) => ['E_WARNING', 'E_ALL']
* errorLevelToConstants(3) => ['E_ERROR', 'E_WARNING', 'E_ALL']
*
* @return string[]
*/
function errorLevelToConstants(int $level): array
{
$constants = [];
foreach (get_defined_constants() as $constant => $value) {
if (str_starts_with($constant, 'E_')) {
if (($level & $value) !== 0) {
$constants[] = $constant;
}
}
}
return $constants;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment