3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Fu { final class Exception extends \Exception { } final class Token { public $tokenType; public $content; public $contentLength; final public function __construct($tokenType, $content, $contentLength) { $this->tokenType = $tokenType; $this->content = $content; $this->contentLength = $contentLength; } } final class Parser { const FU_TOKENTYPE_WHITESPACE = 0x00000001; const FU_TOKENTYPE_DATATYPE_CHAR = 0x00000002; const FU_TOKENTYPE_VARIABLE = 0x00000003; const FU_TOKENTYPE_SEMICOLON = 0x00000004; const FU_TOKENTYPE_OPERATOR_ASSIGN = 0x00000005; const FU_TOKENTYPE_LITERAL_INTEGER = 0x00000006; const FU_TOKENTYPE_OPERATOR_ADD = 0x00000007; //const FU_SCOPE_ROOT = 0x00000001; //const FU_SCOPE_DECLARATION_VARIABLE = 0x00000002; //const FU_SCOPE_ final public static function parse($source) { $tokens = []; while ($source !== '') { $token = NULL; switch (substr($source, 0, 1)) { case ' ': case "\t": case "\n": $i = 1; while ($source{$i} === ' ' || $source{$i} === "\t" || $source{$i} === "\n") { ++$i; } $tokens[] = new Token(self::FU_TOKENTYPE_WHITESPACE, substr($source, 0, $i), $i); $source = substr($source, $i); break; case 'c': if (substr($source, 0, 4) === 'char') { $tokens[] = new Token(self::FU_TOKENTYPE_DATATYPE_CHAR, 'char', 4); $source = substr($source, 4); } else { throw new Exception('Parse error at line x, position x; unidentified symbol'); } break; case '$': $i = 1; while (in_array($source{$i}, ['_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])) { ++$i; } $tokens[] = new Token(self::FU_TOKENTYPE_VARIABLE, substr($source, 0, $i), $i); $source = substr($source, $i); break; case ';': $tokens[] = new Token(self::FU_TOKENTYPE_SEMICOLON, ';', 1); $source = substr($source, 1); break; case '=': $tokens[] = new Token(self::FU_TOKENTYPE_OPERATOR_ASSIGN, '=', 1); $source = substr($source, 1); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': $i = 1; while (in_array($source{$i}, ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])) { ++$i; } // TODO: float parsing $tokens[] = new Token(self::FU_TOKENTYPE_LITERAL_INTEGER, substr($source, 0, $i), $i); $source = substr($source, $i); break; case '+': $tokens[] = new Token(self::FU_TOKENTYPE_OPERATOR_ADD, '+', 1); $source = substr($source, 1); break; } } return $tokens; } } } namespace { var_dump(Fu\Parser::parse('char $x;$x=1+2;')); }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Array and string offset access syntax with curly braces is no longer supported in /in/olZR1 on line 36
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/olZR1 on line 36 Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/olZR1 on line 36 Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/olZR1 on line 36 Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/olZR1 on line 52 Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/olZR1 on line 77 array(10) { [0]=> object(Fu\Token)#1 (3) { ["tokenType"]=> int(2) ["content"]=> string(4) "char" ["contentLength"]=> int(4) } [1]=> object(Fu\Token)#2 (3) { ["tokenType"]=> int(1) ["content"]=> string(1) " " ["contentLength"]=> int(1) } [2]=> object(Fu\Token)#3 (3) { ["tokenType"]=> int(3) ["content"]=> string(2) "$x" ["contentLength"]=> int(2) } [3]=> object(Fu\Token)#4 (3) { ["tokenType"]=> int(4) ["content"]=> string(1) ";" ["contentLength"]=> int(1) } [4]=> object(Fu\Token)#5 (3) { ["tokenType"]=> int(3) ["content"]=> string(2) "$x" ["contentLength"]=> int(2) } [5]=> object(Fu\Token)#6 (3) { ["tokenType"]=> int(5) ["content"]=> string(1) "=" ["contentLength"]=> int(1) } [6]=> object(Fu\Token)#7 (3) { ["tokenType"]=> int(6) ["content"]=> string(1) "1" ["contentLength"]=> int(1) } [7]=> object(Fu\Token)#8 (3) { ["tokenType"]=> int(7) ["content"]=> string(1) "+" ["contentLength"]=> int(1) } [8]=> object(Fu\Token)#9 (3) { ["tokenType"]=> int(6) ["content"]=> string(1) "2" ["contentLength"]=> int(1) } [9]=> object(Fu\Token)#10 (3) { ["tokenType"]=> int(4) ["content"]=> string(1) ";" ["contentLength"]=> int(1) } }
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33
array(10) { [0]=> object(Fu\Token)#1 (3) { ["tokenType"]=> int(2) ["content"]=> string(4) "char" ["contentLength"]=> int(4) } [1]=> object(Fu\Token)#2 (3) { ["tokenType"]=> int(1) ["content"]=> string(1) " " ["contentLength"]=> int(1) } [2]=> object(Fu\Token)#3 (3) { ["tokenType"]=> int(3) ["content"]=> string(2) "$x" ["contentLength"]=> int(2) } [3]=> object(Fu\Token)#4 (3) { ["tokenType"]=> int(4) ["content"]=> string(1) ";" ["contentLength"]=> int(1) } [4]=> object(Fu\Token)#5 (3) { ["tokenType"]=> int(3) ["content"]=> string(2) "$x" ["contentLength"]=> int(2) } [5]=> object(Fu\Token)#6 (3) { ["tokenType"]=> int(5) ["content"]=> string(1) "=" ["contentLength"]=> int(1) } [6]=> object(Fu\Token)#7 (3) { ["tokenType"]=> int(6) ["content"]=> string(1) "1" ["contentLength"]=> int(1) } [7]=> object(Fu\Token)#8 (3) { ["tokenType"]=> int(7) ["content"]=> string(1) "+" ["contentLength"]=> int(1) } [8]=> object(Fu\Token)#9 (3) { ["tokenType"]=> int(6) ["content"]=> string(1) "2" ["contentLength"]=> int(1) } [9]=> object(Fu\Token)#10 (3) { ["tokenType"]=> int(4) ["content"]=> string(1) ";" ["contentLength"]=> int(1) } }
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 2097152 bytes) in /in/olZR1 on line 81
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/olZR1 on line 28
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/olZR1 on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/olZR1 on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/olZR1 on line 3
Process exited with code 255.

preferences:
238.25 ms | 401 KiB | 356 Q