3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function getSql($arr) { $sql = 'SELECT id FROM table1 WHERE '; $and = ''; foreach ($arr as $a) { $sql .= $and . "'$a' IN (cat1, cat2, cat3) "; $and = ' AND '; } return $sql; } echo getSql(array('a')); // one choice echo "\n"; echo getSql(array('a', 'b')); // two choices echo "\n"; echo getSql(array('a', 'b', 'c')); // three choices ?>
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
SELECT id FROM table1 WHERE 'a' IN (cat1, cat2, cat3) SELECT id FROM table1 WHERE 'a' IN (cat1, cat2, cat3) AND 'b' IN (cat1, cat2, cat3) SELECT id FROM table1 WHERE 'a' IN (cat1, cat2, cat3) AND 'b' IN (cat1, cat2, cat3) AND 'c' IN (cat1, cat2, cat3)