Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7622123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:22:21+00:00 2026-05-31T04:22:21+00:00

I have seen this: <?php include( dirname(__FILE__) . DIRECTORY_SEPARATOR . ‘my_file.php’); ?> Why would

  • 0

I have seen this:

<?php
  include( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my_file.php');
?>

Why would I ever need to do this? Why would I go to the trouble of getting the dirname and then concatenating that with a directory separator, and a new filename?

Is the code above not equivalent to this:

<?php
  include( 'my_file.php' );
?>

??

The PHP doc says,

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn’t found in the include_path, include() will finally check in the calling script’s own directory and the current working directory before failing. The include() construct will emit a warning if it cannot find a file; this is different behavior from require(), which will emit a fatal error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:22:24+00:00Added an answer on May 31, 2026 at 4:22 am

    Let’s say I have a (fake) directory structure like:

    .../root/
            /app
                bootstrap.php
            /scripts
                something/
                    somescript.php
            /public
                index.php
    

    Now assume that bootstrap.php has some code included for setting up database connections or some other kind of boostrapping stuff.

    Assume you want to include a file in boostrap.php‘s folder called init.php. Now, to avoid scanning the entire include path with include 'init.php', you could use include './init.php'.

    There’s a problem though. That ./ will be relative to the script that included bootstrap.php, not bootstrap.php. (Technically speaking, it will be relative to the working directory.)

    dirname(__FILE__) allows you to get an absolute path (and thus avoid an include path search) without relying on the working directory being the directory in which bootstrap.php resides.

    (Note: since PHP 5.3, you can use __DIR__ in place of dirname(__FILE__).)

    Now, why not just use include 'init.php';?

    As odd as it is at first though, . is not guaranteed to be in the include path. Sometimes to avoid useless stat()‘s people remove it from the include path when they are rarely include files in the same directory (why search the current directory when you know includes are never going to be there?).

    Note: About half of this answer is address in a rather old post: What's better of require(dirname(__FILE__).'/'.'myParent.php') than just require('myParent.php')?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen this problem arise in many different circumstances and would like to
Particularly, 08 and 09 have caused me some major trouble. Is this a PHP
I have seen this link: Implementing Mutual Exclusion in JavaScript . On the other
I have seen this macro defined before but never really knew its purpose. Can
I have seen this in a lot of XML comments for classes in the
I have seen this syntax in MSDN: yield break , but I don't know
I have seen this question: Are there any decent UI components for touch screen
I have seen this mentioned a few times and I am not clear on
I have seen this question about deploying to WebSphere using the WAS ant tasks.
I have seen this for a long time, and finally decided to put the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.