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 ignoreCount($data) { return @count($data); } function validatedCount($data) { if (PHP_VERSION_ID >= 70300) { if (is_countable($data)) { return count($data); } } elseif (PHP_VERSION_ID >= 70100) { if (is_array($data) || $data instanceof \Countable) { return count($data); } elseif(is_iterable($data)) { return iterator_count($data); } } else { if (is_array($data) || $data instanceof \Countable) { return count($data); } elseif ($data instanceof \Traversable) { return iterator_count($data); } } return null === $data ? 0 : 1; } function castCount($data) { return count((array) $data); } foreach ($tests as $type => $data) { $expected = @count($data); $normal = count($data); $ignored = ignoreCount($data); $validated = validatedCount($data); $casted = castCount($data); echo 'Type ' . $type . ': ' . \PHP_EOL; echo "\t" . 'count(): ' . print_r($normal, 1) . ' '; var_dump($normal === $expected); echo "\t" . 'Ignored: ' . print_r($ignored, 1) . ' '; var_dump($ignored === $expected); echo "\t" . 'Validated: ' . print_r($validated, 1) . ' '; var_dump($validated === $expected); echo "\t" . 'Casted: ' . print_r($casted, 1) . ' '; var_dump($casted === $expected); echo \PHP_EOL . '------' . \PHP_EOL . \PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 95
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 95
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 95
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 95
filename:       /in/UFieo
function name:  (null)
number of ops:  97
compiled vars:  !0 = $tests, !1 = $data, !2 = $type, !3 = $expected, !4 = $normal, !5 = $ignored, !6 = $validated, !7 = $casted
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   DECLARE_CLASS                                            'foocountable'
   21     1        INIT_ARRAY                                       ~8      <array>, 'array'
   22     2        ADD_ARRAY_ELEMENT                                ~8      'hello+world', 'string'
   23     3        ADD_ARRAY_ELEMENT                                ~8      123, 'number'
   24     4        NEW                                              $9      'ArrayIterator'
   21     5        SEND_VAL_EX                                              <array>
   24     6        DO_FCALL                                      0          
   21     7        ADD_ARRAY_ELEMENT                                ~8      $9, 'iterator'
   25     8        NEW                                              $11     'FooCountable'
          9        DO_FCALL                                      0          
         10        ADD_ARRAY_ELEMENT                                ~8      $11, 'countable'
   26    11        ADD_ARRAY_ELEMENT                                ~8      0, 'zero'
   27    12        ADD_ARRAY_ELEMENT                                ~8      '0', 'string_zero'
   28    13        NEW                                              $13     'Foo'
         14        DO_FCALL                                      0          
         15        ADD_ARRAY_ELEMENT                                ~8      $13, 'object'
   29    16        NEW                                              $15     'stdClass'
         17        DO_FCALL                                      0          
         18        ADD_ARRAY_ELEMENT                                ~8      $15, 'stdClass'
   21    19        ADD_ARRAY_ELEMENT                                ~8      null, 'null'
   31    20        ADD_ARRAY_ELEMENT                                ~8      '', 'empty'
   21    21        ADD_ARRAY_ELEMENT                                ~8      <true>, 'boolt'
         22        ADD_ARRAY_ELEMENT                                ~8      <false>, 'boolf'
   20    23        ASSIGN                                                   !0, ~8
   70    24      > FE_RESET_R                                       $18     !0, ->95
         25    > > FE_FETCH_R                                       ~19     $18, !1, ->95
         26    >   ASSIGN                                                   !2, ~19
   71    27        BEGIN_SILENCE                                    ~21     
         28        COUNT                                            ~22     !1
         29        END_SILENCE                                              ~21
         30        ASSIGN                                                   !3, ~22
   72    31        COUNT                                            ~24     !1
         32        ASSIGN                                                   !4, ~24
   73    33        INIT_FCALL                                               'ignorecount'
         34        SEND_VAR                                                 !1
         35        DO_FCALL                                      0  $26     
         36        ASSIGN                                                   !5, $26
   74    37        INIT_FCALL                                               'validatedcount'
         38        SEND_VAR                                                 !1
         39        DO_FCALL                                      0  $28     
         40        ASSIGN                                                   !6, $28
   75    41        INIT_FCALL                                               'castcount'
         42        SEND_VAR                                                 !1
         43        DO_FCALL                                      0  $30     
         44        ASSIGN                                                   !7, $30
   77    45        CONCAT                                           ~32     'Type+', !2
         46        CONCAT                                           ~33     ~32, '%3A+'
         47        CONCAT                                           ~34     ~33, '%0A'
         48        ECHO                                                     ~34
   78    49        INIT_FCALL                                               'print_r'
         50        SEND_VAR                                                 !4
         51        SEND_VAL                                                 1
         52        DO_ICALL                                         $35     
         53        CONCAT                                           ~36     '%09count%28%29%3A+', $35
         54        CONCAT                                           ~37     ~36, '+'
         55        ECHO                                                     ~37
   79    56        INIT_FCALL                                               'var_dump'
         57        IS_IDENTICAL                                     ~38     !4, !3
         58        SEND_VAL                                                 ~38
         59        DO_ICALL                                                 
   80    60        INIT_FCALL                                               'print_r'
         61        SEND_VAR                                                 !5
         62        SEND_VAL                                                 1
         63        DO_ICALL                                         $40     
         64        CONCAT                                           ~41     '%09Ignored%3A+', $40
         65        CONCAT                                           ~42     ~41, '+'
         66        ECHO                                                     ~42
   81    67        INIT_FCALL                                               'var_dump'
         68        IS_IDENTICAL                                     ~43     !5, !3
         69        SEND_VAL                                                 ~43
         70        DO_ICALL                                                 
   82    71        INIT_FCALL                                               'print_r'
         72        SEND_VAR                                                 !6
         73        SEND_VAL                                                 1
         74        DO_ICALL                                         $45     
         75        CONCAT                                           ~46     '%09Validated%3A+', $45
         76        CONCAT                                           ~47     ~46, '+'
         77        ECHO                                                     ~47
   83    78        INIT_FCALL                                               'var_dump'
         79        IS_IDENTICAL                                     ~48     !6, !3
         80        SEND_VAL                                                 ~48
         81        DO_ICALL                                                 
   84    82        INIT_FCALL                                               'print_r'
         83        SEND_VAR                                                 !7
         84        SEND_VAL                                                 1
         85        DO_ICALL                                         $50     
         86        CONCAT                                           ~51     '%09Casted%3A+', $50
         87        CONCAT                                           ~52     ~51, '+'
         88        ECHO                                                     ~52
   85    89        INIT_FCALL                                               'var_dump'
         90        IS_IDENTICAL                                     ~53     !7, !3
         91        SEND_VAL                                                 ~53
         92        DO_ICALL                                                 
   86    93        ECHO                                                     '%0A------%0A%0A'
   70    94      > JMP                                                      ->25
         95    >   FE_FREE                                                  $18
   87    96      > RETURN                                                   1

Function ignorecount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UFieo
function name:  ignoreCount
number of ops:  6
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   39     1        BEGIN_SILENCE                                    ~1      
          2        COUNT                                            ~2      !0
          3        END_SILENCE                                              ~1
          4      > RETURN                                                   ~2
   40     5*     > RETURN                                                   null

End of function ignorecount

Function validatedcount:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 9
Branch analysis from position: 2
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 45
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 27
Branch analysis from position: 10
2 jumps found. (Code = 47) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 26
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
Branch analysis from position: 14
Branch analysis from position: 27
2 jumps found. (Code = 47) Position 1 = 29, Position 2 = 31
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 35
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 41
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
Branch analysis from position: 31
filename:       /in/UFieo
function name:  validatedCount
number of ops:  48
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   44     1      > JMPZ                                                     <true>, ->9
   45     2    >   INIT_FCALL                                               'is_countable'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5      > JMPZ                                                     $1, ->8
   46     6    >   COUNT                                            ~2      !0
          7      > RETURN                                                   ~2
   44     8    > > JMP                                                      ->41
   48     9    > > JMPZ                                                     <true>, ->27
   49    10    >   TYPE_CHECK                                  128  ~3      !0
         11      > JMPNZ_EX                                         ~3      ~3, ->14
         12    >   INSTANCEOF                                       ~4      !0, 'Countable'
         13        BOOL                                             ~3      ~4
         14    > > JMPZ                                                     ~3, ->18
   50    15    >   COUNT                                            ~5      !0
         16      > RETURN                                                   ~5
   49    17*       JMP                                                      ->26
   51    18    >   INIT_FCALL                                               'is_iterable'
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                         $6      
         21      > JMPZ                                                     $6, ->26
   52    22    >   INIT_FCALL                                               'iterator_count'
         23        SEND_VAR                                                 !0
         24        DO_ICALL                                         $7      
         25      > RETURN                                                   $7
   48    26    > > JMP                                                      ->41
   55    27    >   TYPE_CHECK                                  128  ~8      !0
         28      > JMPNZ_EX                                         ~8      ~8, ->31
         29    >   INSTANCEOF                                       ~9      !0, 'Countable'
         30        BOOL                                             ~8      ~9
         31    > > JMPZ                                                     ~8, ->35
   56    32    >   COUNT                                            ~10     !0
         33      > RETURN                                                   ~10
   55    34*       JMP                                                      ->41
   57    35    >   INSTANCEOF                                               !0, 'Traversable'
         36      > JMPZ                                                     ~11, ->41
   58    37    >   INIT_FCALL                                               'iterator_count'
         38        SEND_VAR                                                 !0
         39        DO_ICALL                                         $12     
         40      > RETURN                                                   $12
   62    41    >   TYPE_CHECK                                    2          !0
         42      > JMPZ                                                     ~13, ->45
         43    >   QM_ASSIGN                                        ~14     0
         44      > JMP                                                      ->46
         45    >   QM_ASSIGN                                        ~14     1
         46    > > RETURN                                                   ~14
   63    47*     > RETURN                                                   null

End of function validatedcount

Function castcount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UFieo
function name:  castCount
number of ops:  5
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
   67     1        CAST                                          7  ~1      !0
          2        COUNT                                            ~2      ~1
          3      > RETURN                                                   ~2
   68     4*     > RETURN                                                   null

End of function castcount

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/UFieo
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:
143.8 ms | 1450 KiB | 21 Q