3v4l.org

run code in 300+ PHP versions simultaneously
<?php $table=New Table(); $table->addRecord("Jesus", "Blue", "Fish"); $table->addRecord("Tesla", "", "Coil",); echo $table->tableToHTML(); class Table { private $header; // the table's header private $data; // the actual table's data private $classesCells; // the classes of columns private $classesRows; // the classes of rows private $biggestRow; // number of cells of the longest row public function __construct() { $this->data=array(); $this->header=array(); $this->classesCells=array(); $this->classesRows=array(); $this->biggestRow=0; } /* Add/change header of table * * Takes: Variable number of strings * * */ public function makeHeader(){ $this->header=func_get_args(); // Check if biggestRow should be updated $rowLength=func_num_args(); if ($rowLength>$this->biggestRow) $this->biggestRow=$rowLength; } /* Add a record to the table * * Takes: Variable number of strings * * */ public function addRecord(){ $row=func_get_args(); $this->data[]=$row; // Check if biggestRow should be updated $rowLength=count($row); if ($rowLength>$this->biggestRow) $this->biggestRow=$rowLength; } /* Add classes to all cells on specific column * * Takes: Class name * Column number * * */ public function addClassX($classname, $x){ $rowsN=count($this->data); for ($i=0; $i<$rowsN; $i++){ $this->classesCells[$i][$x]=$classname; } } /* Add classes to every n-th row * * Takes: Class name * Every n row * Starting row position * * */ public function addClassRowEvery($classname, $n, $startingRow){ for ($i=$startingRow; $i<count($this->data); $i+=$n){ $this->classesRows[$i]=$classname; } } /* Print the table in raw format with all variables etc. * * */ public function showTableInfo() { echo '<pre>'; echo '<b>Number of cells in longest row:</b><br />'; echo $this->biggestRow . '<br />'; echo '<br /><b>Header:</b><br />'; print_r($this->header); echo '<br /><b>Data:</b><br />'; print_r($this->data); echo '<br /><b>Row classes:</b><br />'; print_r($this->classesRows); echo '<br /><b>Cell classes:</b><br />'; print_r($this->classesCells); echo '</pre>'; } /* Convert table to HTML code * * Gives: string with formatted table in HTML * * */ public function tableToHTML() { $cellsY=count($this->data); $cellsX=$this->biggestRow; $string="<table>\n"; // th case if (!empty($this->header)) { $header=$this->header; $string.="\t<tr>\n"; for ($i=0; $i<$cellsX; $i++){ $string.="\t\t<th>"; if (isset($header[$i])) $string.=$header[$i]; $string.="</th>\n"; } $string.="\t</tr>\n"; } // td case $rowNumber=0; foreach($this->data as $row){ // per row $string.="\t<tr"; // Add row classes if (!empty($this->classesRows)) { if (!empty($this->classesRows[$rowNumber])) // add row class $string.=" class='" . $this->classesRows[$rowNumber] . "'"; if ($rowNumber<($cellsY-1)) $rowNumber++; } $string.=">\n"; for ($i=0; $i<$cellsX; $i++) // per cell { $string.="\t\t<td"; // Add cell classes if (!empty($this->classesCells)) if (!empty($this->classesCells[$rowNumber][$i])) // add cell class $string.=" class='" . $this->classesCells[$rowNumber][$i] . "'"; $string.=">"; if (isset($row[$i])) $string.=$row[$i]; $string.="</td>\n"; } $string.="\t</tr>\n"; } $string.='</table>'; return $string; } /* ------------------------- SETTERS & GETTERS -------------------------------*/ /* Get length of longest row * * */ public function getXlength() { return $this->biggestRow; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B1hkW
function name:  (null)
number of ops:  17
compiled vars:  !0 = $table
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   NEW                                              $1      'Table'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
    3     3        INIT_METHOD_CALL                                         !0, 'addRecord'
          4        SEND_VAL_EX                                              'Jesus'
          5        SEND_VAL_EX                                              'Blue'
          6        SEND_VAL_EX                                              'Fish'
          7        DO_FCALL                                      0          
    4     8        INIT_METHOD_CALL                                         !0, 'addRecord'
          9        SEND_VAL_EX                                              'Tesla'
         10        SEND_VAL_EX                                              ''
         11        SEND_VAL_EX                                              'Coil'
         12        DO_FCALL                                      0          
    5    13        INIT_METHOD_CALL                                         !0, 'tableToHTML'
         14        DO_FCALL                                      0  $6      
         15        ECHO                                                     $6
  173    16      > RETURN                                                   1

Class Table:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B1hkW
function name:  __construct
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   ASSIGN_OBJ                                               'data'
          1        OP_DATA                                                  <array>
   18     2        ASSIGN_OBJ                                               'header'
          3        OP_DATA                                                  <array>
   20     4        ASSIGN_OBJ                                               'classesCells'
          5        OP_DATA                                                  <array>
   21     6        ASSIGN_OBJ                                               'classesRows'
          7        OP_DATA                                                  <array>
   23     8        ASSIGN_OBJ                                               'biggestRow'
          9        OP_DATA                                                  0
   24    10      > RETURN                                                   null

End of function __construct

Function makeheader:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/B1hkW
function name:  makeHeader
number of ops:  11
compiled vars:  !0 = $rowLength
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   FUNC_GET_ARGS                                    ~2      
          1        ASSIGN_OBJ                                               'header'
          2        OP_DATA                                                  ~2
   37     3        FUNC_NUM_ARGS                                    ~3      
          4        ASSIGN                                                   !0, ~3
   38     5        FETCH_OBJ_R                                      ~5      'biggestRow'
          6        IS_SMALLER                                               ~5, !0
          7      > JMPZ                                                     ~6, ->10
   39     8    >   ASSIGN_OBJ                                               'biggestRow'
          9        OP_DATA                                                  !0
   40    10    > > RETURN                                                   null

End of function makeheader

Function addrecord:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/B1hkW
function name:  addRecord
number of ops:  13
compiled vars:  !0 = $row, !1 = $rowLength
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   FUNC_GET_ARGS                                    ~2      
          1        ASSIGN                                                   !0, ~2
   50     2        FETCH_OBJ_W                                      $4      'data'
          3        ASSIGN_DIM                                               $4
          4        OP_DATA                                                  !0
   53     5        COUNT                                            ~6      !0
          6        ASSIGN                                                   !1, ~6
   54     7        FETCH_OBJ_R                                      ~8      'biggestRow'
          8        IS_SMALLER                                               ~8, !1
          9      > JMPZ                                                     ~9, ->12
   55    10    >   ASSIGN_OBJ                                               'biggestRow'
         11        OP_DATA                                                  !1
   56    12    > > RETURN                                                   null

End of function addrecord

Function addclassx:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 7
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 7
Branch analysis from position: 14
Branch analysis from position: 7
filename:       /in/B1hkW
function name:  addClassX
number of ops:  15
compiled vars:  !0 = $classname, !1 = $x, !2 = $rowsN, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   66     2        FETCH_OBJ_R                                      ~4      'data'
          3        COUNT                                            ~5      ~4
          4        ASSIGN                                                   !2, ~5
   67     5        ASSIGN                                                   !3, 0
          6      > JMP                                                      ->12
   68     7    >   FETCH_OBJ_W                                      $8      'classesCells'
          8        FETCH_DIM_W                                      $9      $8, !3
          9        ASSIGN_DIM                                               $9, !1
         10        OP_DATA                                                  !0
   67    11        PRE_INC                                                  !3
         12    >   IS_SMALLER                                               !3, !2
         13      > JMPNZ                                                    ~12, ->7
   70    14    > > RETURN                                                   null

End of function addclassx

Function addclassrowevery:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 5
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 5
Branch analysis from position: 13
Branch analysis from position: 5
filename:       /in/B1hkW
function name:  addClassRowEvery
number of ops:  14
compiled vars:  !0 = $classname, !1 = $n, !2 = $startingRow, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   81     3        ASSIGN                                                   !3, !2
          4      > JMP                                                      ->9
   82     5    >   FETCH_OBJ_W                                      $5      'classesRows'
          6        ASSIGN_DIM                                               $5, !3
          7        OP_DATA                                                  !0
   81     8        ASSIGN_OP                                     1          !3, !1
          9    >   FETCH_OBJ_R                                      ~8      'data'
         10        COUNT                                            ~9      ~8
         11        IS_SMALLER                                               !3, ~9
         12      > JMPNZ                                                    ~10, ->5
   84    13    > > RETURN                                                   null

End of function addclassrowevery

Function showtableinfo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B1hkW
function name:  showTableInfo
number of ops:  27
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   ECHO                                                     '%3Cpre%3E'
   92     1        ECHO                                                     '%3Cb%3ENumber+of+cells+in+longest+row%3A%3C%2Fb%3E%3Cbr+%2F%3E'
   93     2        FETCH_OBJ_R                                      ~0      'biggestRow'
          3        CONCAT                                           ~1      ~0, '%3Cbr+%2F%3E'
          4        ECHO                                                     ~1
   94     5        ECHO                                                     '%3Cbr+%2F%3E%3Cb%3EHeader%3A%3C%2Fb%3E%3Cbr+%2F%3E'
   95     6        INIT_FCALL                                               'print_r'
          7        FETCH_OBJ_R                                      ~2      'header'
          8        SEND_VAL                                                 ~2
          9        DO_ICALL                                                 
   96    10        ECHO                                                     '%3Cbr+%2F%3E%3Cb%3EData%3A%3C%2Fb%3E%3Cbr+%2F%3E'
   97    11        INIT_FCALL                                               'print_r'
         12        FETCH_OBJ_R                                      ~4      'data'
         13        SEND_VAL                                                 ~4
         14        DO_ICALL                                                 
   98    15        ECHO                                                     '%3Cbr+%2F%3E%3Cb%3ERow+classes%3A%3C%2Fb%3E%3Cbr+%2F%3E'
   99    16        INIT_FCALL                                               'print_r'
         17        FETCH_OBJ_R                                      ~6      'classesRows'
         18        SEND_VAL                                                 ~6
         19        DO_ICALL                                                 
  100    20        ECHO                                                     '%3Cbr+%2F%3E%3Cb%3ECell+classes%3A%3C%2Fb%3E%3Cbr+%2F%3E'
  101    21        INIT_FCALL                                               'print_r'
         22        FETCH_OBJ_R                                      ~8      'classesCells'
         23        SEND_VAL                                                 ~8
         24        DO_ICALL                                                 
  102    25        ECHO                                                     '%3C%2Fpre%3E'
  103    26      > RETURN                                                   null

End of function showtableinfo

Function tabletohtml:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 24
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 14
Branch analysis from position: 23
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 74
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 74
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 45
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 41
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 45
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
2 jumps found. (Code = 44) Position 1 = 72, Position 2 = 48
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 63
Branch analysis from position: 52
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 63
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 68
Branch analysis from position: 66
2 jumps found. (Code = 44) Position 1 = 72, Position 2 = 48
Branch analysis from position: 72
Branch analysis from position: 48
Branch analysis from position: 68
Branch analysis from position: 63
Branch analysis from position: 63
Branch analysis from position: 45
Branch analysis from position: 41
Branch analysis from position: 45
Branch analysis from position: 74
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 74
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 14
Branch analysis from position: 23
Branch analysis from position: 14
Branch analysis from position: 19
Branch analysis from position: 24
filename:       /in/B1hkW
function name:  tableToHTML
number of ops:  78
compiled vars:  !0 = $cellsY, !1 = $cellsX, !2 = $string, !3 = $header, !4 = $i, !5 = $rowNumber, !6 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  112     0  E >   FETCH_OBJ_R                                      ~7      'data'
          1        COUNT                                            ~8      ~7
          2        ASSIGN                                                   !0, ~8
  113     3        FETCH_OBJ_R                                      ~10     'biggestRow'
          4        ASSIGN                                                   !1, ~10
  114     5        ASSIGN                                                   !2, '%3Ctable%3E%0A'
  116     6        ISSET_ISEMPTY_PROP_OBJ                           ~13     'header'
          7        BOOL_NOT                                         ~14     ~13
          8      > JMPZ                                                     ~14, ->24
  117     9    >   FETCH_OBJ_R                                      ~15     'header'
         10        ASSIGN                                                   !3, ~15
  118    11        ASSIGN_OP                                     8          !2, '%09%3Ctr%3E%0A'
  119    12        ASSIGN                                                   !4, 0
         13      > JMP                                                      ->21
  120    14    >   ASSIGN_OP                                     8          !2, '%09%09%3Cth%3E'
  121    15        ISSET_ISEMPTY_DIM_OBJ                         0          !3, !4
         16      > JMPZ                                                     ~20, ->19
  122    17    >   FETCH_DIM_R                                      ~21     !3, !4
         18        ASSIGN_OP                                     8          !2, ~21
  123    19    >   ASSIGN_OP                                     8          !2, '%3C%2Fth%3E%0A'
  119    20        PRE_INC                                                  !4
         21    >   IS_SMALLER                                               !4, !1
         22      > JMPNZ                                                    ~25, ->14
  125    23    >   ASSIGN_OP                                     8          !2, '%09%3C%2Ftr%3E%0A'
  128    24    >   ASSIGN                                                   !5, 0
  129    25        FETCH_OBJ_R                                      ~28     'data'
         26      > FE_RESET_R                                       $29     ~28, ->74
         27    > > FE_FETCH_R                                               $29, !6, ->74
  130    28    >   ASSIGN_OP                                     8          !2, '%09%3Ctr'
  133    29        ISSET_ISEMPTY_PROP_OBJ                           ~31     'classesRows'
         30        BOOL_NOT                                         ~32     ~31
         31      > JMPZ                                                     ~32, ->45
  135    32    >   FETCH_OBJ_IS                                     ~33     'classesRows'
         33        ISSET_ISEMPTY_DIM_OBJ                         1  ~34     ~33, !5
         34        BOOL_NOT                                         ~35     ~34
         35      > JMPZ                                                     ~35, ->41
  136    36    >   FETCH_OBJ_R                                      ~36     'classesRows'
         37        FETCH_DIM_R                                      ~37     ~36, !5
         38        CONCAT                                           ~38     '+class%3D%27', ~37
         39        CONCAT                                           ~39     ~38, '%27'
         40        ASSIGN_OP                                     8          !2, ~39
  137    41    >   SUB                                              ~41     !0, 1
         42        IS_SMALLER                                               !5, ~41
         43      > JMPZ                                                     ~42, ->45
  138    44    >   PRE_INC                                                  !5
  140    45    >   ASSIGN_OP                                     8          !2, '%3E%0A'
  142    46        ASSIGN                                                   !4, 0
         47      > JMP                                                      ->70
  144    48    >   ASSIGN_OP                                     8          !2, '%09%09%3Ctd'
  147    49        ISSET_ISEMPTY_PROP_OBJ                           ~47     'classesCells'
         50        BOOL_NOT                                         ~48     ~47
         51      > JMPZ                                                     ~48, ->63
  148    52    >   FETCH_OBJ_IS                                     ~49     'classesCells'
         53        FETCH_DIM_IS                                     ~50     ~49, !5
         54        ISSET_ISEMPTY_DIM_OBJ                         1  ~51     ~50, !4
         55        BOOL_NOT                                         ~52     ~51
         56      > JMPZ                                                     ~52, ->63
  149    57    >   FETCH_OBJ_R                                      ~53     'classesCells'
         58        FETCH_DIM_R                                      ~54     ~53, !5
         59        FETCH_DIM_R                                      ~55     ~54, !4
         60        CONCAT                                           ~56     '+class%3D%27', ~55
         61        CONCAT                                           ~57     ~56, '%27'
         62        ASSIGN_OP                                     8          !2, ~57
  151    63    >   ASSIGN_OP                                     8          !2, '%3E'
  153    64        ISSET_ISEMPTY_DIM_OBJ                         0          !6, !4
         65      > JMPZ                                                     ~60, ->68
  154    66    >   FETCH_DIM_R                                      ~61     !6, !4
         67        ASSIGN_OP                                     8          !2, ~61
  155    68    >   ASSIGN_OP                                     8          !2, '%3C%2Ftd%3E%0A'
  142    69        PRE_INC                                                  !4
         70    >   IS_SMALLER                                               !4, !1
         71      > JMPNZ                                                    ~65, ->48
  157    72    >   ASSIGN_OP                                     8          !2, '%09%3C%2Ftr%3E%0A'
  129    73      > JMP                                                      ->27
         74    >   FE_FREE                                                  $29
  160    75        ASSIGN_OP                                     8          !2, '%3C%2Ftable%3E'
  161    76      > RETURN                                                   !2
  162    77*     > RETURN                                                   null

End of function tabletohtml

Function getxlength:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B1hkW
function name:  getXlength
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  170     0  E >   FETCH_OBJ_R                                      ~0      'biggestRow'
          1      > RETURN                                                   ~0
  171     2*     > RETURN                                                   null

End of function getxlength

End of class Table.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.61 ms | 1416 KiB | 15 Q