mcaskill/php-fallback

Sets a given variable if it is not set.

v1.0.0 2018-09-10 01:35 UTC

This package is not auto-updated.

Last update: 2020-10-19 11:33:36 UTC


README

(PHP 5 >= 5.6)
fallback — Sets a given variable if it is not set.

Description

mixed fallback( mixed &$var [, mixed $... ] )

This function sets $var if it is not set with the last parameter or the first non-empty value.

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

Parameters

  • var — The variable to return or set.
  • ... — Another variable…

Return Values

Returns the value of whatever was chosen.

Examples

Example #1 fallback() example

function datetime($when = null) {
	fallback($when, time());
	$time = (is_numeric($when)) ? $when : strtotime($when) ;
	return date("Y-m-d", $time);
}

$nextWeek = time() + (7 * 24 * 60 * 60);

echo 'Now:       '. datetime() ."\n";
echo 'Next Week: '. datetime($nextWeek) ."\n";

The above example will output:

Now:       2005-03-30
Next Week: 2005-04-06

Installation

With Composer

$ composer require mcaskill/php-fallback

Without Composer

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