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 (isset($_GET['id'])) { $id = $_GET['id']; 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>'; } ?>
Output for 8.0.0 - 8.0.12
Fatal error: Uncaught Error: Class "Session" not found in /in/ihbdG:13 Stack trace: #0 {main} thrown in /in/ihbdG on line 13
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25
Fatal error: Uncaught Error: Class 'Session' not found in /in/ihbdG:13 Stack trace: #0 {main} thrown in /in/ihbdG on line 13
Process exited with code 255.
Output for 5.2.3 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Class 'Session' not found in /in/ihbdG on line 13
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.2
<br /> <b>Fatal error</b>: Class 'Session' not found in <b>/in/ihbdG</b> on line <b>13</b><br />
Process exited with code 255.
Output for 4.4.2 - 4.4.9
<br /> <b>Parse error</b>: syntax error, unexpected T_NEW in <b>/in/ihbdG</b> on line <b>14</b><br />
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
<br /> <b>Parse error</b>: parse error, unexpected T_NEW in <b>/in/ihbdG</b> on line <b>14</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error in <b>/in/ihbdG</b> on line <b>14</b><br />
Process exited with code 255.

preferences:
283.52 ms | 401 KiB | 386 Q