Skip to content

Instantly share code, notes, and snippets.

@mcaskill
Last active September 10, 2018 01:49
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 mcaskill/c9c5a98477c463f3fbb8c7c97ca54dad to your computer and use it in GitHub Desktop.
Save mcaskill/c9c5a98477c463f3fbb8c7c97ca54dad to your computer and use it in GitHub Desktop.
PHP : Returns the first argument that is set and non-empty.

oneof

(PHP 5 >= 5.6)
oneof — Returns the first argument that is set and non-empty.

Description

mixed oneof( mixed $... )

This function returns the first argument that is set and non-empty.

It will guess where to stop based on the types of the arguments, e.g. "" has priority over array() but not 1.

Based on Alex Suraci's oneof() function. Updated for PHP 5.6.

Parameters

  • ... — One or more variables to be checked.

Return Values

Returns the first argument that is set and non-empty.

Examples

Example #1 oneof() example

$created_at = null;
$updated_at = '2005-04-06';

echo 'Created At: '. oneof($created_at, date('Y-m-d')) ."\n";
echo 'Updated At: '. oneof($updated_at, null) ."\n";

The above example will output:

Created At: 2005-03-30
Updated At: 2005-04-06

Installation

With Composer

$ composer require mcaskill/php-oneof

Without Composer

Why are you not using composer? Download Function.OneOf.php from the gist and save the file into your project path somewhere.

{
"name": "mcaskill/php-oneof",
"description": "Returns the first argument that is set and non-empty.",
"license": "MIT",
"authors": [
{
"name": "Chauncey McAskill",
"email": "chauncey@mcaskill.ca",
"homepage": "https://github.com/mcaskill"
}
],
"keywords": [
"function"
],
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"require": {
"php": ">=5.6.0"
},
"autoload": {
"files": [ "Function.OneOf.php" ]
}
}
<?php
if (!function_exists('oneof')) {
/**
* Returns the first argument that is set and non-empty.
*
* It will guess where to stop based on the types of the arguments, e.g.
* "" has priority over array() but not 1.
*
* @link https://github.com/vito/chyrp/ Origin of function.
* @param mixed ...$args Variable list of arguments to check.
* @return mixed The value of the first non-empty argument.
*/
function oneof(...$args)
{
$last = null;
foreach ($args as $index => $arg) {
if (!isset($arg) ||
(is_string($arg) && (trim($arg) === '')) ||
($arg === []) ||
(is_object($arg) && empty($arg)) ||
($arg === '0000-00-00 00:00:00')
) {
$last = $arg;
} else {
return $arg;
}
if (($index + 1) == count($args)) {
break;
}
$next = $args[($index + 1)];
/**
* This is a big check but it should cover most "incomparable" cases.
* Using simple type comparison wouldn't work too well, for example
* when "" would take priority over 1 in oneof("", 1) because they're
* different types.
*/
$incomparable = (
(is_array($arg) && !is_array($next)) ||
(!is_array($arg) && is_array($next)) ||
(is_object($arg) && !is_object($next)) ||
(!is_object($arg) && is_object($next)) ||
(is_resource($arg) && !is_resource($next)) ||
(!is_resource($arg) && is_resource($next))
);
if (isset($arg) && isset($next) && $incomparable) {
return $arg;
}
}
return $last;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment