3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * renders tables using json output from API call * * PHP version 5 * * @author Matt Scaperoth <mscapero@gwu.edu> * */ ?> <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> <style> .center-wrapper .panel-pane{ margin-bottom:0; } </style> <h2>GW Lecture Capture Locations</h2> <p>All lecture capture classrooms record instructor audio and presentation materials. Select classrooms are also equipped with a camera to record video of the instructor, along with the audio and presentation materials.</p> <br /> <?php //================================== // MAIN: // edit variables for program here //================================== $API_url = "http://classrooms.at.gwu.edu"; $room_feature_type = "capabilities"; $room_feature_name = "lecture capture enabled"; //get the data //$json = get_data($API_url, $room_feature_type, $room_feature_name); //sort by room number //usort($json, "cmp"); // DATA ORGANIZATION //display names of campuses $foggy_title = "Foggy Bottom Campus"; $mvc_title = "Mount Vernon Campus"; $virginia_title = "Virginia Science and Technology Campus"; //links for pdf maps $foggy_bottom_map_url = "https://acadtech.gwu.edu/files/downloads/Foggy%20Bottom%20Campus%20Map.pdf"; $mount_vernon_map_url = "https://acadtech.gwu.edu/files/downloads/Mount%20Vernon%20Campus%20Map.pdf"; $virginia_map_url = "https://acadtech.gwu.edu/files/downloads/VSTC%20Map.pdf"; //campus details $foggy_info = array( "title" => $foggy_title, "link" => "foggy-bottom", "map" => $foggy_bottom_map_url, "buildings" => array(), "html" => null, "notes" => null, ); $mvc_info = array( "title" => $mvc_title, "link" => "mvc", "map" => $foggy_bottom_map_url, "buildings" => array(), "html" => null, "notes" => "*Equipment in Ames Hall is supported by MVC Faculty & Instructional Support, (202) 242-6670.", ); $vstc_info = array( "title" => $virginia_title, "link" => "vstc", "map" => $foggy_bottom_map_url, "buildings" => array(), "html" => null, "notes" => null, ); //we will store each table as it is created $campuses = array("FB" => $foggy_info, "MVC" => $mvc_info, "VSTC" => $vstc_info); //start by building the tables for each campus foreach ($campuses as $campus_info => $value) { //$campuses[$campus_info]["html"] = open_table($campuses[$campus_info]); } //-------------------------- // Process and Render Data //-------------------------- //generate the html tables using json data and campus info //$campuses = build_html($json, $campuses, $room_feature_name); //clean up the output with styles and sorting //$campuses = clean_output($campuses); //return the output to the screen //render($campuses); //================================== // FUNCTIONS // Here you can edit the output // and the data sources //==================================== //-------------------------- // Output Functions //-------------------------- /** * opens html table * @param array() $campus_info * @return String */ function open_table($campus_info) { return htmlspecialchars("<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width: 100%;\"><tbody>" . "<thead> <tr> <th colspan=\"3\" scope=\"col\" style=\"border-color: rgb(51, 51, 51); background-color: rgb(231, 244, 246);text-align:center\"> {$campus_info['title']} (<a href=\"{$campus_info['link']}\" target=\"_blank\">map</a>) </th> </tr> </thead>"); } /** * (b) closes html table * @param mixed $campus_info */ function close_table($campus_info) { return htmlspecialchars("</tr></tbody></table>"); } /** * cleans upthe output by providing <p> wrappers and adding notes * @param mixed $campuses * @return mixed function clean_output($campuses) { foreach ($campuses as $campus_info => $value) { //set pointers to manipulate $campuses directly $campus_info_ptr = &$campuses[$campus_info]; $html_ptr = &$campus_info_ptr["html"]; $notes_ptr = &$campus_info_ptr["notes"]; //close the tables $html_ptr .= close_table($campus_info_ptr); //wrap tables in <p> tags $html_ptr = "<p>" . $html_ptr . "</p>"; //add notes at the end of output $html_ptr .= $notes_ptr; //sorting ksort($campus_info_ptr["buildings"]); } return $campuses; } */ /** * used by uksort on json to sort buildings and rooms function cmp($a, $b) { if ($a->building_name == $b->building_name) { if ($a->room_num == $b->room_num) { return 0; } //do a second-degree sort on room number if (is_numeric($a->room_num) && !is_numeric($b->room_num)) { return 1; } else if (!is_numeric($a->room_num) && is_numeric($b->room_num)) { return -1; } else { return ($a->room_num < $b->room_num) ? -1 : 1; } } else { //if buildings are different, sort on building name return ($a->building_name < $b->building_name) ? -1 : 1; } } */ /** * echos the html from the room info * @param mixed $campuses array of campus information */ function render($campuses) { if (is_array($campuses) || is_object($campuses)) { foreach ($campuses as $campus_info) { echo $campuses[$campus_info]["html"]; } } } /** * generates tables from json data * @param array(Objects) $json * @param array(array()) $campuses * @return mixed */ function build_html($json, $campuses, $room_feature_name) { //used to filter out unwanted parts of name in building $replace_this_string1 = "MVC-"; $replace_this_string2 = "VSTC-"; $replace_these_strings = "/^$replace_this_string1|^$replace_this_string2/"; //regex //track the number of rooms so far $number_of_rooms = 0; foreach ($json as $item) { $campus = $item->campus; $campus_info = &$campuses[$campus]; //room information $room_num = $item->room_num; $room_class = ""; $room_link = ""; //use this variable to put commas between rooms $room_separator = ""; //trim unwanted characters from building name $item->building_name = preg_replace($replace_these_strings, '', $item->building_name); //start new building var $building = array( "name" => $item->building_name, "old_name" => $item->building_name, ); //format building name to make it look nice $building = format_building_name($building); //if building is not a part of campus info do work if (!in_array($building["name"], $campus_info["buildings"])) { //new building means new room count $number_of_rooms = 0; //if not first building, end previous cell and row if (!empty($campus_info["buildings"])) { //close previous cell $campus_info["html"] .= htmlspecialchars("</td>"); //close previous row $campus_info["html"] .= htmlspecialchars("</tr>"); } //add building to list of buildings array_push($campus_info["buildings"], $building["name"]); //create new row. will be closed in next building or at the end $campus_info["html"] .= htmlspecialchars("<tr>"); //create new cell $campus_info["html"] .= htmlspecialchars("<td width=\"50%\" id=\"{$building['name']}\">"); $campus_info["html"] .= htmlspecialchars($building["name"]); //close building cell $campus_info["html"] .= htmlspecialchars("</td>"); //start new cell for room numbers $campus_info["html"] .= htmlspecialchars("<td width=\"50%\">"); } //if it's not the first room, ad a comma before it $room_separator = $number_of_rooms > 0 ? ", " : ""; //for each building add the rooms //check to see if room has video and add camera icon if necessary if (stripos($room_feature_name, "lecture capture") !== false) { if (in_arrayi("(camera)", $item->equipment)) { $room_class = "fa fa-video-camera"; } } //only grab first "word" in building name to make for easier queries $building_name_no_spaces = strtok($building["old_name"], ' '); //generate the link for the website $room_link = "/location?building=$building_name_no_spaces&room=$room_num"; $campus_info["html"] .= htmlspecialchars("$room_separator<a href=\"$room_link\" target=\"_blank\">$room_num <i class=\"$room_class\"></i></a>"); $number_of_rooms++; } return $campuses; } /** * Case-insensitive in_array() wrapper. * https://gist.github.com/sepehr/6351397 * * @param mixed $needle Value to seek. * @param array $haystack Array to seek in. * * @return bool */ function in_arrayi($needle, $haystack) { foreach ($haystack as $value) { if (stripos($value, $needle) !== false) { return true; } } return false; } /** * formats building name to be more user friendly * @param array() $building * @return array() formatted $building */ function format_building_name($building) { switch ($building["name"]) { case "Ames": $building["old_name"] = $building["name"]; $building["name"] .= ' Hall*'; break; case "Corcoran": case "Discovery": case "Duques": case "Exploration": case "Funger": case "Innovation": case "Phillips": case "Rome": $building["old_name"] = $building["name"]; $building["name"] .= ' Hall'; break; case "Government": $building["old_name"] = $building["name"]; $building["name"] = 'Hall of ' . $building["name"]; break; case "GWSPH": $building["old_name"] = "GWSPH"; $building["name"] = "Milken Institute School of Public Health"; break; case "MPA": $building["old_name"] = "MPA"; $building["name"] = "Media and Public Affairs Building"; break; case "SEH": $building["old_name"] = "SEH"; $building["name"] = "Science and Engineering Hall"; break; } return $building; } //-------------------------- // json Functions //-------------------------- /** * uses API to get classroom data * @param String $room_feature_type item type: capabilities, software, quickcodes, etc. * @param String $room_feature_name name of item to look for: lecture capture, video conferencing, etc. * @return array(Objects) */ function get_data($API_url, $room_feature_type, $room_feature_name) { //get all capabilities available $capabilities = curl("$API_url/api/campuses/$room_feature_type"); //get the ids of the capabilities with lecture capture in the name $ids_of_lect_capt = searchJson($capabilities, "name", $room_feature_name); $rooms = array(); //get the ids of the rooms with lecture capture foreach ($ids_of_lect_capt as $cap_id) { $ids_of_rooms = curl("$API_url/api/campuses/capability/$cap_id/rooms"); foreach ($ids_of_rooms as $room) { array_push($rooms, $room->id); } } $json = array(); //get the details for each of the rooms and add it into one object foreach ($rooms as $room_id) { $room_details = curl("$API_url/api/campuses/building/room/$room_id/details"); array_push($json, $room_details); } return $json; } /** * performs curl * @param String $url * @return array(Objecs) */ function curl($url) { //echo '<h3 class="Legend">PHP</h3>'; // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // $output contains the output string $output = curl_exec($ch); // var_dump(curl_error($ch)); //decode json into array $json = json_decode($output); // close curl resource to free up system resources curl_close($ch); return $json; } /** * returns array of values found * @param array(Objects) $obj * @param String $field * @param String $value * @return array() */ function searchJson($obj, $field, $value) { $result = array(); if (is_array($obj) || is_object($obj)) { foreach ($obj as $item) { foreach ($item as $child) { if (isset($item->id) && stripos($child . $field, $value) !== false) { array_push($result, $item->id); } } } } return $result; } //-------------------------- // Debugging Functions //-------------------------- //formatted output of variable function test($var) { echo "<pre>"; var_dump($var); echo "</pre>"; } ?>

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0040.01418.18
8.3.50.0100.00716.61
8.3.40.0150.00018.85
8.3.30.0070.00718.67
8.3.20.0030.00518.86
8.3.10.0040.00421.53
8.3.00.0040.00420.84
8.2.180.0140.00418.18
8.2.170.0150.00022.96
8.2.160.0150.00019.32
8.2.150.0050.00324.18
8.2.140.0040.00424.66
8.2.130.0000.00821.08
8.2.120.0080.00026.35
8.2.110.0060.00321.04
8.2.100.0090.00318.18
8.2.90.0040.00419.34
8.2.80.0080.00017.97
8.2.70.0080.00017.61
8.2.60.0030.00618.05
8.2.50.0060.00318.10
8.2.40.0030.00522.18
8.2.30.0050.00220.73
8.2.20.0060.00617.74
8.2.10.0030.00518.11
8.2.00.0060.00318.24
8.1.280.0110.00425.92
8.1.270.0030.00523.86
8.1.260.0050.00328.09
8.1.250.0070.00028.09
8.1.240.0000.00923.80
8.1.230.0040.00717.58
8.1.220.0050.00317.90
8.1.210.0030.00618.77
8.1.200.0030.00717.48
8.1.190.0000.00817.48
8.1.180.0000.00818.10
8.1.170.0000.00818.78
8.1.160.0060.00319.16
8.1.150.0000.00718.94
8.1.140.0060.00319.68
8.1.130.0030.00517.69
8.1.120.0000.00717.50
8.1.110.0040.00417.55
8.1.100.0080.00017.59
8.1.90.0040.00417.46
8.1.80.0060.00317.64
8.1.70.0030.00317.45
8.1.60.0030.00617.72
8.1.50.0060.00317.59
8.1.40.0060.00317.64
8.1.30.0030.00617.67
8.1.20.0040.00417.62
8.1.10.0000.00817.44
8.1.00.0030.00917.53
8.0.300.0060.00318.77
8.0.290.0060.00316.88
8.0.280.0000.00718.52
8.0.270.0070.00017.38
8.0.260.0000.00717.28
8.0.250.0000.00817.05
8.0.240.0030.00517.00
8.0.230.0000.00717.08
8.0.220.0040.00417.05
8.0.210.0080.00016.89
8.0.200.0080.00016.96
8.0.190.0040.00417.09
8.0.180.0050.00217.08
8.0.170.0040.00416.93
8.0.160.0080.00417.04
8.0.150.0070.00016.83
8.0.140.0050.00216.98
8.0.130.0030.00313.46
8.0.120.0060.00316.87
8.0.110.0040.00416.98
8.0.100.0030.00616.89
8.0.90.0040.00417.03
8.0.80.0080.00817.06
8.0.70.0050.00217.07
8.0.60.0020.00517.13
8.0.50.0000.00716.94
8.0.30.0120.00517.32
8.0.20.0120.01017.40
8.0.10.0040.00416.91
8.0.00.0090.00916.85
7.4.330.0030.00315.08
7.4.320.0070.00016.76
7.4.300.0030.00316.60
7.4.290.0030.00316.64
7.4.280.0040.00416.61
7.4.270.0030.00316.71
7.4.260.0000.00716.50
7.4.250.0040.00416.57
7.4.240.0100.00016.70
7.4.230.0040.00416.38
7.4.220.0060.02016.68
7.4.210.0040.01116.68
7.4.200.0000.00816.76
7.4.160.0090.01216.50
7.4.150.0100.01017.40
7.4.140.0130.00717.86
7.4.130.0120.00616.71
7.4.120.0080.01016.54
7.4.110.0090.00816.81
7.4.100.0110.01416.48
7.4.90.0130.00416.61
7.4.80.0100.01019.39
7.4.70.0140.00316.57
7.4.60.0130.00316.71
7.4.50.0060.00916.75
7.4.40.0160.00816.48
7.4.30.0130.01016.67
7.4.00.0070.01015.16
7.3.330.0030.00313.30
7.3.320.0030.00313.47
7.3.310.0040.00316.36
7.3.300.0000.00716.35
7.3.290.0070.01116.46
7.3.280.0090.01116.48
7.3.270.0070.01017.40
7.3.260.0180.00016.53
7.3.250.0080.01316.54
7.3.240.0100.00816.65
7.3.230.0160.00316.66
7.3.210.0100.00716.57
7.3.200.0120.00816.55
7.3.190.0100.00716.31
7.3.180.0090.00616.40
7.3.170.0140.00616.41
7.3.160.0040.01316.46
7.3.10.0040.00816.43
7.3.00.0060.00316.31
7.2.330.0140.00416.72
7.2.320.0170.00716.47
7.2.310.0120.00616.49
7.2.300.0100.01316.50
7.2.290.0130.00316.72
7.2.130.0030.00917.05
7.2.120.0060.00616.66
7.2.110.0110.00716.34
7.2.100.0090.00616.77
7.2.90.0060.00616.80
7.2.80.0000.01616.85
7.2.70.0060.01116.39
7.2.60.0140.00716.77
7.2.50.0000.01816.77
7.2.40.0100.00316.55
7.2.30.0000.01216.88
7.2.20.0060.00617.10
7.2.10.0060.00616.52
7.2.00.0060.00518.10
7.1.250.0070.01115.90
7.1.200.0040.00715.68
7.1.100.0000.01317.69
7.1.70.0000.01017.04
7.1.60.0100.01620.41
7.1.00.0000.08022.40
7.0.200.0120.00916.82
7.0.100.0200.06720.16
7.0.90.0130.06020.08
7.0.80.0100.08320.02
7.0.70.0100.05020.03
7.0.60.0100.08020.09
7.0.50.0030.07720.49
7.0.40.0130.07720.08
7.0.30.0130.08020.11
7.0.20.0070.07720.16
7.0.10.0000.09020.11
7.0.00.0030.08720.09
5.6.280.0070.07020.99
5.6.250.0100.07720.85
5.6.240.0000.08320.71
5.6.230.0030.07720.73
5.6.220.0170.07320.59
5.6.210.0030.07720.65
5.6.200.0070.07321.08
5.6.190.0130.08021.20
5.6.180.0130.07721.23
5.6.170.0030.06321.09
5.6.160.0130.06721.08
5.6.150.0070.08021.08
5.6.140.0030.05321.20
5.6.130.0070.08021.17
5.6.120.0300.07721.01
5.6.110.0000.08021.14
5.6.100.0000.04721.01
5.6.90.0170.07320.98
5.6.80.0030.05320.50
5.6.70.0070.07020.61
5.6.60.0070.08320.46
5.6.50.0100.06320.50
5.6.40.0130.07720.41
5.6.30.0030.08320.46
5.6.20.0070.08020.38
5.6.10.0100.07720.56
5.6.00.0070.05020.49
5.5.380.0070.06320.51
5.5.370.0070.08020.56
5.5.360.0100.07720.48
5.5.350.0030.08320.54
5.5.340.0230.06020.98
5.5.330.0230.06320.95
5.5.320.0170.08020.91
5.5.310.0100.07720.90
5.5.300.0100.07320.98
5.5.290.0000.09021.00
5.5.280.0100.07320.71
5.5.270.0030.06720.82
5.5.260.0070.06720.89
5.5.250.0100.03720.77
5.5.240.0000.08720.29
5.5.230.0030.08320.38
5.5.220.0030.07020.34
5.5.210.0070.08020.18
5.5.200.0300.04720.06
5.5.190.0000.05320.29
5.5.180.0200.07320.29
5.5.160.0100.07020.25
5.5.150.0100.07720.33
5.5.140.0000.08020.30
5.5.130.0000.07720.29
5.5.120.0030.05720.29
5.5.110.0130.06020.19
5.5.100.0070.04720.05
5.5.90.0070.04320.14
5.5.80.0070.07720.18
5.5.70.0100.07020.21
5.5.60.0070.07720.07
5.5.50.0000.08320.03
5.5.40.0000.08020.07
5.5.30.0070.07020.22
5.5.20.0130.07320.12
5.5.10.0170.06720.16
5.5.00.0100.04320.07

preferences:
74.18 ms | 401 KiB | 5 Q