<?php if($current_page=='page1' || 'sub1' or 'sub2' or 'sub3' or 'sub5'){echo 'active';} ?>你好朋友,如果我將這樣的代碼寫入標(biāo)題的子菜單類,則所有具有子菜單的標(biāo)題都會被激活。我的頁面像 header.php、footer.php 一樣分解。在每個內(nèi)頁中,在頁面頂部我寫了這段代碼<?php ob_start(); session_start(); $current_page='page1'; include 'header.php';?>橙子很活躍:
1 回答

犯罪嫌疑人X
TA貢獻2080條經(jīng)驗 獲得超4個贊
好的,問題出在你的 if 語句上。PHP 將非空字符串視為 true,因此每個“或”條件都返回 true。您需要在每個“或”條件中指定“$current_page ==”
IE
<?php?if($current_page=='page1'?||?$current_page=='sub1'?||?$current_page=='sub2'?||?$current_page=='sub3'?||?$current_page=='sub5'){echo?'active';}??>
但這是很長的。更簡潔的方法是使用“in_array”
<?php?if(in_array($current_page,?array('page1','sub1','sub2','sub3','sub5'))){echo?'active';}??>
- 1 回答
- 0 關(guān)注
- 112 瀏覽
添加回答
舉報
0/150
提交
取消