3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $psr4 = array( 'my\\first\\ns\\prefix\\' => 0, 'my\\second\\ns\\prefix\\' => 0, 'my\\third\\ns\prefix' => 0, ); foreach ($psr4 as $prefix => &$v) { $v = strlen($prefix); } $classes = array( 'my\\third\\ns\\prefix\\subns\\class', 'another\\ns\\prefix\\subns\\class', ); foreach ($classes as $class) { foreach ($psr4 as $prefix => $length) { if (0 === strpos($class, $prefix)) { echo "$class -> $prefix\n"; break; } } } echo "-----------------------------\n"; foreach ($classes as $class) { foreach ($psr4 as $prefix => $length) { if (strncmp($class, $prefix, $length)) { echo "$class -> $prefix\n"; break; } } }
based on lCK3I
Output for 5.0.0 - 7.2.0
my\third\ns\prefix\subns\class -> my\third\ns\prefix ----------------------------- my\third\ns\prefix\subns\class -> my\first\ns\prefix\ another\ns\prefix\subns\class -> my\first\ns\prefix\
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /in/R1559 on line 8
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected '&', expecting T_VARIABLE or '$' in /in/R1559 on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /in/R1559 on line 8
Process exited with code 255.