3v4l.org

run code in 300+ PHP versions simultaneously
<?php class database{ // @object, The PDO object private $pdo; private $db_connected, $error; private $stmt; public function __construct($host, $dbname, $user, $pass, $char_set="utf8"){ $dsn = 'mysql:dbname='.$dbname.';host='.$host.''; try{ $this->pdo = new PDO($dsn, $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES ".$char_set)); // We can now log any exceptions on Fatal error. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Disable emulation of prepared statements, use REAL prepared statements instead. $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $this->db_connected = true; }catch (PDOException $e){ die("Error! Could not connect to the database.<br />".$e->getMessage()); } } public function __destruct(){ $this->pdo = null; $this->db_connected = false; } public function query($query){ try{ $this->stmt = $this->pdo->prepare($query); }catch(PDOException $e){ if( _safemode_ ){ die("Error! Invalid Query.<br />".$query."<br />".$e->getMessage()); } else{ die("A fatal error occurred."); } } } public function bind($param, $value, $type = null){ if ( is_null($type) ){ switch(true){ case is_int($value): $type = PDO::PARAM_INT; break; case is_bool($value): $type = PDO::PARAM_BOOL; break; case is_null($value): $type = PDO::PARAM_NULL; break; default: $type = PDO::PARAM_STR; } } $this->stmt->bindValue($param, $value, $type); } public function execute(){ try{ $this->stmt->execute(); return true; }catch(PDOException $e){ $this->error = $e->getMessage(); return false; } } public function run(){ $this->execute(); } public function results(){ $this->execute(); return $this->stmt->fetchAll(PDO::FETCH_ASSOC); } public function single(){ $this->execute(); return $this->stmt->fetch(PDO::FETCH_ASSOC); } public function num_rows(){ return $this->stmt->rowCount(); } public function last_insert_id(){ return $this->pdo->lastInsertId(); } public function begin_transaction(){ return $this->pdo->beginTransaction(); } public function end_transaction(){ return $this->pdo->commit(); } public function cancel_transaction(){ return $this->pdo->rollBack(); } public function dump(){ return $this->stmt->debugDumpParams(); } }
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/rTv5K on line 5
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
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/rTv5K on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/rTv5K on line 5
Process exited with code 255.

preferences:
187.25 ms | 401 KiB | 309 Q