function append_where(&$sql, $has_where) {
$sql .= $has_where ? ' AND ' : ' WHERE ';
return $sql;
}
function demo($name = '', $age = '') {
$sql = "SELECT * FROM `employee`";
$has_where = FALSE;
if(!empty($name)) {
$has_where = append_where($sql, $has_where);
$sql .= "`name`='{$name}'";
}
if(!empty($age)) {
$has_where = append_where($sql, $has_where);
$sql .= "`age`='{$age}'";
}
echo $sql;
}
demo('张三', '18');,输出为:SELECT * FROM `employee` WHERE `name`='张三' AND `age`='18',demo('18');,输出为:SELECT * FROM `employee` WHERE `name`='18'。