2012-09-24 31 views
3

我需要一些幫助,我有例如index.php和和我需要使它像。 某人訪問:PHP搜索url請求

index.php?search=blbla 
include search.php 
else 
include home.php 

我需要一個忠告與此感謝

+0

瞭解'if'語句和'$ _GET'表。 –

+0

它可能在未來付費以僞碼形式發佈,這樣人們可以更清楚地理解你想說什麼。就像url包含'?search =',然後包含search.php - else include home.php。 – Chris

回答

2

試試這個

if (isset($_GET['search'])) include('search.php'); 
else include('home.php'); 
0
<?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'); 
} 

?> 
2
$sq = $_GET['search']; //$_GET[''] 
if (isset($sq) && $sq != '') { 
include('search.php'); 
} else { 
include('home.php'); 
} 
+0

Yogesh Suthar - 謝謝! – faq

0

使用這樣

if (isset($_GET['search'])) 
    include 'search.php'; 
else 
include 'home.php'; 
2

那麼,你可以使用isset()來查看是否設置了變量。例如

if(isset($_GET['search'])){ 
    include "search.php"; 
} 
else { 
    include "home.php"; 
} 
0

我個人更喜歡來檢查$_GET設置,如果它實際上等於像這樣:

if(isset($_GET['search']) && strlen(trim($_GET['search'])) > 0): include 'search.php'; 
else: include 'home.php'; 

這將避免將在$_GET變量,但實際上沒有設置它的問題。

0

當使用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致命錯誤。