PHP includes

Discussion in 'PHP' started by Jim bob 9 pants, Sep 14, 2006.

  1. #1
    Hi

    I want to use this include

    include("./directory/folder/$town.php")

    (I Have taken away the PHP tags)

    if $town does not exist is there a way of bypassing the include so the page still loads correctly?

    I am getting the variable from the URL

    Jamie

    (fingers crossed):)
     
    Jim bob 9 pants, Sep 14, 2006 IP
  2. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can check to see if the file exists with the file_exists function before trying to include it. If the file exists, include the file, otherwise do nothing.

    $file = "./directory/folder/" . $town . ".php";
    if (file_exists($file)) {
       include ($file);
    }
    PHP:
     
    tflight, Sep 14, 2006 IP
  3. Jim bob 9 pants

    Jim bob 9 pants Peon

    Messages:
    890
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I will try that you star!!

    I was not awear of that function thanks very much
     
    Jim bob 9 pants, Sep 14, 2006 IP
  4. Jim bob 9 pants

    Jim bob 9 pants Peon

    Messages:
    890
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Works like a dream
     
    Jim bob 9 pants, Sep 14, 2006 IP
  5. Crazy4Bass

    Crazy4Bass Well-Known Member

    Messages:
    174
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    138
    #5
    Just and fyi.. include is not a function, it's a special construct that doesn't require you to use the () as their use impacts how the construct evaluates the include.

    include 'filename.php'; is all you need to do.
     
    Crazy4Bass, Sep 14, 2006 IP