3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $primePalindromeFinder = new BetterPrimePalindromeFinder(1000, 1); var_dump($primePalindromeFinder->hasPrimePalindrome() ? $primePalindromeFinder->find() : 'NOPE'); $primePalindromeFinder = new BetterPrimePalindromeFinder(500, 400); var_dump($primePalindromeFinder->hasPrimePalindrome() ? $primePalindromeFinder->find() : 'NOPE'); // Exception #var_dump($primePalindromeFinder->find()); class BetterPrimePalindromeFinder { private $start; private $end; private $prime_palindrome; public function __construct(int $start, int $end) { $this->start = $start; $this->end = $end; for($i = $this->start; $i >= $this->end; $i--) { if($this->isPalindrome($i) && $this->isPrime($i)) { $this->prime_palindrome = $i; } } } public function find():int { if($this->hasPrimePalindrome()) { return $this->prime_palindrome; } throw new \Exception("No Prime Palindrome found between {$start} and {$end}"); } public function hasPrimePalindrome():bool { return $this->prime_palindrome !== null; } private function isPalindrome(int $number):bool { $number = strval($number); for($i = 1; $i <= floor(strlen($number) / 2); $i++) { $from_start = substr($number, $i - 1, 1); $from_end = substr($number, $i * -1, 1); if($from_start !== $from_end) { return false; } } return true; } private function isPrime(int $number):bool { if($number == 1) { return false; } if($number == 2) { return true; } if($number % 2 == 0) { return false; } for($i = 3; $i <= ceil(sqrt($number)); $i = $i + 2) { if($number % $i == 0) return false; } return true; } }
based on J3B2u
Output for 7.0.0 - 7.2.0
int(2) string(4) "NOPE"
Output for 5.3.19 - 5.6.28
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/97YPK on line 28
Process exited with code 255.