如果项目中有条件,我已经嵌套了,但是我有问题;
if(isset($_GET['q']) && isset($_GET['t'])) {
$query = $_GET['q'];
$type = $_GET['t'];
$none_key = FALSE;
if($type = 'singer') {
$singers = $connect->query("SELECT * FROM lyrica_singers WHERE singer_name LIKE '%$query%'");
$control = $singers->rowCount();
if($control > 0) {
$on_page = 24;
$number_singers = $singers->rowCount();
$number_page = ceil($number_singers/$on_page);
$page = isset($_GET['p']) ? (int) $_GET['p'] : 1;
if ($page < 1) $page = 1;
if ($page>$number_page) $page = $number_page;
$limit = ($page - 1) * $on_page;
$singers = $connect->query("SELECT * FROM lyrica_singers WHERE singer_name LIKE '%$query%' ORDER BY singer_name ASC LIMIT ".$limit.",".$on_page);
$singer_key = TRUE;
} else {
$none_key = TRUE;
}
}
if($type = 'song') {
$songs = $connect->query("SELECT * FROM lyrica_songs WHERE song_name LIKE '%$query%'");
$control = $songs->rowCount();
if($control > 0) {
$on_page = 24;
$number_songs = $songs->rowCount();
$number_page = ceil($number_songs/$on_page);
$page = isset($_GET['p']) ? (int) $_GET['p'] : 1;
if ($page < 1) $page = 1;
if ($page>$number_page) $page = $number_page;
$limit = ($page - 1) * $on_page;
$songs = $connect->query("SELECT * FROM lyrica_songs WHERE song_name LIKE '%$query%' ORDER BY song_name ASC LIMIT ".$limit.",".$on_page);
$song_key = TRUE;
} else {
$none_key = TRUE;
}
}
} else {
$key = TRUE;
}
运行代码时,我希望如果'control'变量之一大于0,则'none_key'变量必须等于0。'type'变量为'song'时没有问题但是如果'type'变量是'singer',那么'none_key'变量将显示1,我认为它正在运行第二个if块,并且由于第二个'control'变量不大于零而变为'none_key'1。>
答案 0 :(得分:2)
这是一个分配,它将$type
设置为值singer
:
if ($type = 'singer') {
您要进行比较,它会检查$type
是否等于值singer
:
if ($type == 'singer') {
[编辑] 有些人喜欢用变量last来编写这种语句。这通常称为“尤达条件”:
if ('singer' == $type) {
这样,如果您搞砸了,只使用一个=
,则会出现错误。