1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with stupid PHP include issue please?

Discussion in 'PHP & Perl' started by Entrepreneur, Oct 22, 2009.

  1. Entrepreneur

    Entrepreneur Regular Member

    Joined:
    Oct 12, 2007
    Messages:
    438
    Likes Received:
    379
    What it is, is i have a large site built in php and i'm converting it to a MySQL based CMS, to enable updating and moving it easier.

    The path to my includes is specified in all my files, but while buidling a config file i decided, as i'm moving server, to add the path to an include within the config, then within all my php files access this path within that include. Therefore if the server changes again in the future, i edit 1 line in 1 file and not a shed load in many files.

    What i did was create my config.inc.php file as below:

    Code:
    <?php
    define('INCLUDE_PATH' , '/server/path/to/includes/');
    ?>
    
    Then in all my files i include the above file first, and have the file itself within the public directory so doesn't require a path itself,

    Code:
    <?php
    require_once 'config.inc.php';
    ?>
    
    and then for any includes within say, the aboutus.php i use the include path from the config file as below.

    Code:
    <?php
    include INCLUDE_PATH . "header.inc.php";
    ?>
    
    This actually works perfectly, but it suddenly dawned upon me that, and this is probably because i'm crap at php, that when php does what it does, it would appear as below.

    Code:
    <?php
    include /server/path/to/includes/header.inc.php;
    ?>
    
    This obviously has no quotes or anything around it?

    Is this an issue, should i be doing this in a different way, because although it works on my home computer/server test bed thing (running Apache, PHP and MySQL) i worry that this is incorrect and the tolerant language that is php is letting it slide?

    As always, i go on, but help or a knowledgable answer would please me massively. Thanks. :)
     
  2. cyklotrial

    cyklotrial Regular Member

    Joined:
    Oct 13, 2008
    Messages:
    248
    Likes Received:
    82
    Location:
    Wonderland
    it is correct :) and it should work on normal server as it's working on your localhost
     
    • Thanks Thanks x 1
  3. Entrepreneur

    Entrepreneur Regular Member

    Joined:
    Oct 12, 2007
    Messages:
    438
    Likes Received:
    379
    Cool thanks. Out of curiosity, if you don't mind answering, why can it function ok without quotes when it's created in that manner? Because obviously under normal circumstances if i was to include the path without quotes it wouldn't function would it?
     
  4. BozoClown

    BozoClown Junior Member

    Joined:
    Jan 4, 2009
    Messages:
    150
    Likes Received:
    106
    Due to earlier design decisions php is to some extent loosely typed. For example
    1. function names not case sensitive
    2. Your can do some things in slightly different ways but still be correct
    Code:
    include ( 'file.php' );
    include 'file.php';
    include file.'.php';
    
    It was a bad decision or overlook that breeds unnecessary differences in coding styles.
     
    • Thanks Thanks x 1
  5. windofchange

    windofchange Registered Member

    Joined:
    Jan 31, 2009
    Messages:
    53
    Likes Received:
    11
    Personally I prefer 'require' statement instead 'include'; it helps me to write a more robust code, because if the file specified in the require statement is not found it causes a fatal error, include causes a warning message only. In this manner I have to focus my attention to avoid the file will not be found
     
  6. showboytridin

    showboytridin Regular Member

    Joined:
    Sep 5, 2009
    Messages:
    348
    Likes Received:
    716
    Location:
    127.0.0.1
    It will depend on many things. Sometimes the file is not critic. In that case using include is fine. For critic things we should see if the file exists before the include/require.