3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function buildQuery($retrievables, $table, $conditions = "", $limit = 0, $order = "") { $query = "SELECT $retrievables FROM $table"; // if we have conditions - add them to the query $query .= ($conditions != "" ? " WHERE " . $conditions : ""); // if we have a limit - add it to the query $query .= ($limit != 0 ? " LIMIT " . $limit : ""); // if we have to order the results - add it to the query // first we create a "dictionary" which translates $niceValues that are used throughout the code // to $realValues which are used in the MySQL database // since the $realValues are different in every table - we must account to that switch ($table) { case 'usersTable': $niceValues = ["userID", "userName", "userAge", "userLocation", "userSex", "userIntention", "usersTable"]; $realValues = ["uid", "uname", "user_bdate", "user_from", "name", "user_intrest", "nuke_users"]; // yeah - the field "name" is really the sex of the user break; case 'articlesTable': $niceValues = ["articleID", "articleTitle", "articleLeadingText", "articleBodyText"]; $realValues = ["sid", "title", "hometext", "bodytext"]; break; default: $realValues = []; } // we then replace $niceValues with $realValues $modifiedQuery = str_ireplace($niceValues, $realValues, $query); return $modifiedQuery; } print_r(buildQuery("userID, userAge, userSex", "usersTable", 10)); ?>
Output for 5.4.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
SELECT uid, user_bdate, name FROM nuke_users WHERE 10
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/03pWl on line 16
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/03pWl on line 16
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/03pWl on line 16
Process exited with code 255.