3

I need some help with this I have for example index.php and and i need to make it something like. someone access:

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

I need an advice with this thanks

2
  • Learn about if statement and $_GET table. Commented Sep 24, 2012 at 13:00
  • it might pay in future to post in pseudocode, so that people can more clearly understand what you are trying to say. Something like if url contains '?search=' then include search.php - else include home.php. Commented Sep 24, 2012 at 13:13

7 Answers 7

2

Try this

if (isset($_GET['search'])) include('search.php');
else include('home.php');
Sign up to request clarification or add additional context in comments.

Comments

2

Well, you could use isset() to see if the variable is set. e.g.

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

Comments

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

Comments

0

I personally prefer to check if a $_GET is set and if it actually equals something like so:

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

This will avoid the problem of putting in the $_GET variable but not actually setting it.

Comments

0

use it like this

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

Comments

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');
}

?>

Comments

0

When using isset() you need to be aware that with an empty GET variable like this script.php?foo= that isset($_GET['foo']) will return TRUE
Foo is set but has no value.

So if you want to make sure that a GET variable has a value you might want to use strlen() combined with trim() instead...

if (strlen(trim($_GET['search'])) > 0) {
    include('search.php');
} else {
    include('home.php');
}

Also you might want to use require() instead of include(). A PHP fatal error is generated if search.php cannot be "required" with just a PHP warning if search.php cannot be "included".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.