3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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; global $withAssert; class X { public function doSmth($id) { global $conditional; global $withAssert; if ($withAssert) { // 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')); } if ($conditional) { addSomeIntegerFieldToDB($id); } } } $obj = new X; $withAssert = false; $conditional = true; // Assert off, conditional on $obj->doSmth(13); $obj->doSmth('x'); // Assert off, conditional off $conditional = false; $obj->doSmth(13); $obj->doSmth('x'); // Assert on, conditional on $withAssert = true; $conditional = true; $obj->doSmth(13); $obj->doSmth('x'); // Assert on, conditional off $conditional = false; $obj->doSmth(13); $obj->doSmth('x');
based on ddPFc
Output for 7.0.0 - 7.2.0
executing query: INSERT INTO mytable (integer_field) VALUES (13) executing query: INSERT INTO mytable (integer_field) VALUES (x) executing query: INSERT INTO mytable (integer_field) VALUES (13) Warning: assert(): InvalidArgumentException: wot ar y doin in /in/mhLkk:24 Stack trace: #0 /in/mhLkk(56): X->doSmth('x') #1 {main} failed in /in/mhLkk on line 24 executing query: INSERT INTO mytable (integer_field) VALUES (x) Warning: assert(): InvalidArgumentException: wot ar y doin in /in/mhLkk:24 Stack trace: #0 /in/mhLkk(62): X->doSmth('x') #1 {main} failed in /in/mhLkk on line 24
Output for 5.4.12 - 5.6.28
executing query: INSERT INTO mytable (integer_field) VALUES (13) executing query: INSERT INTO mytable (integer_field) VALUES (x) executing query: INSERT INTO mytable (integer_field) VALUES (13) Warning: assert(): exception 'InvalidArgumentException' with message 'wot ar y doin' in /in/mhLkk:24 Stack trace: #0 /in/mhLkk(56): X->doSmth('x') #1 {main} failed in /in/mhLkk on line 24 executing query: INSERT INTO mytable (integer_field) VALUES (x) Warning: assert(): exception 'InvalidArgumentException' with message 'wot ar y doin' in /in/mhLkk:24 Stack trace: #0 /in/mhLkk(62): X->doSmth('x') #1 {main} failed in /in/mhLkk on line 24
Output for 5.3.22 - 5.3.29
executing query: INSERT INTO mytable (integer_field) VALUES (13) executing query: INSERT INTO mytable (integer_field) VALUES (x) Warning: assert() expects exactly 1 parameter, 2 given in /in/mhLkk on line 24 executing query: INSERT INTO mytable (integer_field) VALUES (13) Warning: assert() expects exactly 1 parameter, 2 given in /in/mhLkk on line 24 executing query: INSERT INTO mytable (integer_field) VALUES (x) Warning: assert() expects exactly 1 parameter, 2 given in /in/mhLkk on line 24 Warning: assert() expects exactly 1 parameter, 2 given in /in/mhLkk on line 24