Hello,

I am confused about the php there is used to include a file like headers footers functions etc etc so I would like to ask what is the difference between the followings:
require_once();
require();
include();
include_once();

Thank You

Recommended Answers

All 6 Replies

okay liek if I write require_once the file will be included once and if i include the file using require statement both will do the same am i right ?

Member Avatar for diafol

Whether to include or require is a matter for you. I very rarely if ever use 'include' these days as most of the files I include are mandatory to the functioning of my app/page and are 'required'.
Using the _once constructs ensures that the file is only ever included/required once, even if there are multiple instances of including/requiring that file. My sites are not usually convoluted enough to demand the _once, but some seasoned php-heads insist that require_once is the only construct that should be used, ever. I can't say I'm totally convinced by this, as it may increase the time taken for code to run (may be marginal) over a stright require.

If you search long enough, you'll come across some examples of when to use which option. Again, many of these examples are largely subjective and open to argument.

Okay perfect my concept is cleared about 60% well I will se the examples as per your guidance

Member Avatar for diafol

Great. So in short, my recommendation would be to use "require" as a rule. The "require_only" can be handy if you have a lot of nested requires with references to the same require files.

This post is talking about performance difference between require and require_once. May be interested for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.