3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function is_prime($n) { if ($n <= 1) { return false; } elseif ($n <= 3) { return true; } elseif (($n % 2 == 0) || ($n % 3 == 0)) { return false; } $i = 5; while ($i * $i <= $n) { if (($n % $i == 0) || ($n % ($i + 2) == 0)) { return false; $i = $i + 6; } } return true; } function prime_gen() { for($x=0; $x< PHP_INT_MAX; $x++) { if(is_prime($x)){ yield $x; } } } function nth_prime($n) { $nth = 0; foreach (prime_gen() as $prime) { if (++$nth >= $n) { break; } } return $prime; } echo nth_prime(9);
Output for 5.5.0 - 7.3.0beta1
23