3v4l.org

run code in 300+ PHP versions simultaneously
<?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)) { return $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; } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 29
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 29
Branch analysis from position: 25
Branch analysis from position: 29
filename:       /in/J3B2u
function name:  (null)
number of ops:  33
compiled vars:  !0 = $primePalindromeFinder
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   NEW                                              $1      'BetterPrimePalindromeFinder'
          1        SEND_VAL_EX                                              1000
          2        SEND_VAL_EX                                              1
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $1
    4     5        INIT_FCALL                                               'var_dump'
          6        INIT_METHOD_CALL                                         !0, 'hasPrimePalindrome'
          7        DO_FCALL                                      0  $4      
          8      > JMPZ                                                     $4, ->13
          9    >   INIT_METHOD_CALL                                         !0, 'find'
         10        DO_FCALL                                      0  $5      
         11        QM_ASSIGN                                        ~6      $5
         12      > JMP                                                      ->14
         13    >   QM_ASSIGN                                        ~6      'NOPE'
         14    >   SEND_VAL                                                 ~6
         15        DO_ICALL                                                 
    6    16        NEW                                              $8      'BetterPrimePalindromeFinder'
         17        SEND_VAL_EX                                              500
         18        SEND_VAL_EX                                              400
         19        DO_FCALL                                      0          
         20        ASSIGN                                                   !0, $8
    7    21        INIT_FCALL                                               'var_dump'
         22        INIT_METHOD_CALL                                         !0, 'hasPrimePalindrome'
         23        DO_FCALL                                      0  $11     
         24      > JMPZ                                                     $11, ->29
         25    >   INIT_METHOD_CALL                                         !0, 'find'
         26        DO_FCALL                                      0  $12     
         27        QM_ASSIGN                                        ~13     $12
         28      > JMP                                                      ->30
         29    >   QM_ASSIGN                                        ~13     'NOPE'
         30    >   SEND_VAL                                                 ~13
         31        DO_ICALL                                                 
   68    32      > RETURN                                                   1

Class BetterPrimePalindromeFinder:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 9
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 9
Branch analysis from position: 25
Branch analysis from position: 9
Branch analysis from position: 17
filename:       /in/J3B2u
function name:  __construct
number of ops:  26
compiled vars:  !0 = $start, !1 = $end, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        ASSIGN_OBJ                                               'start'
          3        OP_DATA                                                  !0
   19     4        ASSIGN_OBJ                                               'end'
          5        OP_DATA                                                  !1
   21     6        FETCH_OBJ_R                                      ~5      'start'
          7        ASSIGN                                                   !2, ~5
          8      > JMP                                                      ->22
   22     9    >   INIT_METHOD_CALL                                         'isPalindrome'
         10        SEND_VAR_EX                                              !2
         11        DO_FCALL                                      0  $7      
         12      > JMPZ_EX                                          ~8      $7, ->17
         13    >   INIT_METHOD_CALL                                         'isPrime'
         14        SEND_VAR_EX                                              !2
         15        DO_FCALL                                      0  $9      
         16        BOOL                                             ~8      $9
         17    > > JMPZ                                                     ~8, ->21
   23    18    >   ASSIGN_OBJ                                       ~10     'prime_palindrome'
         19        OP_DATA                                                  !2
         20      > RETURN                                                   ~10
   21    21    >   PRE_DEC                                                  !2
         22    >   FETCH_OBJ_R                                      ~12     'end'
         23        IS_SMALLER_OR_EQUAL                                      ~12, !2
         24      > JMPNZ                                                    ~13, ->9
   26    25    > > RETURN                                                   null

End of function __construct

Function find:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/J3B2u
function name:  find
number of ops:  16
compiled vars:  !0 = $start, !1 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   INIT_METHOD_CALL                                         'hasPrimePalindrome'
          1        DO_FCALL                                      0  $2      
          2      > JMPZ                                                     $2, ->6
   30     3    >   FETCH_OBJ_R                                      ~3      'prime_palindrome'
          4        VERIFY_RETURN_TYPE                                       ~3
          5      > RETURN                                                   ~3
   32     6    >   NEW                                              $4      'Exception'
          7        ROPE_INIT                                     4  ~6      'No+Prime+Palindrome+found+between+'
          8        ROPE_ADD                                      1  ~6      ~6, !0
          9        ROPE_ADD                                      2  ~6      ~6, '+and+'
         10        ROPE_END                                      3  ~5      ~6, !1
         11        SEND_VAL_EX                                              ~5
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $4
   33    14*       VERIFY_RETURN_TYPE                                       
         15*     > RETURN                                                   null

End of function find

Function hasprimepalindrome:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/J3B2u
function name:  hasPrimePalindrome
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   FETCH_OBJ_R                                      ~0      'prime_palindrome'
          1        TYPE_CHECK                                  1020  ~1      ~0
          2        VERIFY_RETURN_TYPE                                       ~1
          3      > RETURN                                                   ~1
   37     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function hasprimepalindrome

Function ispalindrome:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 5
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 22
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 5
Branch analysis from position: 30
Branch analysis from position: 5
filename:       /in/J3B2u
function name:  isPalindrome
number of ops:  33
compiled vars:  !0 = $number, !1 = $i, !2 = $from_start, !3 = $from_end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
   40     1        CAST                                          6  ~4      !0
          2        ASSIGN                                                   !0, ~4
   41     3        ASSIGN                                                   !1, 1
          4      > JMP                                                      ->23
   42     5    >   INIT_FCALL                                               'substr'
          6        SEND_VAR                                                 !0
          7        SUB                                              ~7      !1, 1
          8        SEND_VAL                                                 ~7
          9        SEND_VAL                                                 1
         10        DO_ICALL                                         $8      
         11        ASSIGN                                                   !2, $8
   43    12        INIT_FCALL                                               'substr'
         13        SEND_VAR                                                 !0
         14        MUL                                              ~10     !1, -1
         15        SEND_VAL                                                 ~10
         16        SEND_VAL                                                 1
         17        DO_ICALL                                         $11     
         18        ASSIGN                                                   !3, $11
   44    19        IS_NOT_IDENTICAL                                         !2, !3
         20      > JMPZ                                                     ~13, ->22
   45    21    > > RETURN                                                   <false>
   41    22    >   PRE_INC                                                  !1
         23    >   INIT_FCALL                                               'floor'
         24        STRLEN                                           ~15     !0
         25        DIV                                              ~16     ~15, 2
         26        SEND_VAL                                                 ~16
         27        DO_ICALL                                         $17     
         28        IS_SMALLER_OR_EQUAL                                      !1, $17
         29      > JMPNZ                                                    ~18, ->5
   48    30    > > RETURN                                                   <true>
   49    31*       VERIFY_RETURN_TYPE                                       
         32*     > RETURN                                                   null

End of function ispalindrome

Function isprime:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 11
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 13
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 17
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 13
Branch analysis from position: 27
Branch analysis from position: 13
filename:       /in/J3B2u
function name:  isPrime
number of ops:  30
compiled vars:  !0 = $number, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
   52     1        IS_EQUAL                                                 !0, 1
          2      > JMPZ                                                     ~2, ->4
   53     3    > > RETURN                                                   <false>
   55     4    >   IS_EQUAL                                                 !0, 2
          5      > JMPZ                                                     ~3, ->7
   56     6    > > RETURN                                                   <true>
   58     7    >   MOD                                              ~4      !0, 2
          8        IS_EQUAL                                                 ~4, 0
          9      > JMPZ                                                     ~5, ->11
   59    10    > > RETURN                                                   <false>
   61    11    >   ASSIGN                                                   !1, 3
         12      > JMP                                                      ->19
   62    13    >   MOD                                              ~7      !0, !1
         14        IS_EQUAL                                                 ~7, 0
         15      > JMPZ                                                     ~8, ->17
   63    16    > > RETURN                                                   <false>
   61    17    >   ADD                                              ~9      !1, 2
         18        ASSIGN                                                   !1, ~9
         19    >   INIT_FCALL                                               'ceil'
         20        INIT_FCALL                                               'sqrt'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $11     
         23        SEND_VAR                                                 $11
         24        DO_ICALL                                         $12     
         25        IS_SMALLER_OR_EQUAL                                      !1, $12
         26      > JMPNZ                                                    ~13, ->13
   66    27    > > RETURN                                                   <true>
   67    28*       VERIFY_RETURN_TYPE                                       
         29*     > RETURN                                                   null

End of function isprime

End of class BetterPrimePalindromeFinder.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.17 ms | 1404 KiB | 23 Q