3v4l.org

run code in 300+ PHP versions simultaneously
<?php function isPalindrome(string &$str): bool { if ($str === '') { return false; // an empty string is not a palindrome } if (strlen($str) === 1) { return true; // odd length string is a palindrome, do not recurse } if ($str[0] !== $str[strlen($str) - 1]) { return false; // outermost characters are not identical, halt processing } $str = substr($str, 1, -1); // shed outermost charcters if ($str === '') { return true; // even length string is a palindrome, do not recurse } return (__FUNCTION__)($str); // traverse deeper and bubble outcome to top level } function sanitizePalindrome(&$str) { return preg_replace('/[^a-z]+/', '', strtolower($str)); } $tests = [ '', 'radar', 'f', 'neveroddoreven', 'foo', 'palindrome', 'Red rum, sir, is murder' ]; foreach ($tests as $test) { $test = sanitizePalindrome($test); // this is optional printf("%20s : %s\n", $test, json_encode(isPalindrome($test))); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 19
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 19
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/TiNa3
function name:  (null)
number of ops:  21
compiled vars:  !0 = $tests, !1 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   ASSIGN                                                   !0, <array>
   35     1      > FE_RESET_R                                       $3      !0, ->19
          2    > > FE_FETCH_R                                               $3, !1, ->19
   36     3    >   INIT_FCALL                                               'sanitizepalindrome'
          4        SEND_REF                                                 !1
          5        DO_FCALL                                      0  $4      
          6        ASSIGN                                                   !1, $4
   37     7        INIT_FCALL                                               'printf'
          8        SEND_VAL                                                 '%2520s+%3A+%25s%0A'
          9        SEND_VAR                                                 !1
         10        INIT_FCALL                                               'json_encode'
         11        INIT_FCALL                                               'ispalindrome'
         12        SEND_REF                                                 !1
         13        DO_FCALL                                      0  $6      
         14        SEND_VAR                                                 $6
         15        DO_ICALL                                         $7      
         16        SEND_VAR                                                 $7
         17        DO_ICALL                                                 
   35    18      > JMP                                                      ->2
         19    >   FE_FREE                                                  $3
   38    20      > RETURN                                                   1

Function ispalindrome:
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 = 7, Position 2 = 8
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 24
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TiNa3
function name:  isPalindrome
number of ops:  31
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        IS_IDENTICAL                                             !0, ''
          2      > JMPZ                                                     ~1, ->4
    6     3    > > RETURN                                                   <false>
    8     4    >   STRLEN                                           ~2      !0
          5        IS_IDENTICAL                                             ~2, 1
          6      > JMPZ                                                     ~3, ->8
    9     7    > > RETURN                                                   <true>
   11     8    >   FETCH_DIM_R                                      ~4      !0, 0
          9        STRLEN                                           ~5      !0
         10        SUB                                              ~6      ~5, 1
         11        FETCH_DIM_R                                      ~7      !0, ~6
         12        IS_NOT_IDENTICAL                                         ~4, ~7
         13      > JMPZ                                                     ~8, ->15
   12    14    > > RETURN                                                   <false>
   14    15    >   INIT_FCALL                                               'substr'
         16        SEND_VAR                                                 !0
         17        SEND_VAL                                                 1
         18        SEND_VAL                                                 -1
         19        DO_ICALL                                         $9      
         20        ASSIGN                                                   !0, $9
   15    21        IS_IDENTICAL                                             !0, ''
         22      > JMPZ                                                     ~11, ->24
   16    23    > > RETURN                                                   <true>
   18    24    >   INIT_FCALL_BY_NAME                                       'isPalindrome'
         25        SEND_VAR_EX                                              !0
         26        DO_FCALL                                      0  $12     
         27        VERIFY_RETURN_TYPE                                       $12
         28      > RETURN                                                   $12
   19    29*       VERIFY_RETURN_TYPE                                       
         30*     > RETURN                                                   null

End of function ispalindrome

Function sanitizepalindrome:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TiNa3
function name:  sanitizePalindrome
number of ops:  11
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   23     1        INIT_FCALL                                               'preg_replace'
          2        SEND_VAL                                                 '%2F%5B%5Ea-z%5D%2B%2F'
          3        SEND_VAL                                                 ''
          4        INIT_FCALL                                               'strtolower'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $1      
          7        SEND_VAR                                                 $1
          8        DO_ICALL                                         $2      
          9      > RETURN                                                   $2
   24    10*     > RETURN                                                   null

End of function sanitizepalindrome

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.97 ms | 1009 KiB | 20 Q