1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-30 19:30:25 +02:00

added vartrue(), alias of varsettrue(); added deftrue(), alias of defsettrue()

This commit is contained in:
secretr
2009-08-28 00:22:08 +00:00
parent af00e68bf5
commit ceef12645c

View File

@@ -9,46 +9,99 @@
* e107 Core functions
*
* $Source: /cvs_backup/e107_0.8/e107_handlers/core_functions.php,v $
* $Revision: 1.1 $
* $Date: 2009-08-05 19:53:47 $
* $Revision: 1.2 $
* $Date: 2009-08-28 00:22:08 $
* $Author: secretr $
*/
//
// Use these to combine isset() and use of the set value. or defined and use of a constant
// i.e. to fix if($pref['foo']) ==> if ( varset($pref['foo']) ) will use the pref, or ''.
// Can set 2nd param to any other default value you like (e.g. false, 0, or whatever)
// $testvalue adds additional test of the value (not just isset())
// Examples:
// $something = pref; // Bug if pref not set ==> $something = varset(pref);
// $something = isset(pref) ? pref : ""; ==> $something = varset(pref);
// $something = isset(pref) ? pref : default; ==> $something = varset(pref,default);
// $something = isset(pref) && pref ? pref : default; ==> use varsettrue(pref,default)
//
/**
* Use these to combine isset() and use of the set value. or defined and use of a constant
* i.e. to fix if($pref['foo']) ==> if ( varset($pref['foo']) ) will use the pref, or ''.
* Can set 2nd param to any other default value you like (e.g. false, 0, or whatever)
* $testvalue adds additional test of the value (not just isset())
* Examples:
* <code>
* $something = pref; Bug if pref not set ==> $something = varset(pref);
* $something = isset(pref) ? pref : ""; ==> $something = varset(pref);
* $something = isset(pref) ? pref : default; ==> $something = varset(pref,default);
* $something = isset(pref) && pref ? pref : default; ==> use varsettrue(pref,default)
* </code>
*
* @param mixed $val
* @param mixed $default [optional]
* @return mixed
*/
function varset(&$val, $default='')
{
if (isset($val)) { return $val; }
return $default;
}
/**
* Check if the given string is defined (constant)
*
* @param string $str
* @param mixed $default [optional]
* @return
*/
function defset($str, $default='')
{
if (defined($str)) { return constant($str); }
return $default;
}
//
// These variants are like the above, but only return the value if both set AND 'true'
//
/**
* Variant of {@link varset()}, but only return the value if both set AND 'true'
*
* @param mixed $val
* @param mixed $default [optional]
* @return mixed
*/
function varsettrue(&$val, $default='')
{
if (isset($val) && $val) { return $val; }
return $default;
}
function defsettrue($str,$default='')
/**
* Alias of {@link varsettrue()}
*
* @param mixed $val
* @param mixed $default [optional]
* @return mixed
*/
function vartrue(&$val, $default='')
{
if (defined($str) && constant($str)) {return constant($str); }
return varsettrue($val, $default);
}
/**
* Variant of {@link defset()}, but only return the value if both defined AND 'true'
*
* @param string $str
* @param mixed $default [optional]
* @return mixed
*/
function defsettrue($str, $default='')
{
if (defined($str) && constant($str)) { return constant($str); }
return $default;
}
/**
* Alias of {@link defsettrue()}
*
* @param string $str
* @param mixed $default [optional]
* @return mixed
*/
function deftrue($str, $default='')
{
if (defined($str) && constant($str)) { return constant($str); }
return $default;
}