3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $allowedweapons = ['Scissors', 'Paper', 'Rock', 'Lizard', 'Spock']; $rules = [ 'scissorspaper' => 'cuts', 'paperrock' => 'covers', 'rocklizard' => 'crushes', 'lizardspock' => 'poisons', 'spockscissors' => 'smashes', 'scissorslizard' => 'decapitates', 'lizardpaper' => 'eats', 'paperspock' => 'disproves', 'spockrock' => 'vaporizes', 'rockscissors' => 'crushes' ]; class Game { protected $rules; public function __construct($rules) { $this->rules = $rules; } public function run($weaponP1, $weaponP2) { if ($weaponP1 === $weaponP2) { echo "It is a tie"; return; } $result = $this->checkVerbs($weaponP1, $weaponP2); $winner = 'Player 1'; if (!$result) { $result = $this->checkVerbs($weaponP2, $weaponP1); $winner = 'Player 2'; } echo "The winner is: $winner\n"; echo "Because of: " . $result; } /** * @param $weaponP1 * @param $weaponP2 * @return bool|string */ protected function checkVerbs($weaponP1, $weaponP2) { $key = strtolower($weaponP1 . $weaponP2); if (!array_key_exists($key, $this->rules)) { return false; } return $weaponP1 . ' ' . $this->rules[$key] . ' ' . $weaponP2; } } (new Game($rules))->run('Scissors', 'Paper');
Output for 5.4.10 - 7.3.0rc4
The winner is: Player 1 Because of: Scissors cuts Paper
Output for 5.3.20 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/oom3P on line 2
Process exited with code 255.