basic PHP again require and include

Hello,

I can see PHP codes like this:

<?php require_once 'includes/temps/header.php'; ?>

everything is working fine I'm just wondering how was the file found?

This PHP code <?php require_once 'includes/temps/header.php'; ?> is found on this folder admin/templates/loginform.php
and the file it is trying to include or require is found here includes/temps/header.php

The structure look like this on my local machine:

the root:

admin (folder)          it is found here admin/templates/loginform.php <?php require_once 'includes/temps/header.php'; ?>
frontend (folder)
includes (folder)   requiring the file found on this folder   includes/temps/header.php
index.php (file)
login.php (file)

How come it is working fine and was able to locate the file it is trying to require? Why not use this <?php require_once '../../ these kind of things to go outside of the folder and go to the root?

I'm thinking about base URL is place somewhere among the files? What exactly does it do? I mean it gives the Wamp server orientation where the root and other folders??

I'm using Windows 10 and wamp server.

Kind Regards,

Don
prinLeAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian TaoSenior Business Solutions ConsultantCommented:
Files are included (using include/include_once/require/require_once) based on the file path given or, if none is given, the include_path specified in your php.ini file.
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.

PS. Information comes from http://php.net/manual/en/function.include.php
Brian TaoSenior Business Solutions ConsultantCommented:
BTW, relevant path, e.g. "../../some_other_folder/included_file.php", is also accepted.
Dave BaldwinFixer of ProblemsCommented:
I believe the paths are relative to the main file that the 'includes' are in.  If you find them in 'index.php' in the root, they are being 'included' from that location and they all make sense then.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Julian HansenCommented:
Let's say you have this
index.php
<?php require_once('admin/templates/loginform.php')?>
...

Open in new window

And in loginform.php you have this
<?php require_once 'includes/temps/header.php'; ?>
...

Open in new window


The master script here is index.php - when it includes files (like loginform.php) they become part of the index.php script. The location where they were loaded from is ignored and the system treats the combined file as if it was all index.php.

Therefore when loginform.php does a require_once it is interpreted as being relative to index.php not loginform.php

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prinLeAAuthor Commented:
Hello,

Thanks Julian Hansen you are clearing things up for me I really appreciate that. I will wait for other experts I might get more knowledge.

Kind Regards,

Prinlea
prinLeAAuthor Commented:
Hello,

To Julian Hansen how come other PHP codes are using this <?php require_once('admin/templates/loginform.php')?> while others have no parenthesis like this <?php require_once 'includes/temps/header.php'; ?> are they one and the same? What is the difference?
Julian HansenCommented:
The parenthesis are not a function of the require / require_once / include etc but rather of how php deals with strings

these are all equivalent

echo "Hello World";
echo ("Hello World");
echo ("Hello"). "World";

Open in new window


PHP uses the parenthesis in strings for grouping.

So to answer your question the following is how require / include works
require_once 'path/to/file.php';

Open in new window

but putting () around the string is also legal
Dave BaldwinFixer of ProblemsCommented:
I agree with Julian.  He said what I was trying to say only better.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.