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 3803224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:19:47+00:00 2026-05-19T14:19:47+00:00

I have just started learning PHP and I have read several answers to pretty

  • 0

I have just started learning PHP and I have read several answers to pretty much similar question, but it didn’t help.

I have several files:

index.php 
folder1\file.php
folder2\file.php 

So index.php includes folder1\file.php and it is really easy. Then folder1\file.php includes folder2\file.php. The book I am reading says to do this I have to write this inside folder1\file.php:

require $_SERVER['DOCUMENT_ROOT'] . '/' . 'folder2/file.php';

So basically why didn’t they do this?

require 'folder2/file.php';

As far as I understood, all the paths are always relative to the initial script’s location and it works just fine on my machine.

I still don’t get why not use .\folder\file.php which would keep it working if I move my directory deeper or copy the whole website to another machine.

According to the manual: If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory.

  • 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-19T14:19:48+00:00Added an answer on May 19, 2026 at 2:19 pm

    The PHP manual 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.

    So it’s always a best practice to set the include path in your master file (in your case, index.php) using the set_include_path() method.

    When working with frameworks or customised servers, it is good to specify your include path without leaving behind the system defined include paths by doing the following:

    set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);
    

    That way, your new include path will be added to the existing paths.

    For what you have asked in the question, it is always better to specify the include path as I mentioned above and then refer your files relatively. If in case in future you want to move your files, editing just one line will do instead of finding and replacing in hundreds of files.

    Example:

    index.php:

    set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);
    require('folder1/file.php');
    

    folder1/file.php:

    require('folder2/file.php');
    

    folder2/file.php:

    require('folder3/file.php');
    

    and so on..

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

Sidebar

Related Questions

I have just started learning GWT. My question is pretty simple: If I create
I have just started learning C# and would have one question that I cannot
I have just started learning coding and PHP so I have been looking at
I just started learning Zend (& OO PHP for that matter), I have spent
I have just started learning Android and thought of a simple project to help
I have just started learning Erlang and am trying out some Project Euler problems
I have just started writing my own JavaScript Framework (just for the learning experience),
I haven't officially started learning PHP, just skimming through a couple tutorials and I
I have just started learning C#. Can anyone explain the technical differences between a
Hello i have just started learning mvc2 and im having a problem with 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.