3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php declare(strict_types=1); var_dump(PrimePalindromeFinder::find(1000, 1)); var_dump(PrimePalindromeFinder::find(10, 1)); var_dump(PrimePalindromeFinder::find(500, 400)); var_dump(PrimePalindromeFinder::findStrict(1000, 1)); var_dump(PrimePalindromeFinder::findStrict(10, 1)); try { var_dump(PrimePalindromeFinder::findStrict(500, 400)); } catch(\Exception $e) { var_dump($e->getMessage()); } class PrimePalindromeFinder { public static function find(int $start, int $end) { for($i = $start; $i >= $end; $i--) { if(self::isPalindrome($i) && self::isPrime($i)) { return $i; } } return false; } public static function findStrict(int $start, int $end):int { for($i = $start; $i >= $end; $i--) { if(self::isPalindrome($i) && self::isPrime($i)) { return $i; } } throw new \Exception("No Prime Palindrome found between {$start} and {$end}"); } private static 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 static 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; } }
Output for 7.0.0 - 7.2.0
int(929) int(7) bool(false) int(929) int(7) string(45) "No Prime Palindrome found between 500 and 400"
Output for 5.3.19 - 5.6.28
Warning: Unsupported declare 'strict_types' in /in/1CVHB on line 2 Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/1CVHB on line 26
Process exited with code 255.