So is there a way to stack allllll those needs for IF into a function? I think so...as long as the arguments dont get ridiculous. Lets use a starting point inspiration: the Wordpress checked() function - http://codex.wordpress.org/Function_Reference/checked Its basically an IF based on some setting which adds the word "checked". Its handy and all, but its limited to checkboxes (due to static output). Could we make it better?
After tinkering around for awhile, this seemed to encompass much of it. Basically its a barebones IF template for OC settings or beyond. It works to check if a var is yes/no, set, or exists, then spits out your 3 [optional] outputs. At the very least it will standardize var checks into 0 or 1 if nothing is set besides the check var.
Perhaps this could be better, any input is appreciated to make an uber helper. The test thus far:
Code: Select all
<?php
// a generic IF check function
function check($trigger, $true = 1, $false = 0, $default = 0) {
$output = '';
if (($trigger === false) || ($trigger == 'false') || ($trigger === 0)) {
$output = $false;
} elseif (empty($trigger) || !isset($trigger)) {
$output = $default;
} elseif (($trigger === true) || ($trigger == 'true') || ($trigger === 1)) {
$output = $true;
} elseif (!empty($trigger) || isset($trigger)) {
$output = $true;
}
return $output;
}
// define a var for test
$trigger_sample = 'Hi There, i am the contents of a field';
// define a function for test
function sayThis() {
echo 'Hey too, i am a function';
}
?>
<p>This text field or string exists: <?php echo check($trigger_sample, $trigger_sample, 'False Output', 'Default Output'); ?>
<p>This text field or string exists and returns a function: <?php echo check($trigger_sample, sayThis(), 'False Output', 'Default Output'); ?>
<p>This does not exist: <?php echo check($fff2341351324, 'True Output', 'False Output', 'Default Output'); ?></p>
<p>This is set to false: <?php echo check(false, 'True Output', 'False Output', 'Default Output'); ?></p>
<p>This is set to "true": <?php echo check('true', 'True Output', 'False Output', 'Default Output'); ?></p>
<p>This is set to 0: <?php echo check(0, 'True Output', 'False Output', 'Default Output'); ?></p>
<p>This is set to "1": <?php echo check(1, 'True Output', 'False Output', 'Default Output'); ?></p>