{"id":340,"date":"2021-01-27T12:00:00","date_gmt":"2021-01-27T17:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=340"},"modified":"2020-12-24T00:55:17","modified_gmt":"2020-12-24T05:55:17","slug":"what-is-the-difference-between-include-include_once-require-and-require_once-in-php","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/what-is-the-difference-between-include-include_once-require-and-require_once-in-php\/","title":{"rendered":"What is the difference between include, include_once, require, and require_once in PHP"},"content":{"rendered":"\n<p>PHP provides four functions which enable you to insert code from other files.<\/p>\n\n\n\n<p>* include()<br>* require()<br>* include_once()<br>* require_once()<\/p>\n\n\n\n<p>All four can take a local file or URL as input. None of them can import a remote file. require() and include() functions are virtually similar in their function except for the way they handle an irretrievable resource. include() and include_once() provide a warning if the resource cannot be retrieved and try to continue execution of the program if possible. require() and require_once functions provide stop processing the page if they cannot retrieve the resource.<\/p>\n\n\n\n<p><strong>Why include_once() and require_once()<\/strong><br>The include_once() and require_once() functions are handy in situations where multiple files may reference the same included code. For example:<\/p>\n\n\n\n<p>File A.php includes File B.php and C.php<br>File B.php includes File C.php<\/p>\n\n\n\n<p>File C.php has been included twice, so the interpreter would print an error. Since a function cannot be redefined once it\u2019s declared, this restriction can help prevent errors.<\/p>\n\n\n\n<p>If both File A.php and File B.php use include_once() or require_once() to import File C.php, no errors would be generated. PHP would understand that you only want one instance of the code in File C and would not try to redeclare the functions.<\/p>\n\n\n\n<p>It is best to use require_once() to include files which contain necessary code and include_once() to include files that contain content which the program can run without e.g. HTML, CSS, etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP provides four functions which enable you to insert code from other files. * include()* require()* include_once()* require_once() All four can take a local file or URL as input. None of them can import a remote file. require() and include() functions are virtually similar in their function except for the way they handle an irretrievable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[24],"class_list":["post-340","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=340"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/340\/revisions"}],"predecessor-version":[{"id":341,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/340\/revisions\/341"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}