3v4l.org

run code in 300+ PHP versions simultaneously
#!/usr/bin/php <?php // input parameters $api_key = "YOUR_API_KEY"; // default arguments $backup_tag = "daily"; $num_of_backups = 3; // sunday = weekly, 1st day of month = monthly if(intval(date('w')) == 0) $backup_tag = "weekly"; if(intval( date('d')) == 1) $backup_tag = "monthly"; $api_addr = "https://api.vultr.com/v1/"; $curl = "curl -s -H 'API-Key: " . $api_key . "' " . $api_addr; $host = trim(shell_exec("hostname")); $ip = trim(shell_exec("hostname -I")); // find a subid that matches the current ip $json = shell_exec( $curl . "server/list" ); $obj = json_decode($json,true); $subid = 0; foreach ($obj as $k => $v) { if( $v['main_ip'] == $ip ){ $subid = $k; break; } } if($subid == 0){ echo "Couldn't find a Vultr instance with ip " . $ip; exit(1); } // find old snapshot(s) to remove $json = shell_exec( $curl . "snapshot/list" ); $obj = json_decode($json,true); // filter non-backup snapshots $obj = array_filter($obj, function ($x){ return strpos($x['description'], $GLOBALS['ip'] . "-" . $GLOBALS['backup_tag']) !== false; }); // reverse sort by creation time usort($obj, function($x, $y){ return $y['date_created'] <=> $x['date_created']; }); // delete oldest snapshot(s) for($i=0;$i<count($obj);$i++){ if($i >= $num_of_backups-1){ $id = $obj[$i]['SNAPSHOTID']; $cmd = $curl . "snapshot/destroy"; shell_exec( $cmd . " --data 'SNAPSHOTID=" . $id . "'" ); } } // create new snapshot $cmd = $curl . "snapshot/create"; $cmd .= " --data 'SUBID=" . $subid . "'"; $cmd .= " --data 'description=" . $host . "-" . $ip . "-" . $backup_tag . "'"; shell_exec( $cmd ); ?>
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: shell_exec(): Unable to execute 'hostname' in /in/tI4uO on line 18 Warning: shell_exec(): Unable to execute 'hostname -I' in /in/tI4uO on line 19 Warning: shell_exec(): Unable to execute 'curl -s -H 'API-Key: YOUR_API_KEY' https://api.vultr.com/v1/server/list' in /in/tI4uO on line 22 Warning: foreach() argument must be of type array|object, null given in /in/tI4uO on line 26 Couldn't find a Vultr instance with ip
Process exited with code 1.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: shell_exec(): Unable to execute 'hostname' in /in/tI4uO on line 18 Warning: shell_exec(): Unable to execute 'hostname -I' in /in/tI4uO on line 19 Warning: shell_exec(): Unable to execute 'curl -s -H 'API-Key: YOUR_API_KEY' https://api.vultr.com/v1/server/list' in /in/tI4uO on line 22 Warning: foreach() argument must be of type array|object, null given in /in/tI4uO on line 26 Couldn't find a Vultr instance with ip
Process exited with code 1.
Output for 7.2.29 - 7.2.33, 7.3.16 - 7.3.33, 7.4.4 - 7.4.33
Warning: shell_exec(): Unable to execute 'hostname' in /in/tI4uO on line 18 Warning: shell_exec(): Unable to execute 'hostname -I' in /in/tI4uO on line 19 Warning: shell_exec(): Unable to execute 'curl -s -H 'API-Key: YOUR_API_KEY' https://api.vultr.com/v1/server/list' in /in/tI4uO on line 22 Warning: Invalid argument supplied for foreach() in /in/tI4uO on line 26 Couldn't find a Vultr instance with ip
Process exited with code 1.
Output for 7.0.0 - 7.0.31, 7.1.0 - 7.1.25, 7.2.0 - 7.2.13, 7.3.0 - 7.3.1, 7.4.0
Warning: Invalid argument supplied for foreach() in /in/tI4uO on line 26 Couldn't find a Vultr instance with ip
Process exited with code 1.
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
Parse error: syntax error, unexpected '>' in /in/tI4uO on line 49
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/tI4uO on line 43
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, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/tI4uO on line 43
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/tI4uO on line 43
Process exited with code 255.

preferences:
303.33 ms | 401 KiB | 416 Q