3v4l.org

run code in 500+ PHP versions simultaneously
<?php $pdo = new PDO('sqlite::memory:'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec('CREATE TABLE t (id INTEGER, name TEXT)'); $pdo->exec("INSERT INTO t VALUES (1, 'a'), (2, 'b'), (3, 'c')"); // each fetch() returns the next row $stmt = $pdo->query('SELECT id, name FROM t ORDER BY id'); var_dump($stmt->fetch(PDO::FETCH_ASSOC)['id'], $stmt->fetch(PDO::FETCH_ASSOC)['id']); // fetchColumn() moves to the next row too var_dump($stmt->fetchColumn()); var_dump($stmt->fetch()); // fetchAll() returns the rest, then nothing $stmt = $pdo->query('SELECT id FROM t ORDER BY id'); $stmt->fetch(); var_dump(count($stmt->fetchAll()), count($stmt->fetchAll())); // fetchObject() and FETCH_CLASS run the constructor, FETCH_FUNC runs a callback class Row { public $id; public function __construct() { echo "constructor runs\n"; } } $stmt = $pdo->query('SELECT id FROM t ORDER BY id'); $stmt->fetchObject(Row::class); $stmt->setFetchMode(PDO::FETCH_CLASS, Row::class); $stmt->fetch(); $stmt = $pdo->query('SELECT id FROM t ORDER BY id'); $stmt->fetchAll(PDO::FETCH_FUNC, function ($id) { echo "callback runs for $id\n"; return $id; });
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/069gK
function name:  (null)
number of ops:  79
compiled vars:  !0 = $pdo, !1 = $stmt
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   NEW                                                  $2      'PDO'
          1        SEND_VAL_EX                                                  'sqlite%3A%3Amemory%3A'
          2        DO_FCALL                                          0          
          3        ASSIGN                                                       !0, $2
    4     4        INIT_METHOD_CALL                                             !0, 'setAttribute'
          5        SEND_VAL_EX                                                  3
          6        SEND_VAL_EX                                                  2
          7        DO_FCALL                                          0          
    5     8        INIT_METHOD_CALL                                             !0, 'exec'
          9        SEND_VAL_EX                                                  'CREATE+TABLE+t+%28id+INTEGER%2C+name+TEXT%29'
         10        DO_FCALL                                          0          
    6    11        INIT_METHOD_CALL                                             !0, 'exec'
         12        SEND_VAL_EX                                                  'INSERT+INTO+t+VALUES+%281%2C+%27a%27%29%2C+%282%2C+%27b%27%29%2C+%283%2C+%27c%27%29'
         13        DO_FCALL                                          0          
    9    14        INIT_METHOD_CALL                                             !0, 'query'
         15        SEND_VAL_EX                                                  'SELECT+id%2C+name+FROM+t+ORDER+BY+id'
         16        DO_FCALL                                          0  $8      
         17        ASSIGN                                                       !1, $8
   10    18        INIT_FCALL                                                   'var_dump'
         19        INIT_METHOD_CALL                                             !1, 'fetch'
         20        SEND_VAL_EX                                                  2
         21        DO_FCALL                                          0  $10     
         22        FETCH_DIM_R                                          ~11     $10, 'id'
         23        SEND_VAL                                                     ~11
         24        INIT_METHOD_CALL                                             !1, 'fetch'
         25        SEND_VAL_EX                                                  2
         26        DO_FCALL                                          0  $12     
         27        FETCH_DIM_R                                          ~13     $12, 'id'
         28        SEND_VAL                                                     ~13
         29        DO_ICALL                                                     
   12    30        INIT_FCALL                                                   'var_dump'
         31        INIT_METHOD_CALL                                             !1, 'fetchColumn'
         32        DO_FCALL                                          0  $15     
         33        SEND_VAR                                                     $15
         34        DO_ICALL                                                     
   13    35        INIT_FCALL                                                   'var_dump'
         36        INIT_METHOD_CALL                                             !1, 'fetch'
         37        DO_FCALL                                          0  $17     
         38        SEND_VAR                                                     $17
         39        DO_ICALL                                                     
   16    40        INIT_METHOD_CALL                                             !0, 'query'
         41        SEND_VAL_EX                                                  'SELECT+id+FROM+t+ORDER+BY+id'
         42        DO_FCALL                                          0  $19     
         43        ASSIGN                                                       !1, $19
   17    44        INIT_METHOD_CALL                                             !1, 'fetch'
         45        DO_FCALL                                          0          
   18    46        INIT_FCALL                                                   'var_dump'
         47        INIT_METHOD_CALL                                             !1, 'fetchAll'
         48        DO_FCALL                                          0  $22     
         49        COUNT                                                ~23     $22
         50        SEND_VAL                                                     ~23
         51        INIT_METHOD_CALL                                             !1, 'fetchAll'
         52        DO_FCALL                                          0  $24     
         53        COUNT                                                ~25     $24
         54        SEND_VAL                                                     ~25
         55        DO_ICALL                                                     
   29    56        INIT_METHOD_CALL                                             !0, 'query'
         57        SEND_VAL_EX                                                  'SELECT+id+FROM+t+ORDER+BY+id'
         58        DO_FCALL                                          0  $27     
         59        ASSIGN                                                       !1, $27
   30    60        INIT_METHOD_CALL                                             !1, 'fetchObject'
         61        SEND_VAL_EX                                                  'Row'
         62        DO_FCALL                                          0          
   31    63        INIT_METHOD_CALL                                             !1, 'setFetchMode'
         64        SEND_VAL_EX                                                  8
         65        SEND_VAL_EX                                                  'Row'
         66        DO_FCALL                                          0          
   32    67        INIT_METHOD_CALL                                             !1, 'fetch'
         68        DO_FCALL                                          0          
   33    69        INIT_METHOD_CALL                                             !0, 'query'
         70        SEND_VAL_EX                                                  'SELECT+id+FROM+t+ORDER+BY+id'
         71        DO_FCALL                                          0  $32     
         72        ASSIGN                                                       !1, $32
   34    73        INIT_METHOD_CALL                                             !1, 'fetchAll'
         74        SEND_VAL_EX                                                  10
         75        DECLARE_LAMBDA_FUNCTION                              ~34     [0]
         76        SEND_VAL_EX                                                  ~34
         77        DO_FCALL                                          0          
         78      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/069gK
function name:  {closure:/in/069gK:34}
number of ops:  7
compiled vars:  !0 = $id
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
          0  E >   RECV                                                 !0      
          1        ROPE_INIT                                         3  ~2      'callback+runs+for+'
          2        ROPE_ADD                                          1  ~2      ~2, !0
          3        ROPE_END                                          2  ~1      ~2, '%0A'
          4        ECHO                                                         ~1
          5      > RETURN                                                       !0
          6*     > RETURN                                                       null

End of Dynamic Function 0

Class Row:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/069gK
function name:  __construct
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   ECHO                                                         'constructor+runs%0A'
   27     1      > RETURN                                                       null

End of function __construct

End of class Row.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
115.87 ms | 495 KiB | 10 Q