3v4l.org

run code in 300+ PHP versions simultaneously
<?php $search_input = 'bob'; $industry_general = ''; $industry_specific = ''; $selected_city = 'bobville'; $conditions = []; // Start by processing the user input into a data structure that can be used to construct the query if (!empty($search_input)) { $conditions[] = [ ['company_name', 'LIKE', '%' . $search_input . '%'], ['company_keywords', 'LIKE', '%' . $search_input . '%'], ['company_city', 'LIKE', '%' . $search_input . '%'], ['company_email', 'LIKE', '%' . $search_input . '%'], ]; } if (!empty($industry_general)) { $conditions[] = [ ['industry_types', '=', $industry_general], ]; } if (!empty($industry_specific)) { $conditions[] = [ ['sub_industy', '=', $industry_specific], ]; } if (!empty($selected_city)) { $conditions[] = [ ['company_city', '=', $selected_city], ]; } // Loop the conditions and process them into valid SQL strings $i = 1; $bindValues = []; $whereClauseParts = []; foreach ($conditions as $conditionSet) { $set = []; foreach ($conditionSet as $condition) { list($fieldName, $operator, $value) = $condition; $bindValueName = 'value_' . $i++; $set[] = "`{$fieldName}` {$operator} :{$bindValueName}"; $bindValues[$bindValueName] = $value; } $whereClauseParts[] = implode(' OR ', $set); } $statement = "SELECT *\nFROM business_table"; if (!empty($whereClauseParts)) { $statement .= "\nWHERE (" . implode(")\n AND (", $whereClauseParts) . ")"; } echo "Statement:\n"; echo $statement, "\n\n"; echo "Values:\n"; foreach ($bindValues as $name => $value) { echo " $name => $value\n"; }
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Statement: SELECT * FROM business_table WHERE (`company_name` LIKE :value_1 OR `company_keywords` LIKE :value_2 OR `company_city` LIKE :value_3 OR `company_email` LIKE :value_4) AND (`company_city` = :value_5) Values: value_1 => %bob% value_2 => %bob% value_3 => %bob% value_4 => %bob% value_5 => bobville

preferences:
127.63 ms | 404 KiB | 184 Q