Results 1 to 9 of 9

Thread: Is there a PHP version of the ASP #include?

  1. #1
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question Is there a PHP version of the ASP #include?

    Hello people,

    A quick question: is there a PHP version of the ASP #include?

    I'm talking about a command that brings script onto the page from an external file.

    Great for elements like menus that appear on various pages because when you need to edit them you just edit the external file.

    Thanks for any help,

    Dog

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    PHP Code:
    <?php include_once('/path/to/file.ext'?>
    that will spit out a warning but will continue to process the rest of the code... if the file you are attempting to bring in is critical you can use

    PHP Code:
    <?php require_once('/path/to/file.ext'?>

  3. #3
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Smile

    Thanks for the quick reply!

    Quote Originally Posted by boogyman View Post
    PHP Code:
    <?php include_once('/path/to/file.ext'?>
    that will spit out a warning but will continue to process the rest of the code... if the file you are attempting to bring in is critical you can use

    PHP Code:
    <?php require_once('/path/to/file.ext'?>
    What is the effect of spitting out a warning? Sounds bad

    What do you mean by critical. Sounds serious!

    Thanks

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    think of this situation...
    you are ddamin (owner of this site), and rebuilding a new login system.
    when you want the userCP interface to look totally different, so you create a new process page.
    connecting to the database is a critical step.. if you cannot select the user, well then it doesnt matter if you can or cannot do something else.... thus thats critical, and really nothing else should be done if you cannot connect to the database... thus you should require that file.

    okay so the user is in and inside their control panel page. the user now would like to print the page for personal use, but the css print styles are not prepared correctly... this would be a situation where you could use an include, because you would like the print styles to not print all of the extra colors n background images, but if it does well its not the end of the world.

    basically if something must be used you should make it required becaues it will terminate the script and give an error
    if something should be included but is not absolutely necessary you should use include which will just give a warning.

    both of these will be parsed in the error log located on the local machine, and unless handled differently, they will print the error / warning on the page

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The error will only be generated if the page DOESN'T exist.

    you could also use:
    <?php $f='my/path/myfile.ext'; if (file_exists($f)) { include($f); } ?>
    That will check that it exists and if so, output.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    But that's silly, because this check is built into the include keyword, so you're effectively checking twice. You might as well just do:
    Code:
    <?php @include 'my/path/myfile.ext'; ?>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Hm. I didn't do that because I thought there were some strange error suppression restrictions on include, though I must have imagined that.
    It does seem that will work fine, though the if format would at least give an alternate option.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Not as far as I'm aware.
    the if format would at least give an alternate option.
    That's true. You could also calculate the precise distance between the Earth and Jupiter, then ignore the result before using the include statement.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    500,598,2..... nevermind.

    With an include, though, it makes sense to have some backup if it failed, though in this case, it may not be dynamic anyway.

    I looked on PHP.net for comments on the subject, and I don't see anything like that, so I must have imagined it, but I thought that I read somewhere that error suppression causes problems with certain functions and that include was a "big enough", "construct [not function]" that it fell into this.
    Again, though, probably just thinking of something else.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •