{"id":639,"date":"2021-11-14T20:59:57","date_gmt":"2021-11-15T01:59:57","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=639"},"modified":"2024-02-08T08:43:29","modified_gmt":"2024-02-08T13:43:29","slug":"php-require-vs-include","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/php-require-vs-include\/","title":{"rendered":"PHP require() vs include()"},"content":{"rendered":"\n<p>PHP provides four functions which enable you to insert code from other files.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/ch2.php.net\/manual\/en\/function.include.php\" target=\"_blank\" rel=\"noreferrer noopener\">include()<\/a><\/li><li><a href=\"http:\/\/ch2.php.net\/manual\/en\/function.require.php\" target=\"_blank\" rel=\"noreferrer noopener\">require()<\/a><\/li><li><a href=\"http:\/\/ch2.php.net\/manual\/en\/function.include-once.php\" target=\"_blank\" rel=\"noreferrer noopener\">include_once()<\/a><\/li><li><a href=\"http:\/\/ch2.php.net\/manual\/en\/function.require-once.php\" target=\"_blank\" rel=\"noreferrer noopener\">require_once()<\/a><\/li><\/ul>\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<h3 class=\"wp-block-heading\">Why include_once() and require_once()<\/h3>\n\n\n\n<p>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<pre class=\"wp-block-code\"><code>File A.php includes File B.php and C.php\nFile B.php includes File C.php\n<\/code><\/pre>\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 resource. [&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":[],"class_list":["post-639","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/639","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=639"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/639\/revisions"}],"predecessor-version":[{"id":640,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/639\/revisions\/640"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}