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 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
Deprecated: TemplateString::__construct(): Implicitly marking parameter $defaultparams as nullable is deprecated, the explicit nullable type must be used instead in /in/vNhYl on line 11 Your name is John Your name is Bill Your name is Bill Your name is Sam
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28
Your name is John Your name is Bill Your name is Bill Your name is Sam

preferences:
192.03 ms | 411 KiB | 5 Q