fork download
  1. <?php
  2.  
  3. function getWhereSql($id, $fio, $phone){
  4. $whereArr = array();
  5. if(!empty($id)){
  6. $whereArr[] = 'id = ' . intval($id);
  7. }
  8.  
  9. if(!empty($fio)){
  10. $whereArr[] = "fio LIKE '%" . $fio. "%'";
  11. }
  12.  
  13. if(!empty($phone)){
  14. $whereArr[] = "phone LIKE '%" . $phone. "%'";
  15. }
  16.  
  17. if(empty($whereArr)){
  18. echo "ERROR, empty data \n";
  19. } else {
  20. echo "WHERE " . implode(' AND ', $whereArr) . "\n";
  21. }
  22. }
  23.  
  24. getWhereSql(1, 'Миша Маваши', 12345678);
  25. getWhereSql(null, 'Миша Маваши', null);
  26. getWhereSql(null, null, null);
  27.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
WHERE id = 1 AND fio LIKE '%Миша Маваши%' AND phone LIKE '%12345678%'
WHERE fio LIKE '%Миша Маваши%'
ERROR, empty data