3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); class FooStringable implements \Stringable { public function __toString(): string { return 'foo'; } } function my_printf(mixed ...$args): void { $printArg = fn (mixed $arg) => match (true) { is_bool($arg) => $arg ? 'true' : 'false', is_string($arg) => "'{$arg}'", is_numeric($arg) => $arg, is_null($arg) => 'null', is_object($arg) => $arg::class, default => var_export($arg, true), }; $argsTxt = array_map($printArg, $args); $argsTxt = implode(', ', $argsTxt); echo "printf({$argsTxt});\n"; try { printf(...$args); } catch (Throwable $e) { echo $e->getMessage() . "\n"; } echo "\n-------------------------------\n"; }; $stream = fopen('php://stdout', 'w'); // Error my_printf('%d', new FooStringable()); my_printf('%d', rand() ? new FooStringable() : 5); my_printf('%f', new FooStringable()); my_printf('%*s', '5', 'a'); my_printf('%*s', 5.0, 'a'); my_printf('%*s', new \SimpleXMLElement('<a>7</a>'), 'a'); my_printf('%*s', null, 'a'); my_printf('%*s', true, 'a'); my_printf('%.*s', '5', 'a'); my_printf('%2$s %3$.*s', '1', 5, 'a'); // * is the first ordinary placeholder, so it matches '1' my_printf('%1$-\'X10.2f', new FooStringable()); my_printf('%s %1$*.*f', new FooStringable(), 5, 2); my_printf('%3$f', 1, 2, new FooStringable()); // Strict error my_printf('%d', 1.23); my_printf('%d', rand() ? 1.23 : 1); my_printf('%d', 'a'); my_printf('%d', '1.23'); my_printf('%d', null); my_printf('%d', true); my_printf('%d', new \SimpleXMLElement('<a>aaa</a>')); my_printf('%f', 'a'); my_printf('%f', null); my_printf('%f', true); my_printf('%f', new \SimpleXMLElement('<a>aaa</a>')); my_printf('%s', null); my_printf('%s', true); // Error, but already reported by CallToFunctionParametersRule my_printf('%d', new \stdClass()); my_printf('%s', []); // Error, but already reported by my_printfParametersRule my_printf('%s'); my_printf('%s', 1, 2); // OK my_printf('%s', 'a'); my_printf('%s', new FooStringable()); my_printf('%d', 1); my_printf('%f', 1); my_printf('%f', 1.1); my_printf('%*s', 5, 'a'); my_printf('%2$*s', 5, 'a'); my_printf('%s %2$*s', 'a', 5, 'a'); my_printf('%1$-+\'X10.2f', 5); my_printf('%1$*.*f %s %2$d', 5, 6, new FooStringable()); // 5.000000 foo 6
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 100, Position 2 = 102
Branch analysis from position: 100
1 jumps found. (Code = 42) Position 1 = 103
Branch analysis from position: 103
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 102
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 100, Position 2 = 102
Branch analysis from position: 100
Branch analysis from position: 102
filename:       /in/3pXMc
function name:  (null)
number of ops:  224
compiled vars:  !0 = $stream
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   DECLARE_CLASS                                            'foostringable'
   35     1        INIT_FCALL                                               'fopen'
          2        SEND_VAL                                                 'php%3A%2F%2Fstdout'
          3        SEND_VAL                                                 'w'
          4        DO_ICALL                                         $1      
          5        ASSIGN                                                   !0, $1
   37     6        INIT_FCALL                                               'my_printf'
          7        SEND_VAL                                                 '%25d'
          8        NEW                                              $3      'FooStringable'
          9        DO_FCALL                                      0          
         10        SEND_VAR                                                 $3
         11        DO_FCALL                                      0          
   38    12        INIT_FCALL                                               'my_printf'
         13        SEND_VAL                                                 '%25d'
         14        INIT_FCALL                                               'rand'
         15        DO_ICALL                                         $6      
         16      > JMPZ                                                     $6, ->21
         17    >   NEW                                              $7      'FooStringable'
         18        DO_FCALL                                      0          
         19        QM_ASSIGN                                        ~9      $7
         20      > JMP                                                      ->22
         21    >   QM_ASSIGN                                        ~9      5
         22    >   SEND_VAL                                                 ~9
         23        DO_FCALL                                      0          
   39    24        INIT_FCALL                                               'my_printf'
         25        SEND_VAL                                                 '%25f'
         26        NEW                                              $11     'FooStringable'
         27        DO_FCALL                                      0          
         28        SEND_VAR                                                 $11
         29        DO_FCALL                                      0          
   40    30        INIT_FCALL                                               'my_printf'
         31        SEND_VAL                                                 '%25%2As'
         32        SEND_VAL                                                 '5'
         33        SEND_VAL                                                 'a'
         34        DO_FCALL                                      0          
   41    35        INIT_FCALL                                               'my_printf'
         36        SEND_VAL                                                 '%25%2As'
         37        SEND_VAL                                                 5
         38        SEND_VAL                                                 'a'
         39        DO_FCALL                                      0          
   42    40        INIT_FCALL                                               'my_printf'
         41        SEND_VAL                                                 '%25%2As'
         42        NEW                                              $16     'SimpleXMLElement'
         43        SEND_VAL_EX                                              '%3Ca%3E7%3C%2Fa%3E'
         44        DO_FCALL                                      0          
         45        SEND_VAR                                                 $16
         46        SEND_VAL                                                 'a'
         47        DO_FCALL                                      0          
   43    48        INIT_FCALL                                               'my_printf'
         49        SEND_VAL                                                 '%25%2As'
         50        SEND_VAL                                                 null
         51        SEND_VAL                                                 'a'
         52        DO_FCALL                                      0          
   44    53        INIT_FCALL                                               'my_printf'
         54        SEND_VAL                                                 '%25%2As'
         55        SEND_VAL                                                 <true>
         56        SEND_VAL                                                 'a'
         57        DO_FCALL                                      0          
   45    58        INIT_FCALL                                               'my_printf'
         59        SEND_VAL                                                 '%25.%2As'
         60        SEND_VAL                                                 '5'
         61        SEND_VAL                                                 'a'
         62        DO_FCALL                                      0          
   46    63        INIT_FCALL                                               'my_printf'
         64        SEND_VAL                                                 '%252%24s+%253%24.%2As'
         65        SEND_VAL                                                 '1'
         66        SEND_VAL                                                 5
         67        SEND_VAL                                                 'a'
         68        DO_FCALL                                      0          
   47    69        INIT_FCALL                                               'my_printf'
         70        SEND_VAL                                                 '%251%24-%27X10.2f'
         71        NEW                                              $23     'FooStringable'
         72        DO_FCALL                                      0          
         73        SEND_VAR                                                 $23
         74        DO_FCALL                                      0          
   48    75        INIT_FCALL                                               'my_printf'
         76        SEND_VAL                                                 '%25s+%251%24%2A.%2Af'
         77        NEW                                              $26     'FooStringable'
         78        DO_FCALL                                      0          
         79        SEND_VAR                                                 $26
         80        SEND_VAL                                                 5
         81        SEND_VAL                                                 2
         82        DO_FCALL                                      0          
   49    83        INIT_FCALL                                               'my_printf'
         84        SEND_VAL                                                 '%253%24f'
         85        SEND_VAL                                                 1
         86        SEND_VAL                                                 2
         87        NEW                                              $29     'FooStringable'
         88        DO_FCALL                                      0          
         89        SEND_VAR                                                 $29
         90        DO_FCALL                                      0          
   52    91        INIT_FCALL                                               'my_printf'
         92        SEND_VAL                                                 '%25d'
         93        SEND_VAL                                                 1.23
         94        DO_FCALL                                      0          
   53    95        INIT_FCALL                                               'my_printf'
         96        SEND_VAL                                                 '%25d'
         97        INIT_FCALL                                               'rand'
         98        DO_ICALL                                         $33     
         99      > JMPZ                                                     $33, ->102
        100    >   QM_ASSIGN                                        ~34     1.23
        101      > JMP                                                      ->103
        102    >   QM_ASSIGN                                        ~34     1
        103    >   SEND_VAL                                                 ~34
        104        DO_FCALL                                      0          
   54   105        INIT_FCALL                                               'my_printf'
        106        SEND_VAL                                                 '%25d'
        107        SEND_VAL                                                 'a'
        108        DO_FCALL                                      0          
   55   109        INIT_FCALL                                               'my_printf'
        110        SEND_VAL                                                 '%25d'
        111        SEND_VAL                                                 '1.23'
        112        DO_FCALL                                      0          
   56   113        INIT_FCALL                                               'my_printf'
        114        SEND_VAL                                                 '%25d'
        115        SEND_VAL                                                 null
        116        DO_FCALL                                      0          
   57   117        INIT_FCALL                                               'my_printf'
        118        SEND_VAL                                                 '%25d'
        119        SEND_VAL                                                 <true>
        120        DO_FCALL                                      0          
   58   121        INIT_FCALL                                               'my_printf'
        122        SEND_VAL                                                 '%25d'
        123        NEW                                              $40     'SimpleXMLElement'
        124        SEND_VAL_EX                                              '%3Ca%3Eaaa%3C%2Fa%3E'
        125        DO_FCALL                                      0          
        126        SEND_VAR                                                 $40
        127        DO_FCALL                                      0          
   60   128        INIT_FCALL                                               'my_printf'
        129        SEND_VAL                                                 '%25f'
        130        SEND_VAL                                                 'a'
        131        DO_FCALL                                      0          
   61   132        INIT_FCALL                                               'my_printf'
        133        SEND_VAL                                                 '%25f'
        134        SEND_VAL                                                 null
        135        DO_FCALL                                      0          
   62   136        INIT_FCALL                                               'my_printf'
        137        SEND_VAL                                                 '%25f'
        138        SEND_VAL                                                 <true>
        139        DO_FCALL                                      0          
   63   140        INIT_FCALL                                               'my_printf'
        141        SEND_VAL                                                 '%25f'
        142        NEW                                              $46     'SimpleXMLElement'
        143        SEND_VAL_EX                                              '%3Ca%3Eaaa%3C%2Fa%3E'
        144        DO_FCALL                                      0          
        145        SEND_VAR                                                 $46
        146        DO_FCALL                                      0          
   65   147        INIT_FCALL                                               'my_printf'
        148        SEND_VAL                                                 '%25s'
        149        SEND_VAL                                                 null
        150        DO_FCALL                                      0          
   66   151        INIT_FCALL                                               'my_printf'
        152        SEND_VAL                                                 '%25s'
        153        SEND_VAL                                                 <true>
        154        DO_FCALL                                      0          
   69   155        INIT_FCALL                                               'my_printf'
        156        SEND_VAL                                                 '%25d'
        157        NEW                                              $51     'stdClass'
        158        DO_FCALL                                      0          
        159        SEND_VAR                                                 $51
        160        DO_FCALL                                      0          
   70   161        INIT_FCALL                                               'my_printf'
        162        SEND_VAL                                                 '%25s'
        163        SEND_VAL                                                 <array>
        164        DO_FCALL                                      0          
   73   165        INIT_FCALL                                               'my_printf'
        166        SEND_VAL                                                 '%25s'
        167        DO_FCALL                                      0          
   74   168        INIT_FCALL                                               'my_printf'
        169        SEND_VAL                                                 '%25s'
        170        SEND_VAL                                                 1
        171        SEND_VAL                                                 2
        172        DO_FCALL                                      0          
   77   173        INIT_FCALL                                               'my_printf'
        174        SEND_VAL                                                 '%25s'
        175        SEND_VAL                                                 'a'
        176        DO_FCALL                                      0          
   78   177        INIT_FCALL                                               'my_printf'
        178        SEND_VAL                                                 '%25s'
        179        NEW                                              $58     'FooStringable'
        180        DO_FCALL                                      0          
        181        SEND_VAR                                                 $58
        182        DO_FCALL                                      0          
   79   183        INIT_FCALL                                               'my_printf'
        184        SEND_VAL                                                 '%25d'
        185        SEND_VAL                                                 1
        186        DO_FCALL                                      0          
   80   187        INIT_FCALL                                               'my_printf'
        188        SEND_VAL                                                 '%25f'
        189        SEND_VAL                                                 1
        190        DO_FCALL                                      0          
   81   191        INIT_FCALL                                               'my_printf'
        192        SEND_VAL                                                 '%25f'
        193        SEND_VAL                                                 1.1
        194        DO_FCALL                                      0          
   82   195        INIT_FCALL                                               'my_printf'
        196        SEND_VAL                                                 '%25%2As'
        197        SEND_VAL                                                 5
        198        SEND_VAL                                                 'a'
        199        DO_FCALL                                      0          
   83   200        INIT_FCALL                                               'my_printf'
        201        SEND_VAL                                                 '%252%24%2As'
        202        SEND_VAL                                                 5
        203        SEND_VAL                                                 'a'
        204        DO_FCALL                                      0          
   84   205        INIT_FCALL                                               'my_printf'
        206        SEND_VAL                                                 '%25s+%252%24%2As'
        207        SEND_VAL                                                 'a'
        208        SEND_VAL                                                 5
        209        SEND_VAL                                                 'a'
        210        DO_FCALL                                      0          
   85   211        INIT_FCALL                                               'my_printf'
        212        SEND_VAL                                                 '%251%24-%2B%27X10.2f'
        213        SEND_VAL                                                 5
        214        DO_FCALL                                      0          
   86   215        INIT_FCALL                                               'my_printf'
        216        SEND_VAL                                                 '%251%24%2A.%2Af+%25s+%252%24d'
        217        SEND_VAL                                                 5
        218        SEND_VAL                                                 6
        219        NEW                                              $68     'FooStringable'
        220        DO_FCALL                                      0          
        221        SEND_VAR                                                 $68
        222        DO_FCALL                                      0          
        223      > RETURN                                                   1

Function my_printf:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 22
Branch analysis from position: 22
2 jumps found. (Code = 107) Position 1 = 23, Position 2 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3pXMc
function name:  my_printf
number of ops:  29
compiled vars:  !0 = $args, !1 = $printArg, !2 = $argsTxt, !3 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV_VARIADIC                                    !0      
   14     1        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
          2        ASSIGN                                                   !1, ~4
   22     3        INIT_FCALL                                               'array_map'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $6      
          7        ASSIGN                                                   !2, $6
   23     8        INIT_FCALL                                               'implode'
          9        SEND_VAL                                                 '%2C+'
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                         $8      
         12        ASSIGN                                                   !2, $8
   24    13        ROPE_INIT                                     3  ~11     'printf%28'
         14        ROPE_ADD                                      1  ~11     ~11, !2
         15        ROPE_END                                      2  ~10     ~11, '%29%3B%0A'
         16        ECHO                                                     ~10
   27    17        INIT_FCALL                                               'printf'
         18        SEND_UNPACK                                              !0
         19        CHECK_UNDEF_ARGS                                         
         20        DO_ICALL                                                 
         21      > JMP                                                      ->27
   28    22  E > > CATCH                                       last         'Throwable'
   29    23    >   INIT_METHOD_CALL                                         !3, 'getMessage'
         24        DO_FCALL                                      0  $14     
         25        CONCAT                                           ~15     $14, '%0A'
         26        ECHO                                                     ~15
   32    27    >   ECHO                                                     '%0A-------------------------------%0A'
   33    28      > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 4, Position 2 = 19
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 25
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 12, Position 2 = 30
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 32
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 34
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
filename:       /in/3pXMc
function name:  {closure}
number of ops:  45
compiled vars:  !0 = $arg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   15     1        TYPE_CHECK                                   12  ~2      !0
          2        IS_IDENTICAL                                             ~2, <true>
          3      > JMPNZ                                                    ~1, ->19
   16     4    >   TYPE_CHECK                                   64  ~3      !0
          5        IS_IDENTICAL                                             ~3, <true>
          6      > JMPNZ                                                    ~1, ->25
   17     7    >   INIT_FCALL                                               'is_numeric'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $4      
         10        IS_IDENTICAL                                             $4, <true>
         11      > JMPNZ                                                    ~1, ->30
   18    12    >   TYPE_CHECK                                    2  ~5      !0
         13        IS_IDENTICAL                                             ~5, <true>
         14      > JMPNZ                                                    ~1, ->32
   19    15    >   TYPE_CHECK                                  256  ~6      !0
         16        IS_IDENTICAL                                             ~6, <true>
         17      > JMPNZ                                                    ~1, ->34
         18    > > JMP                                                      ->37
   15    19    > > JMPZ                                                     !0, ->22
         20    >   QM_ASSIGN                                        ~7      'true'
         21      > JMP                                                      ->23
         22    >   QM_ASSIGN                                        ~7      'false'
         23    >   QM_ASSIGN                                        ~8      ~7
         24      > JMP                                                      ->43
   16    25    >   ROPE_INIT                                     3  ~10     '%27'
         26        ROPE_ADD                                      1  ~10     ~10, !0
         27        ROPE_END                                      2  ~9      ~10, '%27'
         28        QM_ASSIGN                                        ~8      ~9
         29      > JMP                                                      ->43
   17    30    >   QM_ASSIGN                                        ~8      !0
         31      > JMP                                                      ->43
   18    32    >   QM_ASSIGN                                        ~8      'null'
         33      > JMP                                                      ->43
   19    34    >   FETCH_CLASS_NAME                                 ~12     !0
         35        QM_ASSIGN                                        ~8      ~12
         36      > JMP                                                      ->43
   20    37    >   INIT_FCALL                                               'var_export'
         38        SEND_VAR                                                 !0
         39        SEND_VAL                                                 <true>
         40        DO_ICALL                                         $13     
         41        QM_ASSIGN                                        ~8      $13
         42      > JMP                                                      ->43
         43    > > RETURN                                                   ~8
   21    44*     > RETURN                                                   null

End of Dynamic Function 0

End of function my_printf

Class FooStringable:
Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3pXMc
function name:  __toString
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E > > RETURN                                                   'foo'
   10     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function __tostring

End of class FooStringable.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.54 ms | 1038 KiB | 60 Q