3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Outside of the application, e.g. DB or some storage. function addSomeIntegerFieldToDB($value) { $sql = 'INSERT INTO mytable (integer_field) VALUES (:int)'; // $st = $pdo->prepare($sql); // $st->execute(array('int' => $value); echo "executing query: ".str_replace(':int', $value, $sql)." \n"; } // the application: global $conditional; class X { public function doSmth($id) { // Assertion will run independently of $conditional, thus preventing a wrong $id to be passed to ->doSmth() method assert(is_int($id), new \InvalidArgumentException('wot ar y doin')); global $conditional; if ($conditional) { addSomeIntegerFieldToDB($id); } } } $conditional = true; $obj = new X; $obj->doSmth(13); $obj->doSmth('x'); $obj->doSmth(13);

preferences:
50.49 ms | 402 KiB | 5 Q