3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Social network site where you can see a list of users and "follow" them. */ // setup the `Db` object: //require_once 'lib/db.php'; // setup the `Session` object: //require_once 'lib/session.php'; // if (!Session::isLoggedIn()) { // throw new Exception('You must be logged in to view this page'); // } // display a user as html function displayUser ($user, $tag='div') { $full_name = $user->first_name . ' ' . $user->last_name; // get user's avatar from 3rd-party avatar service, based on which environment we're running in if (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) { $avatar_data = json_decode(file_get_contents('http://dev.avatars.com/user/' . $user->id)); } else { $avatar_data = json_decode(file_get_contents('http://www.avatars.com/user/' . $user->id)); } ?> <<?= $tag ?> class="user"> <a href="/users/<?= $user->id ?>"> <img src="<?= $avatar_data->url ?>"> <?= $full_name ?> </a> <a href="?follow=<?= $user->id ?>">Follow</a> </<?= $tag ?>> <?php } // check that a user ID is valid (must be a number) function validateUserId ($id) { if (!is_numeric($id)) { // log invalid user IDs to help us track down bugs. error_log("Invalid user id: {$id}"); return false; } return true; } // load the user's custom theme config // $theme_filename = dirname(__FILE__) . 'themes/' . Session::getLoggedInUser()->theme_id . '.php'; // if (file_exists($theme_filename)) { // require $theme_filename; // } // show all users if (isset($_GET['show-all'])) { echo '<ul>'; foreach (Db::getUsers() as $user) { displayUser($user, 'li'); } echo '</ul>'; } // show a single user else if (true) { $id = 'john'; if (!validateUserId($id)) throw new Exception('Please select a valid user ID'); $user = Db::getUserById($id); if (!$user) throw new Exception('Sorry, user not found'); displayUser($user); } // follow a user else if (isset($_GET['follow'])) { $id = $_GET['follow']; if (!validateUserId($id)) throw new Exception('Please select a valid user ID'); $user_to_follow = Db::getUserById($id); if (!$user_to_follow) throw new Exception('Sorry, user not found'); $current_user = Session::getLoggedInUser(); try { $current_user->follow($user_to_follow); } catch (Exception $e) { throw new Exception('Unable to follow at this time'); } // inform the user that they have a new follower. mail( $user_to_follow->email, 'You have a new follower!', "{$current_user->username} is now following you.", 'From: notifications@awesome-social-network.com' ); // also update the activity feed, so users can see what others are doing on the site. Db::updateActivityFeed(array( 'action' => 'follow', 'from' => $current_user->id, 'to' => $user_to_follow->id, 'timestamp' => time(), )); echo 'Followed! <a href="/">Return to home</a>'; } ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 16
Branch analysis from position: 3
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 13
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 100
Branch analysis from position: 100
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 41
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 27
Branch analysis from position: 23
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 37
Branch analysis from position: 33
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 100
Branch analysis from position: 100
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 100
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 56
Branch analysis from position: 52
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 66
Branch analysis from position: 62
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 100
Found catch point at position: 73
Branch analysis from position: 73
2 jumps found. (Code = 107) Position 1 = 74, Position 2 = -2
Branch analysis from position: 74
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/kjuRt
function name:  (null)
number of ops:  101
compiled vars:  !0 = $user, !1 = $id, !2 = $user_to_follow, !3 = $current_user, !4 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   FETCH_IS                                         ~5      '_GET'
          1        ISSET_ISEMPTY_DIM_OBJ                         0          ~5, 'show-all'
          2      > JMPZ                                                     ~6, ->16
   59     3    >   ECHO                                                     '%3Cul%3E'
   60     4        INIT_STATIC_METHOD_CALL                                  'Db', 'getUsers'
          5        DO_FCALL                                      0  $7      
          6      > FE_RESET_R                                       $8      $7, ->13
          7    > > FE_FETCH_R                                               $8, !0, ->13
   61     8    >   INIT_FCALL                                               'displayuser'
          9        SEND_VAR                                                 !0
         10        SEND_VAL                                                 'li'
         11        DO_FCALL                                      0          
   60    12      > JMP                                                      ->7
         13    >   FE_FREE                                                  $8
   63    14        ECHO                                                     '%3C%2Ful%3E'
         15      > JMP                                                      ->100
   66    16    > > JMPZ                                                     <true>, ->41
   67    17    >   ASSIGN                                                   !1, 'john'
   68    18        INIT_FCALL                                               'validateuserid'
         19        SEND_VAR                                                 !1
         20        DO_FCALL                                      0  $11     
         21        BOOL_NOT                                         ~12     $11
         22      > JMPZ                                                     ~12, ->27
         23    >   NEW                                              $13     'Exception'
         24        SEND_VAL_EX                                              'Please+select+a+valid+user+ID'
         25        DO_FCALL                                      0          
         26      > THROW                                         0          $13
   69    27    >   INIT_STATIC_METHOD_CALL                                  'Db', 'getUserById'
         28        SEND_VAR_EX                                              !1
         29        DO_FCALL                                      0  $15     
         30        ASSIGN                                                   !0, $15
   70    31        BOOL_NOT                                         ~17     !0
         32      > JMPZ                                                     ~17, ->37
         33    >   NEW                                              $18     'Exception'
         34        SEND_VAL_EX                                              'Sorry%2C+user+not+found'
         35        DO_FCALL                                      0          
         36      > THROW                                         0          $18
   72    37    >   INIT_FCALL                                               'displayuser'
         38        SEND_VAR                                                 !0
         39        DO_FCALL                                      0          
         40      > JMP                                                      ->100
   75    41    >   FETCH_IS                                         ~21     '_GET'
         42        ISSET_ISEMPTY_DIM_OBJ                         0          ~21, 'follow'
         43      > JMPZ                                                     ~22, ->100
   76    44    >   FETCH_R                      global              ~23     '_GET'
         45        FETCH_DIM_R                                      ~24     ~23, 'follow'
         46        ASSIGN                                                   !1, ~24
   77    47        INIT_FCALL                                               'validateuserid'
         48        SEND_VAR                                                 !1
         49        DO_FCALL                                      0  $26     
         50        BOOL_NOT                                         ~27     $26
         51      > JMPZ                                                     ~27, ->56
         52    >   NEW                                              $28     'Exception'
         53        SEND_VAL_EX                                              'Please+select+a+valid+user+ID'
         54        DO_FCALL                                      0          
         55      > THROW                                         0          $28
   78    56    >   INIT_STATIC_METHOD_CALL                                  'Db', 'getUserById'
         57        SEND_VAR_EX                                              !1
         58        DO_FCALL                                      0  $30     
         59        ASSIGN                                                   !2, $30
   79    60        BOOL_NOT                                         ~32     !2
         61      > JMPZ                                                     ~32, ->66
         62    >   NEW                                              $33     'Exception'
         63        SEND_VAL_EX                                              'Sorry%2C+user+not+found'
         64        DO_FCALL                                      0          
         65      > THROW                                         0          $33
   81    66    >   INIT_STATIC_METHOD_CALL                                  'Session', 'getLoggedInUser'
         67        DO_FCALL                                      0  $35     
         68        ASSIGN                                                   !3, $35
   83    69        INIT_METHOD_CALL                                         !3, 'follow'
         70        SEND_VAR_EX                                              !2
         71        DO_FCALL                                      0          
         72      > JMP                                                      ->78
   84    73  E > > CATCH                                       last         'Exception'
   85    74    >   NEW                                              $38     'Exception'
         75        SEND_VAL_EX                                              'Unable+to+follow+at+this+time'
         76        DO_FCALL                                      0          
         77      > THROW                                         0          $38
   89    78    >   INIT_FCALL                                               'mail'
   90    79        FETCH_OBJ_R                                      ~40     !2, 'email'
         80        SEND_VAL                                                 ~40
   91    81        SEND_VAL                                                 'You+have+a+new+follower%21'
   92    82        FETCH_OBJ_R                                      ~41     !3, 'username'
         83        NOP                                                      
         84        FAST_CONCAT                                      ~42     ~41, '+is+now+following+you.'
         85        SEND_VAL                                                 ~42
   93    86        SEND_VAL                                                 'From%3A+notifications%40awesome-social-network.com'
         87        DO_ICALL                                                 
   97    88        INIT_STATIC_METHOD_CALL                                  'Db', 'updateActivityFeed'
   98    89        INIT_ARRAY                                       ~44     'follow', 'action'
   99    90        FETCH_OBJ_R                                      ~45     !3, 'id'
         91        ADD_ARRAY_ELEMENT                                ~44     ~45, 'from'
  100    92        FETCH_OBJ_R                                      ~46     !2, 'id'
         93        ADD_ARRAY_ELEMENT                                ~44     ~46, 'to'
  101    94        INIT_FCALL                                               'time'
         95        DO_ICALL                                         $47     
         96        ADD_ARRAY_ELEMENT                                ~44     $47, 'timestamp'
         97        SEND_VAL_EX                                              ~44
         98        DO_FCALL                                      0          
  104    99        ECHO                                                     'Followed%21+%3Ca+href%3D%22%2F%22%3EReturn+to+home%3C%2Fa%3E'
  107   100    > > RETURN                                                   1

Function displayuser:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 25
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kjuRt
function name:  displayUser
number of ops:  51
compiled vars:  !0 = $user, !1 = $tag, !2 = $full_name, !3 = $avatar_data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'div'
   19     2        FETCH_OBJ_R                                      ~4      !0, 'first_name'
          3        CONCAT                                           ~5      ~4, '+'
          4        FETCH_OBJ_R                                      ~6      !0, 'last_name'
          5        CONCAT                                           ~7      ~5, ~6
          6        ASSIGN                                                   !2, ~7
   22     7        INIT_FCALL                                               'strpos'
          8        FETCH_R                      global              ~9      '_SERVER'
          9        FETCH_DIM_R                                      ~10     ~9, 'SERVER_NAME'
         10        SEND_VAL                                                 ~10
         11        SEND_VAL                                                 'localhost'
         12        DO_ICALL                                         $11     
         13        TYPE_CHECK                                  1018          $11
         14      > JMPZ                                                     ~12, ->25
   23    15    >   INIT_FCALL                                               'json_decode'
         16        INIT_FCALL                                               'file_get_contents'
         17        FETCH_OBJ_R                                      ~13     !0, 'id'
         18        CONCAT                                           ~14     'http%3A%2F%2Fdev.avatars.com%2Fuser%2F', ~13
         19        SEND_VAL                                                 ~14
         20        DO_ICALL                                         $15     
         21        SEND_VAR                                                 $15
         22        DO_ICALL                                         $16     
         23        ASSIGN                                                   !3, $16
         24      > JMP                                                      ->34
   26    25    >   INIT_FCALL                                               'json_decode'
         26        INIT_FCALL                                               'file_get_contents'
         27        FETCH_OBJ_R                                      ~18     !0, 'id'
         28        CONCAT                                           ~19     'http%3A%2F%2Fwww.avatars.com%2Fuser%2F', ~18
         29        SEND_VAL                                                 ~19
         30        DO_ICALL                                         $20     
         31        SEND_VAR                                                 $20
         32        DO_ICALL                                         $21     
         33        ASSIGN                                                   !3, $21
   30    34    >   ECHO                                                     '++++%3C'
         35        ECHO                                                     !1
         36        ECHO                                                     '+class%3D%22user%22%3E%0A%09%3Ca+href%3D%22%2Fusers%2F'
   31    37        FETCH_OBJ_R                                      ~23     !0, 'id'
         38        ECHO                                                     ~23
         39        ECHO                                                     '%22%3E%0A%09++++%3Cimg+src%3D%22'
   32    40        FETCH_OBJ_R                                      ~24     !3, 'url'
         41        ECHO                                                     ~24
         42        ECHO                                                     '%22%3E%0A%09++++'
   33    43        ECHO                                                     !2
   34    44        ECHO                                                     '%09%3C%2Fa%3E%0A%09%3Ca+href%3D%22%3Ffollow%3D'
   35    45        FETCH_OBJ_R                                      ~25     !0, 'id'
         46        ECHO                                                     ~25
         47        ECHO                                                     '%22%3EFollow%3C%2Fa%3E%0A++++%3C%2F'
   36    48        ECHO                                                     !1
         49        ECHO                                                     '%3E%0A'
   38    50      > RETURN                                                   null

End of function displayuser

Function validateuserid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kjuRt
function name:  validateUserId
number of ops:  14
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   42     1        INIT_FCALL                                               'is_numeric'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4        BOOL_NOT                                         ~2      $1
          5      > JMPZ                                                     ~2, ->12
   44     6    >   INIT_FCALL                                               'error_log'
          7        NOP                                                      
          8        FAST_CONCAT                                      ~3      'Invalid+user+id%3A+', !0
          9        SEND_VAL                                                 ~3
         10        DO_ICALL                                                 
   45    11      > RETURN                                                   <false>
   48    12    > > RETURN                                                   <true>
   49    13*     > RETURN                                                   null

End of function validateuserid

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163 ms | 1415 KiB | 31 Q