3v4l.org

run code in 500+ PHP versions simultaneously
<?php trait StringFunctionMapper { public Closure $substr; protected ?string $encoding = null; public function setSingleByte(): void { $this->encoding = null; $this->substr = fn (string $string, int $start) => substr($string, $start); } public function setMultiByte(string $encoding = 'UTF-8'): void { $this->encoding = $encoding; $this->substr = fn (string $string, int $start, int $length = null) => mb_substr($string, $start, $length, $this->encoding); } } $temp = new class () { use StringFunctionMapper; }; $temp->setSingleByte(); var_dump(substr("search phrase", 6), ($temp->substr)("search phrase", 6)); $temp->setMultiByte(); var_dump(mb_substr("search phrase", 6), ($temp->substr)("search phrase", 6));
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'Closure' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/j1qXn on line 5
Process exited with code 255.
Output for 7.4.4 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.34, 8.3.0 - 8.3.35
string(7) " phrase" string(7) " phrase" string(7) " phrase" string(7) " phrase"
Output for 8.4.1 - 8.4.26, 8.5.0 - 8.5.11
Deprecated: {closure:StringFunctionMapper::setMultiByte():17}(): Implicitly marking parameter $length as nullable is deprecated, the explicit nullable type must be used instead in /in/j1qXn on line 17 string(7) " phrase" string(7) " phrase" string(7) " phrase" string(7) " phrase"

preferences:
33.06 ms | 1144 KiB | 11 Q