3v4l.org

run code in 300+ PHP versions simultaneously
<?php /*************************************************************************** * template.php * ------------------- * begin : Saturday, Feb 13, 2001 * copyright : (C) 2001 The phpBB Group * email : support@phpbb.com * * $Id: template.php 66 2010-09-23 11:49:44Z guob7366 $ * * ***************************************************************************/ /*************************************************************************** * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * ***************************************************************************/ /** * Template class. By Nathan Codding of the phpBB group. * The interface was originally inspired by PHPLib templates, * and the template file formats are quite similar. * */ class Template { var $classname = "Template"; // variable that holds all the data we'll be substituting into // the compiled templates. // ... // This will end up being a multi-dimensional array like this: // $this->_tpldata[block.][iteration#][child.][iteration#][child2.][iteration#][variablename] == value // if it's a root-level variable, it'll be like this: // $this->_tpldata[.][0][varname] == value var $_tpldata = array(); // Hash of filenames for each template handle. var $files = array(); // Root template directory. var $root = ""; // this will hash handle names to the compiled code for that handle. var $compiled_code = array(); // This will hold the uncompiled code for that handle. var $uncompiled_code = array(); /** * Constructor. Simply sets the root dir. * */ function Template($root = ".") { $this->set_rootdir($root); } /** * Destroys this template object. Should be called when you're done with it, in order * to clear out the template data so you can load/parse a new template set. */ function destroy() { $this->_tpldata = array(); } /** * Sets the template root directory for this Template object. */ function set_rootdir($dir) { if (!is_dir($dir)) { return false; } $this->root = $dir; return true; } /** * Sets the template filenames for handles. $filename_array * should be a hash of handle => filename pairs. */ function set_filenames($filename_array) { if (!is_array($filename_array)) { return false; } reset($filename_array); while(list($handle, $filename) = each($filename_array)) { $this->files[$handle] = $this->make_filename($filename); } return true; } /** * Load the file for the handle, compile the file, * and run the compiled code. This will print out * the results of executing the template. */ function pparse($handle) { if (!$this->loadfile($handle)) { die("Template->pparse(): Couldn't load template file for handle $handle"); } // actually compile the template now. if (!isset($this->compiled_code[$handle]) || empty($this->compiled_code[$handle])) { // Actually compile the code now. $this->compiled_code[$handle] = $this->compile($this->uncompiled_code[$handle]); } // Run the compiled code. eval($this->compiled_code[$handle]); return true; } /** * Inserts the uncompiled code for $handle as the * value of $varname in the root-level. This can be used * to effectively include a template in the middle of another * template. * Note that all desired assignments to the variables in $handle should be done * BEFORE calling this function. */ function assign_var_from_handle($varname, $handle) { if (!$this->loadfile($handle)) { die("Template->assign_var_from_handle(): Couldn't load template file for handle $handle"); } // Compile it, with the "no echo statements" option on. $_str = ""; $code = $this->compile($this->uncompiled_code[$handle], true, '_str'); // evaluate the variable assignment. eval($code); // assign the value of the generated variable to the given varname. $this->assign_var($varname, $_str); return true; } /** * Block-level variable assignment. Adds a new block iteration with the given * variable assignments. Note that this should only be called once per block * iteration. */ function assign_block_vars($blockname, $vararray) { if (strstr($blockname, '.')) { // Nested block. $blocks = explode('.', $blockname); $blockcount = sizeof($blocks) - 1; $str = '$this->_tpldata'; for ($i = 0; $i < $blockcount; $i++) { $str .= '[\'' . $blocks[$i] . '.\']'; eval('$lastiteration = sizeof(' . $str . ') - 1;'); $str .= '[' . $lastiteration . ']'; } // Now we add the block that we're actually assigning to. // We're adding a new iteration to this block with the given // variable assignments. $str .= '[\'' . $blocks[$blockcount] . '.\'][] = $vararray;'; // Now we evaluate this assignment we've built up. eval($str); } else { // Top-level block. // Add a new iteration to this block with the variable assignments // we were given. $this->_tpldata[$blockname . '.'][] = $vararray; } return true; } /** * Root-level variable assignment. Adds to current assignments, overriding * any existing variable assignment with the same name. */ function assign_vars($vararray) { reset ($vararray); while (list($key, $val) = each($vararray)) { $this->_tpldata['.'][0][$key] = $val; } return true; } /** * Root-level variable assignment. Adds to current assignments, overriding * any existing variable assignment with the same name. */ function assign_var($varname, $varval) { $this->_tpldata['.'][0][$varname] = $varval; return true; } /** * Generates a full path+filename for the given filename, which can either * be an absolute name, or a name relative to the rootdir for this Template * object. */ function make_filename($filename) { // Check if it's an absolute or relative path. if (substr($filename, 0, 1) != '/') { $filename = ($rp_filename = realpath($this->root . '/' . $filename)) ? $rp_filename : $filename; } if (!file_exists($filename)) { die("Template->make_filename(): Error - file $filename does not exist"); } return $filename; } /** * If not already done, load the file for the given handle and populate * the uncompiled_code[] hash with its code. Do not compile. */ function loadfile($handle) { // If the file for this handle is already loaded and compiled, do nothing. if (isset($this->uncompiled_code[$handle]) && !empty($this->uncompiled_code[$handle])) { return true; } // If we don't have a file assigned to this handle, die. if (!isset($this->files[$handle])) { die("Template->loadfile(): No file specified for handle $handle"); } $filename = $this->files[$handle]; $str = implode("", @file($filename)); if (empty($str)) { die("Template->loadfile(): File $filename for handle $handle is empty"); } $this->uncompiled_code[$handle] = $str; return true; } /** * Compiles the given string of code, and returns * the result in a string. * If "do_not_echo" is true, the returned code will not be directly * executable, but can be used as part of a variable assignment * for use in assign_code_from_handle(). */ function compile($code, $do_not_echo = false, $retvar = '') { // replace \ with \\ and then ' with \'. $code = str_replace('\\', '\\\\', $code); $code = str_replace('\'', '\\\'', $code); // change template varrefs into PHP varrefs // This one will handle varrefs WITH namespaces $varrefs = array(); preg_match_all('#\{(([a-z0-9\-_]+?\.)+?)([a-z0-9\-_]+?)\}#is', $code, $varrefs); $varcount = sizeof($varrefs[1]); for ($i = 0; $i < $varcount; $i++) { $namespace = $varrefs[1][$i]; $varname = $varrefs[3][$i]; $new = $this->generate_block_varref($namespace, $varname); $code = str_replace($varrefs[0][$i], $new, $code); } // This will handle the remaining root-level varrefs $code = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '\' . ( ( isset($this->_tpldata[\'.\'][0][\'\1\']) ) ? $this->_tpldata[\'.\'][0][\'\1\'] : \'\' ) . \'', $code); // Break it up into lines. $code_lines = explode("\n", $code); $block_nesting_level = 0; $block_names = array(); $block_names[0] = "."; // Second: prepend echo ', append ' . "\n"; to each line. $line_count = sizeof($code_lines); for ($i = 0; $i < $line_count; $i++) { $code_lines[$i] = chop($code_lines[$i]); if (preg_match('#<!-- BEGIN (.*?) -->#', $code_lines[$i], $m)) { $n[0] = $m[0]; $n[1] = $m[1]; // Added: dougk_ff7-Keeps templates from bombing if begin is on the same line as end.. I think. :) if ( preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $n) ) { $block_nesting_level++; $block_names[$block_nesting_level] = $m[1]; if ($block_nesting_level < 2) { // Block is not nested. $code_lines[$i] = '$_' . $n[1] . '_count = ( isset($this->_tpldata[\'' . $n[1] . '.\']) ) ? sizeof($this->_tpldata[\'' . $n[1] . '.\']) : 0;'; $code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)'; $code_lines[$i] .= "\n" . '{'; } else { // This block is nested. // Generate a namespace string for this block. $namespace = implode('.', $block_names); // strip leading period from root level.. $namespace = substr($namespace, 2); // Get a reference to the data array for this block that depends on the // current indices of all parent blocks. $varref = $this->generate_block_data_ref($namespace, false); // Create the for loop code to iterate over this block. $code_lines[$i] = '$_' . $n[1] . '_count = ( isset(' . $varref . ') ) ? sizeof(' . $varref . ') : 0;'; $code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)'; $code_lines[$i] .= "\n" . '{'; } // We have the end of a block. unset($block_names[$block_nesting_level]); $block_nesting_level--; $code_lines[$i] .= '} // END ' . $n[1]; $m[0] = $n[0]; $m[1] = $n[1]; } else { // We have the start of a block. $block_nesting_level++; $block_names[$block_nesting_level] = $m[1]; if ($block_nesting_level < 2) { // Block is not nested. $code_lines[$i] = '$_' . $m[1] . '_count = ( isset($this->_tpldata[\'' . $m[1] . '.\']) ) ? sizeof($this->_tpldata[\'' . $m[1] . '.\']) : 0;'; $code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)'; $code_lines[$i] .= "\n" . '{'; } else { // This block is nested. // Generate a namespace string for this block. $namespace = implode('.', $block_names); // strip leading period from root level.. $namespace = substr($namespace, 2); // Get a reference to the data array for this block that depends on the // current indices of all parent blocks. $varref = $this->generate_block_data_ref($namespace, false); // Create the for loop code to iterate over this block. $code_lines[$i] = '$_' . $m[1] . '_count = ( isset(' . $varref . ') ) ? sizeof(' . $varref . ') : 0;'; $code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)'; $code_lines[$i] .= "\n" . '{'; } } } else if (preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $m)) { // We have the end of a block. unset($block_names[$block_nesting_level]); $block_nesting_level--; $code_lines[$i] = '} // END ' . $m[1]; } else { // We have an ordinary line of code. if (!$do_not_echo) { $code_lines[$i] = 'echo \'' . $code_lines[$i] . '\' . "\\n";'; } else { $code_lines[$i] = '$' . $retvar . '.= \'' . $code_lines[$i] . '\' . "\\n";'; } } } // Bring it back into a single string of lines of code. $code = implode("\n", $code_lines); return $code ; } /** * Generates a reference to the given variable inside the given (possibly nested) * block namespace. This is a string of the form: * ' . $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . ' * It's ready to be inserted into an "echo" line in one of the templates. * NOTE: expects a trailing "." on the namespace. */ function generate_block_varref($namespace, $varname) { // Strip the trailing period. $namespace = substr($namespace, 0, strlen($namespace) - 1); // Get a reference to the data block for this namespace. $varref = $this->generate_block_data_ref($namespace, true); // Prepend the necessary code to stick this in an echo line. // Append the variable reference. $varref .= '[\'' . $varname . '\']'; $varref = '\' . ( ( isset(' . $varref . ') ) ? ' . $varref . ' : \'\' ) . \''; return $varref; } /** * Generates a reference to the array of data values for the given * (possibly nested) block namespace. This is a string of the form: * $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN'] * * If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above. * NOTE: does not expect a trailing "." on the blockname. */ function generate_block_data_ref($blockname, $include_last_iterator) { // Get an array of the blocks involved. $blocks = explode(".", $blockname); $blockcount = sizeof($blocks) - 1; $varref = '$this->_tpldata'; // Build up the string with everything but the last child. for ($i = 0; $i < $blockcount; $i++) { $varref .= '[\'' . $blocks[$i] . '.\'][$_' . $blocks[$i] . '_i]'; } // Add the block reference for the last child. $varref .= '[\'' . $blocks[$blockcount] . '.\']'; // Add the iterator for the last child if requried. if ($include_last_iterator) { $varref .= '[$_' . $blocks[$blockcount] . '_i]'; } return $varref; } } ?>

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0110.00718.31
8.3.50.0070.00717.95
8.3.40.0060.00919.02
8.3.30.0110.00418.83
8.3.20.0050.00520.29
8.3.10.0030.00521.94
8.3.00.0040.00419.25
8.2.180.0040.01518.01
8.2.170.0120.00922.96
8.2.160.0040.01622.11
8.2.150.0030.00524.18
8.2.140.0060.00324.66
8.2.130.0040.00426.16
8.2.120.0000.00819.83
8.2.110.0090.00920.64
8.2.100.0030.00917.83
8.2.90.0060.00319.22
8.2.80.0080.00017.97
8.2.70.0000.01117.63
8.2.60.0040.00418.16
8.2.50.0000.00818.10
8.2.40.0050.00320.63
8.2.30.0030.00619.23
8.2.20.0000.00818.16
8.2.10.0080.00018.11
8.2.00.0000.00718.28
8.1.280.0070.00725.92
8.1.270.0030.00622.15
8.1.260.0030.00626.35
8.1.250.0040.00428.09
8.1.240.0100.00021.03
8.1.230.0090.00317.70
8.1.220.0000.00817.74
8.1.210.0000.00818.77
8.1.200.0000.00917.36
8.1.190.0000.00917.35
8.1.180.0040.00418.10
8.1.170.0040.00418.70
8.1.160.0040.00419.00
8.1.150.0040.00418.98
8.1.140.0000.00817.71
8.1.130.0000.00719.15
8.1.120.0070.00017.45
8.1.110.0050.00317.55
8.1.100.0000.00817.54
8.1.90.0000.00817.60
8.1.80.0000.01117.45
8.1.70.0000.00717.45
8.1.60.0000.00817.58
8.1.50.0040.00417.63
8.1.40.0030.00617.51
8.1.30.0080.00017.61
8.1.20.0030.00517.64
8.1.10.0030.01017.57
8.1.00.0030.00717.41
8.0.300.0080.00018.77
8.0.290.0040.00416.88
8.0.280.0040.00418.49
8.0.270.0000.00717.41
8.0.260.0000.00718.56
8.0.250.0000.00717.01
8.0.240.0040.00416.94
8.0.230.0040.00416.99
8.0.220.0070.00216.99
8.0.210.0050.00216.93
8.0.200.0000.00716.93
8.0.190.0090.00016.91
8.0.180.0050.00316.92
8.0.170.0060.00316.85
8.0.160.0040.00416.94
8.0.150.0060.00317.00
8.0.140.0030.00616.93
8.0.130.0030.00313.27
8.0.120.0040.00416.84
8.0.110.0040.00417.01
8.0.100.0050.00316.88
8.0.90.0070.00016.93
8.0.80.0060.01616.95
8.0.70.0040.00416.87
8.0.60.0000.00817.01
8.0.50.0000.00816.96
8.0.30.0090.00916.97
8.0.20.0100.00917.41
8.0.10.0000.00817.09
8.0.00.0110.00816.86
7.4.330.0000.00515.55
7.4.320.0030.00316.49
7.4.300.0040.00416.57
7.4.290.0070.00016.57
7.4.280.0040.00416.59
7.4.270.0080.00016.55
7.4.260.0080.00016.55
7.4.250.0030.00616.38
7.4.240.0000.00716.55
7.4.230.0040.00416.66
7.4.220.0050.00316.67
7.4.210.0070.01016.65
7.4.200.0000.00816.63
7.4.160.0110.00816.63
7.4.150.0120.00617.40
7.4.140.0120.00817.86
7.4.130.0130.00416.66
7.4.120.0120.00816.65
7.4.110.0030.01316.70
7.4.100.0090.00916.30
7.4.90.0130.00516.50
7.4.80.0100.01419.39
7.4.70.0190.00416.68
7.4.60.0090.00816.50
7.4.50.0030.01316.48
7.4.40.0030.01516.66
7.4.00.0000.01415.12
7.3.330.0000.00713.39
7.3.320.0000.00613.34
7.3.310.0040.00416.29
7.3.300.0000.00716.32
7.3.290.0030.00316.20
7.3.280.0060.01616.46
7.3.270.0090.01417.40
7.3.260.0090.01116.56
7.3.250.0140.00616.52
7.3.240.0090.01116.38
7.3.230.0080.00816.54
7.3.210.0090.00916.67
7.3.200.0120.01216.37
7.3.190.0060.01116.48
7.3.180.0060.01416.29
7.3.170.0100.00716.68
7.3.160.0070.01116.57
7.2.330.0110.00716.36
7.2.320.0090.00916.49
7.2.310.0070.01016.76
7.2.300.0060.01216.56
7.2.290.0100.00716.31
7.2.110.0160.00516.52
7.2.60.0000.01316.80
7.2.10.0020.01219.65
7.2.00.0070.00719.89
7.1.200.0070.00315.60
7.1.130.0090.00318.61
7.1.120.0000.01518.54
7.1.110.0030.00918.05
7.1.100.0040.00818.13
7.1.90.0070.00717.64
7.1.80.0120.00318.17
7.1.70.0070.01216.93
7.1.60.0190.01034.92
7.1.50.0160.01334.63
7.1.40.0190.00934.21
7.1.30.0130.01734.26
7.1.20.0180.01134.39
7.1.10.0060.00616.30
7.1.00.0080.00816.23

preferences:
56.14 ms | 400 KiB | 5 Q