我需要一些幫助,我有例如index.php和和我需要使它像。 某人訪問:PHP搜索url請求
index.php?search=blbla
include search.php
else
include home.php
我需要一個忠告與此感謝
我需要一些幫助,我有例如index.php和和我需要使它像。 某人訪問:PHP搜索url請求
index.php?search=blbla
include search.php
else
include home.php
我需要一個忠告與此感謝
試試這個
if (isset($_GET['search'])) include('search.php');
else include('home.php');
<?php
//if($_GET['search'] > ""){ // this will likely cause an error
if(isset($_GET['search']) && trim($_GET['search']) > ""){ // this is better
include ('search.php');
}else{
include ('home.php');
}
?>
$sq = $_GET['search']; //$_GET['']
if (isset($sq) && $sq != '') {
include('search.php');
} else {
include('home.php');
}
Yogesh Suthar - 謝謝! – faq
使用這樣
if (isset($_GET['search']))
include 'search.php';
else
include 'home.php';
那麼,你可以使用isset()
來查看是否設置了變量。例如
if(isset($_GET['search'])){
include "search.php";
}
else {
include "home.php";
}
我個人更喜歡來檢查$_GET
設置,如果它實際上等於像這樣:
if(isset($_GET['search']) && strlen(trim($_GET['search'])) > 0): include 'search.php';
else: include 'home.php';
這將避免將在$_GET
變量,但實際上沒有設置它的問題。
當使用isset()
你需要知道,像這樣一個空的GET變量script.php?foo=
這isset($_GET['foo'])
將返回TRUE
美孚設置,但沒有價值。
所以,如果你想確保一個GET變量,你可能想使用strlen()
與trim()
,而不是相結合的價值...
if (strlen(trim($_GET['search'])) > 0) {
include('search.php');
} else {
include('home.php');
}
而且你可能想使用require()
而不是include()
。如果不能「包含」search.php,只有PHP警告,search.php不能「需要」纔會產生PHP致命錯誤。
瞭解'if'語句和'$ _GET'表。 –
它可能在未來付費以僞碼形式發佈,這樣人們可以更清楚地理解你想說什麼。就像url包含'?search =',然後包含search.php - else include home.php。 – Chris