使用php函数打印动态js文件的列表


print list of dynamic js file using php function

我有以下代码用于在索引页中使用php函数打印js文件:

<? PHP
    function admin_js($js, $file = '') {
        global $_admin_js;
        if ($file != '') {
            $file = preg_replace('/'''/', '/', dirname($file));
        }
        $_admin_js[] = array(
            $js,
            $file
        );
    }
    $_admin_js = array();
    global $_admin_js;
    $value = '';
    foreach($_admin_js as $js) {
        $relative_path = RELATIVE_PATH;
        echo '<script src="' . $relative_path . '/' . preg_replace('/'''/', '/', $js[0]) . '"></script>' . " 'n't";
    }
    global $_admin_js;
    admin_js('admin/templates/js/jquery-1.10.1.min.js');
    admin_js('admin/templates/js/bootstrap.min.js');
?>

但是在实际操作中php输出为空。我有什么问题?如何打印?!

此处演示

您的代码序列错误。

<? PHP
 function admin_js($js, $file = '')
 {
     global $_admin_js;
     if ($file != '')
     {
        $file = preg_replace('/'''/', '/', dirname($file));
     }
     $_admin_js[] = array( $js, $file);
}
/* here should be the code of declaration */
$_admin_js = array();
global $_admin_js;
$value = '';
// Call this first to populate the array of $_admin_js
admin_js('admin/templates/js/jquery-1.10.1.min.js');
admin_js('admin/templates/js/bootstrap.min.js');
//Now you get the filled up array...
foreach($_admin_js as $js)
{
     $relative_path = RELATIVE_PATH;
     echo '<script src="' . $relative_path . '/' . preg_replace('/'''/', '/', $js[0]) . '"></script>' . " 'n't";
}