2011-06-21, 02:52:41
After a little testing, this is the correct way to find out whether a variable ($eval) is NULL or non-FALSE:
This is way too confusing, so it’s much easier to just set $eval to FALSE and take it from there.
We now have two proposed solutions to this problem, both have two alternative ways of writing—since they can even be made to work with a break or with two seperate returns. The only real difference between the solutions is mvlcek’s returns TRUE when a component is found, no matter what happens with eval() while mine will return TRUE only when a component is found and was correctly eval()’d.
Should this be put up for vote or something? :p
mvlcek:
Uses 2 separate returns (as preferred by mvlcek over breaks). The function will return TRUE when a component exists with the provided $id and FALSE if there doesn’t.
Zegnåt:
Uses a break to stop the loop after a component has been found. The function will return TRUE when the component eval()’d correctly and FALSE if no component has been found to match the provided $id or the component produced PHP errors when being eval()’d. (In debug mode, these errors are still shown, nothing is hidden!)
Code:
!isset($eval)&&array_key_exists('eval',get_defined_vars())||isset($eval)&&$eval!==FALSE
We now have two proposed solutions to this problem, both have two alternative ways of writing—since they can even be made to work with a break or with two seperate returns. The only real difference between the solutions is mvlcek’s returns TRUE when a component is found, no matter what happens with eval() while mine will return TRUE only when a component is found and was correctly eval()’d.
Should this be put up for vote or something? :p
mvlcek:
Code:
function get_component($id) {
global $components;
if (func_num_args() > 1) { $args = func_get_args(); array_shift($args); }
if (!$components) {
if (file_exists(GSDATAOTHERPATH.'components.xml')) {
$data = getXML(GSDATAOTHERPATH.'components.xml');
$components = $data->item;
} else {
$components = array();
}
}
if (count($components) > 0) {
foreach ($components as $component) {
if ($id == $component->slug) {
eval("?>" . strip_decode($component->value) . "<?php ");
return true;
}
}
}
return false;
}
Zegnåt:
Code:
function get_component($id) {
global $components;
$eval = FALSE;
if (func_num_args() > 1) { $args = func_get_args(); array_shift($args); }
if (!$components) {
if (file_exists(GSDATAOTHERPATH.'components.xml')) {
$data = getXML(GSDATAOTHERPATH.'components.xml');
$components = $data->item;
} else {
$components = array();
}
}
if (count($components) > 0) {
foreach ($components as $component) {
if ($id == $component->slug) {
$eval = eval("?>" . strip_decode($component->value) . "<?php ");
break;
}
}
}
return $eval!==FALSE;
}
“Don’t forget the important ˚ (not °) on the a,†says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!