home > web > include-vs-include-once-php

include vs. include_once in PHP

posted on 2014-10-08 by Spencer Bravo

A few months ago I was building this site and while looking up how to do something, I noticed the person whose code I was looking at used:

include_once($_SERVER['DOCUMENT_ROOT']."/file.php");

Rather than what I was using:

include($_SERVER['DOCUMENT_ROOT']."/file.php");

I wasn't really sure what the difference was so I did the research. When you use "include" you tell the document just to include the file right there no matter what. When you use "include_once" if the file has been already included in that page it will skip over that command. For example: if you're workin gon a site and you have a seperate header file which you "include" into both your "config" and "index" page that means that if you include the "config" file in the "index" page it will "include" the header twice. Though if you use "include_once" it will understand that the header has already been included and skip it.

I hope this post is useful! If you have any questions or comments please don't hesitate to email us at CSTeam@CodeShrub.com

posted in web