I have an if statement that is used in two different functions against the same conditions. The problem is that the conditions end up fairly long (5-7 ORs) and may need to be modified in the future. If they end up being modified, the conditions will change the same way in both functions.
Example of one if statement:
if ($this->object === 'one' || $this->object === 'two' || $this->object === 'three' ) {
echo 'Yes!';
} else {
echo 'No!';
};
I am thinking of creating an array and a function that will feed into the if statement but can't think of any way to get the if statement to check the condition rather than checking the presence of input.
$this->object = 'one';
$test_array = array('one', 'two', 'three');
function stmbuilder($array) {
$count = count($array);
$stm = '';
for ($i = 0; $i < $count; $i++) {
$intro = '$this->object ===';
$connector = ($i < $count-1 ? ' || ' : '');
$stm .= $intro . $array[$i] . $connector;
}
return $stm;
}
$condition = stmbuilder($test_array);
if ($condition) {
echo 'Yes!';
} else {
echo 'No!';
} //Will always echo Yes! since $condition has a value but does not check against what $this->object is
Any help is appreciated!
Aucun commentaire:
Enregistrer un commentaire