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, int $length = null) => \func_num_args() === 3 ? substr($string, $start, $length) : 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->substr)("search phrase", 6, 2)); $temp->setMultiByte(); var_dump(mb_substr("search phrase", 6), ($temp->substr)("search phrase", 6));
Output for 7.2.29 - 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/Wmqru 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(2) " p" string(7) " phrase" string(7) " phrase"
Output for 8.4.1 - 8.4.26, 8.5.0 - 8.5.11
Deprecated: {closure:StringFunctionMapper::setSingleByte():11}(): Implicitly marking parameter $length as nullable is deprecated, the explicit nullable type must be used instead in /in/Wmqru on line 11 Deprecated: {closure:StringFunctionMapper::setMultiByte():19}(): Implicitly marking parameter $length as nullable is deprecated, the explicit nullable type must be used instead in /in/Wmqru on line 19 string(7) " phrase" string(7) " phrase" string(2) " p" string(7) " phrase" string(7) " phrase"

preferences:
31.56 ms | 1144 KiB | 11 Q