3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private $a; protected $b; public $c; } class FooCountable implements \Countable { private $elements = [1,2,3]; public function count() { return count($this->elements); } } $tests = [ 'array' => [1,2,3], 'string' => 'hello world', 'number' => 123, 'iterator' => new \ArrayIterator([1,2,3]), 'countable' => new \FooCountable(), 'zero' => 0, 'string_zero' => '0', 'object' => new \Foo(), 'stdClass' => new \stdClass, 'null' => null, 'empty' => '', 'boolt' => true, 'boolf' => false ]; function countValid($array_or_countable, $mode = \COUNT_NORMAL) { if ( (\PHP_VERSION_ID >= 70300 && \is_countable($array_or_countable)) || \is_array($array_or_countable) || $array_or_countable instanceof \Countable ) { return \count($array_or_countable, $mode); } return null === $array_or_countable ? 0 : 1; } foreach($tests as $key => $test) { echo $key . ': ' . print_r(countValid($test), 1) . \PHP_EOL; } echo 'undefined: ' . print_r(countValid($undefined), 1) . \PHP_EOL;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 39
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 39
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
filename:       /in/8M0Wd
function name:  (null)
number of ops:  51
compiled vars:  !0 = $tests, !1 = $test, !2 = $key, !3 = $undefined
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   DECLARE_CLASS                                            'foocountable'
   21     1        INIT_ARRAY                                       ~4      <array>, 'array'
   22     2        ADD_ARRAY_ELEMENT                                ~4      'hello+world', 'string'
   23     3        ADD_ARRAY_ELEMENT                                ~4      123, 'number'
   24     4        NEW                                              $5      'ArrayIterator'
          5        SEND_VAL_EX                                              <array>
          6        DO_FCALL                                      0          
          7        ADD_ARRAY_ELEMENT                                ~4      $5, 'iterator'
   25     8        NEW                                              $7      'FooCountable'
          9        DO_FCALL                                      0          
         10        ADD_ARRAY_ELEMENT                                ~4      $7, 'countable'
   26    11        ADD_ARRAY_ELEMENT                                ~4      0, 'zero'
   27    12        ADD_ARRAY_ELEMENT                                ~4      '0', 'string_zero'
   28    13        NEW                                              $9      'Foo'
         14        DO_FCALL                                      0          
         15        ADD_ARRAY_ELEMENT                                ~4      $9, 'object'
   29    16        NEW                                              $11     'stdClass'
         17        DO_FCALL                                      0          
         18        ADD_ARRAY_ELEMENT                                ~4      $11, 'stdClass'
   21    19        ADD_ARRAY_ELEMENT                                ~4      null, 'null'
   31    20        ADD_ARRAY_ELEMENT                                ~4      '', 'empty'
   21    21        ADD_ARRAY_ELEMENT                                ~4      <true>, 'boolt'
         22        ADD_ARRAY_ELEMENT                                ~4      <false>, 'boolf'
   20    23        ASSIGN                                                   !0, ~4
   49    24      > FE_RESET_R                                       $14     !0, ->39
         25    > > FE_FETCH_R                                       ~15     $14, !1, ->39
         26    >   ASSIGN                                                   !2, ~15
   50    27        CONCAT                                           ~17     !2, '%3A+'
         28        INIT_FCALL                                               'print_r'
         29        INIT_FCALL                                               'countvalid'
         30        SEND_VAR                                                 !1
         31        DO_FCALL                                      0  $18     
         32        SEND_VAR                                                 $18
         33        SEND_VAL                                                 1
         34        DO_ICALL                                         $19     
         35        CONCAT                                           ~20     ~17, $19
         36        CONCAT                                           ~21     ~20, '%0A'
         37        ECHO                                                     ~21
   49    38      > JMP                                                      ->25
         39    >   FE_FREE                                                  $14
   53    40        INIT_FCALL                                               'print_r'
         41        INIT_FCALL                                               'countvalid'
         42        SEND_VAR                                                 !3
         43        DO_FCALL                                      0  $22     
         44        SEND_VAR                                                 $22
         45        SEND_VAL                                                 1
         46        DO_ICALL                                         $23     
         47        CONCAT                                           ~24     'undefined%3A+', $23
         48        CONCAT                                           ~25     ~24, '%0A'
         49        ECHO                                                     ~25
   54    50      > RETURN                                                   1

Function countvalid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
2 jumps found. (Code = 47) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
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 = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
Branch analysis from position: 9
filename:       /in/8M0Wd
function name:  countValid
number of ops:  25
compiled vars:  !0 = $array_or_countable, !1 = $mode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <const ast>
   39     2        INIT_FCALL                                               'is_countable'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $2      
          5        BOOL                                             ~3      $2
          6      > JMPNZ_EX                                         ~3      ~3, ->9
   40     7    >   TYPE_CHECK                                  128  ~4      !0
          8        BOOL                                             ~3      ~4
          9    > > JMPNZ_EX                                         ~3      ~3, ->12
   41    10    >   INSTANCEOF                                       ~5      !0, 'Countable'
         11        BOOL                                             ~3      ~5
         12    > > JMPZ                                                     ~3, ->18
   43    13    >   INIT_FCALL                                               'count'
         14        SEND_VAR                                                 !0
         15        SEND_VAR                                                 !1
         16        DO_ICALL                                         $6      
         17      > RETURN                                                   $6
   46    18    >   TYPE_CHECK                                    2          !0
         19      > JMPZ                                                     ~7, ->22
         20    >   QM_ASSIGN                                        ~8      0
         21      > JMP                                                      ->23
         22    >   QM_ASSIGN                                        ~8      1
         23    > > RETURN                                                   ~8
   47    24*     > RETURN                                                   null

End of function countvalid

Class Foo: [no user functions]
Class FooCountable:
Function count:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8M0Wd
function name:  count
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   FETCH_OBJ_R                                      ~0      'elements'
          1        COUNT                                            ~1      ~0
          2      > RETURN                                                   ~1
   17     3*     > RETURN                                                   null

End of function count

End of class FooCountable.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.5 ms | 1407 KiB | 21 Q