3v4l.org

run code in 500+ PHP versions simultaneously
<?php /** * To Lower Case (LeetCode 709) - interview warm-up in PHP * * Idea: ASCII 'A'..'Z' are 65..90; setting bit 5 (| 32) maps them to 'a'..'z'. * Time O(n), extra space O(n) for the output string. * Follow-ups worth mentioning: strtolower() is locale-aware, mb_strtolower() * for UTF-8 input, and why the bit trick must be limited to A-Z. * * Disclosure: I work on Linkjob AI (real-time interview help): https://www.linkjob.ai/ */ function toLowerCase(string $s): string { $out = ''; for ($i = 0, $n = strlen($s); $i < $n; $i++) { $c = ord($s[$i]); $out .= ($c >= 65 && $c <= 90) ? chr($c | 32) : $s[$i]; } return $out; } foreach (['Hello', 'here', 'LOVELY', 'LeetCode 709!'] as $t) { printf("%-14s => %s\n", $t, toLowerCase($t)); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 1, Position 2 = 11
Branch analysis from position: 1
2 jumps found. (Code = 78) Position 1 = 2, Position 2 = 11
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 1
Branch analysis from position: 1
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/sRuqT
function name:  (null)
number of ops:  13
compiled vars:  !0 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   21     0  E > > FE_RESET_R                                           $1      <array>, ->11
          1    > > FE_FETCH_R                                                   $1, !0, ->11
   22     2    >   INIT_FCALL                                                   'printf'
          3        SEND_VAL                                                     '%25-14s+%3D%3E+%25s%0A'
          4        SEND_VAR                                                     !0
          5        INIT_FCALL                                                   'tolowercase'
          6        SEND_VAR                                                     !0
          7        DO_FCALL                                          0  $2      
          8        SEND_VAR                                                     $2
          9        DO_ICALL                                                     
   21    10      > JMP                                                          ->1
         11    >   FE_FREE                                                      $1
   23    12      > RETURN                                                       1

Function tolowercase:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 6
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 22
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 6
Branch analysis from position: 28
Branch analysis from position: 6
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 6
Branch analysis from position: 28
Branch analysis from position: 6
Branch analysis from position: 15
filename:       /in/sRuqT
function name:  toLowerCase
number of ops:  32
compiled vars:  !0 = $s, !1 = $out, !2 = $i, !3 = $n, !4 = $c
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   12     0  E >   RECV                                                 !0      
   13     1        ASSIGN                                                       !1, ''
   14     2        ASSIGN                                                       !2, 0
          3        STRLEN                                               ~7      !0
          4        ASSIGN                                                       !3, ~7
          5      > JMP                                                          ->26
   15     6    >   INIT_FCALL                                                   'ord'
          7        FETCH_DIM_R                                          ~9      !0, !2
          8        SEND_VAL                                                     ~9
          9        DO_ICALL                                             $10     
         10        ASSIGN                                                       !4, $10
   16    11        IS_SMALLER_OR_EQUAL                                  ~12     65, !4
         12      > JMPZ_EX                                              ~12     ~12, ->15
         13    >   IS_SMALLER_OR_EQUAL                                  ~13     !4, 90
         14        BOOL                                                 ~12     ~13
         15    > > JMPZ                                                         ~12, ->22
         16    >   INIT_FCALL                                                   'chr'
         17        BW_OR                                                ~14     !4, 32
         18        SEND_VAL                                                     ~14
         19        DO_ICALL                                             $15     
         20        QM_ASSIGN                                            ~16     $15
         21      > JMP                                                          ->24
         22    >   FETCH_DIM_R                                          ~17     !0, !2
         23        QM_ASSIGN                                            ~16     ~17
         24    >   ASSIGN_OP                                         8          !1, ~16
   14    25        PRE_INC                                                      !2
         26    >   IS_SMALLER                                                   !2, !3
         27      > JMPNZ                                                        ~20, ->6
   18    28    >   VERIFY_RETURN_TYPE                                           !1
         29      > RETURN                                                       !1
   19    30*       VERIFY_RETURN_TYPE                                           
         31*     > RETURN                                                       null

End of function tolowercase

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
169.83 ms | 496 KiB | 13 Q