3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TemplateString { // Usage: // $t = new TemplateString("Your name is {{name}}"); // $t->name = "John"; // echo $t; // Your name is John private $template; private $parameters = []; public function __construct( $template, array $defaultparams = null) { $this->template = $template; if( $defaultparams) $this->parameters = $defaultparams; } public function __set($k,$v) { $this->parameters[$k] = $v; } public function __get($k) { if( array_key_exists($k,$this->parameters)) { return $this->parameters[$k]; } return null; } public function __toString() { $words = $this->parameters; return preg_replace_callback("/\{\{(\w+)\}\}/",function($m) use ($words) { if( array_key_exists($m[1],$words)) return $words[$m[1]]; return $m[0]; },$this->template); } } $t = new TemplateString("Your name is {{name}}\n"); $t->name = "John"; echo $t; $t->name = "Bill"; echo $t; // alternative: $t2 = clone $t; $t2->name = "Sam"; echo $t.$t2;
Output for git.master, git.master_jit, rfc.property-hooks
Your name is John Your name is Bill Your name is Bill Your name is Sam

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
148.65 ms | 405 KiB | 5 Q