3v4l.org

run code in 300+ PHP versions simultaneously
<?php class DeterministicRandom { protected $counter; public function __construct(string $seed = '', int $counter = 0) { $this->seed('set', $seed); $this->counter = 0; } private function seed(string $action = 'get', string $data = '') { static $seed = null; if ($action === 'set') { $seed = $data; } elseif ($action === 'get') { return $data; } else { throw new \Error( 'Unknown action' ); } } public function getBytes(int $numBytes): string { return \openssl_encrypt( \str_repeat("\0", $numBytes), 'aes-128-ctr', $this->seed('get'), OPENSSL_RAW_DATA, $this->getNonce($numBytes) ); } public function getInt(int $min, int $max): int { /** * @todo */ return 0; } protected function getNonce(int $increment = 0): string { $nonce = ''; $ctr = $this->counter; while ($ctr > 0) { $nonce = \chr($ctr & 0xFF) . $nonce; $ctr >>= 8; } $this->counter += $increment; return \str_pad($nonce, 16, "\0", STR_PAD_LEFT); } } $seed = str_repeat("\x80", 16); $rnd1 = new DeterministicRandom($seed); $rnd2 = new DeterministicRandom($seed); $out1 = $rnd1->getBytes(16); $out2 = $rnd1->getBytes(16); $out3 = $rnd2->getBytes(32); var_dump([ bin2hex($out1), bin2hex($out2), bin2hex($out3) ]);
Output for 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 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: Uncaught Error: Call to undefined function openssl_encrypt() in /in/Db90F:29 Stack trace: #0 /in/Db90F(64): DeterministicRandom->getBytes(16) #1 {main} thrown in /in/Db90F on line 29
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0
array(3) { [0]=> string(32) "66e94bd4ef8a2c3b884cfa59ca342b2e" [1]=> string(32) "58b2431bc0bede02550f40238969ec78" [2]=> string(64) "66e94bd4ef8a2c3b884cfa59ca342b2e58e2fccefa7e3061367f1d57a4e7455a" }
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Fatal error: Default value for parameters with a class type hint can only be NULL in /in/Db90F on line 7
Process exited with code 255.

preferences:
145.8 ms | 402 KiB | 183 Q