The following warnings occurred:
Warning [2] Undefined array key "threadviews_countguests" - Line: 745 - File: showthread.php PHP 8.1.31 (Linux)
File Line Function
/inc/class_error.php 153 errorHandler->error
/showthread.php 745 errorHandler->error_callback
Warning [2] Undefined array key "allowautourl" - Line: 584 - File: inc/class_parser.php PHP 8.1.31 (Linux)
File Line Function
/inc/class_error.php 153 errorHandler->error
/inc/class_parser.php 584 errorHandler->error_callback
/inc/class_parser.php 228 postParser->parse_mycode
/inc/functions_post.php 830 postParser->parse_message
/showthread.php 916 build_postbit
Warning [2] Undefined array key "allowautourl" - Line: 584 - File: inc/class_parser.php PHP 8.1.31 (Linux)
File Line Function
/inc/class_error.php 153 errorHandler->error
/inc/class_parser.php 584 errorHandler->error_callback
/inc/class_parser.php 228 postParser->parse_mycode
/inc/functions_post.php 861 postParser->parse_message
/showthread.php 916 build_postbit
Warning [2] Undefined property: MyLanguage::$thread_modes - Line: 46 - File: showthread.php(1650) : eval()'d code PHP 8.1.31 (Linux)
File Line Function
/inc/class_error.php 153 errorHandler->error
/showthread.php(1650) : eval()'d code 46 errorHandler->error_callback
/showthread.php 1650 eval




Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
optional parameters of "component"
#20
After a little testing, this is the correct way to find out whether a variable ($eval) is NULL or non-FALSE:
Code:
!isset($eval)&&array_key_exists('eval',get_defined_vars())||isset($eval)&&$eval!==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:
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;
}
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:
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;
}
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!)
“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!
Reply


Messages In This Thread
optional parameters of "component" - by bugman - 2011-06-08, 21:29:05
optional parameters of "component" - by bugman - 2011-06-08, 21:46:44
optional parameters of "component" - by mvlcek - 2011-06-08, 21:53:29
optional parameters of "component" - by bugman - 2011-06-08, 22:03:32
optional parameters of "component" - by bugman - 2011-06-08, 22:23:43
optional parameters of "component" - by mvlcek - 2011-06-18, 07:23:05
optional parameters of "component" - by mvlcek - 2011-06-18, 21:33:07
optional parameters of "component" - by ccagle8 - 2011-06-20, 09:10:54
optional parameters of "component" - by mvlcek - 2011-06-20, 16:27:53
optional parameters of "component" - by mvlcek - 2011-06-20, 19:36:30
optional parameters of "component" - by mvlcek - 2011-06-20, 20:45:03
optional parameters of "component" - by mvlcek - 2011-06-20, 23:55:27
optional parameters of "component" - by Zegnåt - 2011-06-21, 02:52:41
optional parameters of "component" - by mvlcek - 2011-06-21, 05:28:08



Users browsing this thread: 5 Guest(s)