Skip to content

Instantly share code, notes, and snippets.

@nathan-fiscaletti
Last active December 31, 2018 01:41
Show Gist options
  • Save nathan-fiscaletti/77cbc3952de72d0248c60e2422905d44 to your computer and use it in GitHub Desktop.
Save nathan-fiscaletti/77cbc3952de72d0248c60e2422905d44 to your computer and use it in GitHub Desktop.
<?php
if (! function_exists('_or')) {
/**
* Shorthand OR comparison.
*
* @param mixed $compareTo
* @param mixed $values
*/
function _or(...$values) {
$result = false;
$compareTo = array_shift($values);
foreach($values as $value) {
$result = ($result || ($value == $compareTo));
if ($result)
break;
}
return $result;
}
}
if (! function_exists('_sor')) {
/**
* Strict shorthand OR comparison.
*
* @param mixed $compareTo
* @param mixed $values
*/
function _sor(...$values) {
$result = false;
$compareTo = array_shift($values);
foreach($values as $value) {
$result = ($result || ($value === $compareTo));
if ($result)
break;
}
return $result;
}
}
$a = 'foo';
if (_or($a, null, 'sex')) {
echo 'A is null or sex';
} else {
echo 'A is not null or sex';
}
// -- or --
echo 'A is' . ((_sor($a, null, 'sex')) ? '' : ' not') . ' null or sex.';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment