3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Template { // Path var $path; // Handles var $handles = array(); // Vars var $v = array(); // Set templates path function, use relative, for explample: path('./html'); function SetPath ($path) { if (is_dir($path)) { $this->path = $path . '/'; return true; } else { die ('Template: path is not a relative path to an existent directory'); return false; } } // Loadfile Function; function LoadFile ($h) { if (empty($this->handles[$h])) { die ('Template: no filename specified for "'.$h.'"'); } $str = implode('', file($this->path.$this->handles[$h])); if (empty($str)) { die ('Template: template file ("'.$this->handles[$h].'") is empty'); } return $str; } // Set vars function, for exaple: setvars (array('onevar'=>'onevalue')); function SetVars ($arr) { while (list ($name, $value) = each ($arr)) { $this->v[0][$name] = $value; } return true; } // Set blockvars, for example: setbvars ('block.subblock', array('a'=>'b'); function SetBlockVars ($block, $arr) { if (strstr($block, '.')) { $all = explode ('.', $block); $count = count ($all) -1; $str = '$this->v[1]'; for ($i = 0; $i < $count; $i++) { $str .= '[\''.$all[$i].'\']'; eval ('$last = count ('.$str.') - 1;'); $str .= '['.$last.']'; } $str .= '[\''.$all[$count].'\'][] = $arr;'; eval ($str); } else { $this->v[1][$block][] = $arr; } return true; } // Set handles ( handle (array('name'=>'filename')) function Assign ($str) { $this->handles[$str] = $str.'.tpl'; return true; } // Compile code function compile ($code) { $code = str_replace ('\\', '\\\\', $code); $code = str_replace ('\'', '\\\'', $code); preg_match_all ('#\{([a-z0-9\-_.]+)\.([a-z0-9\-_]+)\}#is', $code, $bvars); $bvarscount = count ($bvars[0]); for ($i = 0; $i < $bvarscount; $i++) { $all = explode ('.', $bvars[1][$i]); $count = count ($all); $str = '$this->v[1]'; for ($ii = 0; $ii < $count; $ii++) { $str .= '[\''.$all[$ii].'\'][$_repeat_'.$all[$ii].']'; } $str .= '[\''.$bvars[2][$i].'\']'; $str = '\' . ( isset ('.$str.') ? '.$str.' : \'\' ) . \''; $replace[] = $str; $search[] = $bvars[0][$i]; } $code = str_replace ($search, $replace, $code); $code = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '\' . ( ( isset($this->v[0][\'\1\']) ) ? $this->v[0][\'\1\'] : \'\' ) . \'', $code); $line = explode ("\n", $code); $lines = count ($line); $names = array (); for ($i = 0; $i < $lines; $i++) { $line[$i] = chop ($line[$i]); if (preg_match('#<!-- BEGIN (.*?) -->#', $line[$i], $temp)) { $blockname = $temp[1]; if (strstr ($blockname, '.')) { $blockarr = explode ('.', $blockname); $blocklen = count ($blockarr) -1; $ref = '$this->v[1]'; for ($x = 0; $x < $blocklen; $x++) { $ref .= '[\''.$blockarr[$x].'\'][$_repeat_'.$blockarr[$x].']'; } $ref .= '[\''.$blockarr[$x].'\']'; $line[$i] = '$_count_'.$blockarr[$x].' = ( isset ('.$ref.') ? count ('.$ref.') : 0);'; $line[$i] .= "\n".'for ($_repeat_'.$blockarr[$x].' = 0; $_repeat_'.$blockarr[$x].' < $_count_'.$blockarr[$x].'; $_repeat_'.$blockarr[$x].'++)'; $line[$i] .= "\n".'{ // BEGIN '.$blockname; } else { $ref = '$this->v[1][\''.$blockname.'\']'; $line[$i] = '$_count_'.$blockname.' = ( isset ('.$ref.') ? count ('.$ref.') : 0);'; $line[$i] .= "\n".'for ($_repeat_'.$blockname.' = 0; $_repeat_'.$blockname.' < $_count_'.$blockname.'; $_repeat_'.$blockname.'++)'; $line[$i] .= "\n".'{ // BEGIN '.$blockname; } } else if (preg_match('#<!-- END (.*?) -->#', $line[$i], $temp)) { $line[$i] = '} // END '.$temp[1]; } else { $line[$i] = 'print \''.$line[$i].'\'."\n";'; } } $code = implode ("\n", $line); return $code; } function Make ($handle) { $template = $this->LoadFile ($handle); $code = $this->compile ($template); eval ($code); /* highlight_string ("<?php\n".$code."\n?>"); */ return true; } } $tpl = new Template; $tpl->SetPath ('html/'); ?>
Output for 7.2.29 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: is_dir(): open_basedir restriction in effect. File(html/) is not within the allowed path(s): (/tmp:/in:/etc) in /in/ram1Z on line 13 Template: path is not a relative path to an existent directory
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: is_dir(): open_basedir restriction in effect. File(html/) is not within the allowed path(s): (/tmp:/in:/etc) in /in/ram1Z on line 13 Template: path is not a relative path to an existent directory
Output for 7.1.10, 7.2.0, 7.3.32 - 7.3.33, 7.4.26, 7.4.33, 8.0.13
Template: path is not a relative path to an existent directory
Output for 7.1.20, 7.2.6
Warning: is_dir(): open_basedir restriction in effect. File(html/) is not within the allowed path(s): (/tmp:/in) in /in/ram1Z on line 13 Template: path is not a relative path to an existent directory
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.7
Warning: is_dir(): open_basedir restriction in effect. File(html/) is not within the allowed path(s): (/tmp/) in /in/ram1Z on line 13 Template: path is not a relative path to an existent directory

preferences:
147.02 ms | 402 KiB | 173 Q