3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Version { const STAGE_ALPHA = 1; const STAGE_BETA = 2; const STAGE_RC = 3; const STAGE_RELEASE = 4; public $major; public $minor; public $release; public $stageName; public $stageNo; private $stageMap = [ 'alpha' => self::STAGE_ALPHA, 'beta' => self::STAGE_BETA, 'rc' => self::STAGE_RC, ]; /** * @param string $version * @throws \Exception */ public function __construct($version) { static $tagNameExpr = '# ^ v? (?P<major>[0-9]+) \. (?P<minor>[0-9]+) (?:\.(?P<release>[0-9]+))? (?:- (?P<stage>alpha|beta|rc) (?:(?P<stageno>\d+))? )? $ #ix'; if (!preg_match($tagNameExpr, trim(strtolower($version)), $match)) { throw new \Exception('Invalid version spec string'); } var_dump($match); $this->major = (int)$match['major']; $this->minor = (int)$match['minor']; $this->release = isset($match['release']) ? (int)$match['release'] : null; $this->stageName = isset($match['stage']) ? $match['stage'] : ''; $this->stageNo = isset($match['stageno']) ? (int)$match['stageno'] : null; } /** * @return string */ public function __toString() { return "$this->major.$this->minor." . ($this->release ?: 0) . ($this->stageNo ? '-' . $this->stageName . $this->stageNo : ''); } /** * @param string[] $stages * @return bool */ public function isValidNextStage(array $stages) { $latestStageType = $latestStagePoint = 0; foreach ($stages as $stage) { if ($stage === '') { // a release already exists return false; } if (preg_match('#^(alpha|beta|rc)(\d+)$#', strtolower($stage), $match)) { if ($this->stageMap[$match[1]] > $latestStageType) { $latestStageType = $this->stageMap[$match[1]]; $latestStagePoint = 0; } else if ($this->stageMap[$match[1]] === $latestStageType && $match[2] > $latestStagePoint) { $latestStagePoint = (int)$match[2]; } } } if (!$this->stageName) { // this is a release and one does not yet exist return true; } if (!isset($this->stageMap[$this->stageName])) { // invalid new stage return false; } if ($this->stageMap[$this->stageName] < $latestStageType) { // new stage lower than latest return false; } if ($this->stageMap[$this->stageName] > $latestStageType) { // new stage greater than latest, number must be 1 if (!isset($this->stageNo)) { $this->stageNo = 1; return true; } return $this->stageNo === 1; } if (!isset($this->stageNo)) { $this->stageNo = $latestStagePoint + 1; return true; } return $this->stageNo === $latestStagePoint + 1; } } var_dump(new Version('1.12-beta'));
based on pvldm
Output for 5.4.0 - 5.6.28, hhvm-3.12.0, 7.0.0 - 7.1.0
array(9) { [0]=> string(9) "1.12-beta" ["major"]=> string(1) "1" [1]=> string(1) "1" ["minor"]=> string(2) "12" [2]=> string(2) "12" ["release"]=> string(0) "" [3]=> string(0) "" ["stage"]=> string(4) "beta" [4]=> string(4) "beta" } object(Version)#1 (6) { ["major"]=> int(1) ["minor"]=> int(12) ["release"]=> int(0) ["stageName"]=> string(4) "beta" ["stageNo"]=> NULL ["stageMap":"Version":private]=> array(3) { ["alpha"]=> int(1) ["beta"]=> int(2) ["rc"]=> int(3) } }
Output for hhvm-3.10.0

Process exited with code 153.
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/32XFH on line 16
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/32XFH on line 16
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/32XFH on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/32XFH on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/32XFH on line 5
Process exited with code 255.