{"id":77,"date":"2020-12-11T19:43:16","date_gmt":"2020-12-11T19:43:16","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/php\/?p=77"},"modified":"2026-10-29T01:29:15","modified_gmt":"2026-10-29T01:29:15","slug":"php-include-and-require","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/","title":{"rendered":"PHP &#8211; Include and Require"},"content":{"rendered":"<p>This is a way to allow you include the content one PHP file into another PHP file. To achieve this you will you either of the two functions:<\/p>\n<ol>\n<li><a href=\"#t1\">The include() function<\/a><\/li>\n<li><a href=\"#t2\">The require() function<\/a><\/li>\n<\/ol>\n<p>The benefit of this is that certain elements can be reused in multiple pages of a website. For example, you may want to have a navigation bar and a footer that appears in all your pages. Instead of creating them in all the pages, you can just create on file containing the header and another file containing\u00a0 the footer. Then you can include this file in the pages. Any changes you make to these included file would reflect in all the pages. In this way,\u00a0 it would be easier to change the layout of a quicker.<\/p>\n<p>Let&#8217;s take a closer look at these two functions<\/p>\n<h4><strong id=\"t1\">1. The include() Function<\/strong><\/h4>\n<p>The<em> include()<\/em> function takes all the text in a given file and copies them into the other file. If there are some error loading this include file, then the interpreter would output a warning but the script would continue executing.<\/p>\n<p>For example, the code below contains\u00a0 a possible navigation bar for my website <em>www.kindsonthegenius.com<\/em>. I create as save this menu in a file call <em>navbar.php<\/em>.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">&lt;a href=\"https:\/\/www.kindsonthegenius.com\/home\"&gt;Home&lt;\/a&gt; - \r\n&lt;a href=\"https:\/\/www.kindsonthegenius.com\/java\"&gt;Java&lt;\/a&gt; - \r\n&lt;a href=\"https:\/\/www.kindsonthegenius.com\/python\"&gt;Python&lt;\/a&gt; - \r\n&lt;a href=\"https:\/\/www.kindsonthegenius.com\/javascript\"&gt;JavaScript&lt;\/a&gt; &lt;br \/&gt;\r\n<\/pre>\n<p>Now, in any of the page you would like to use the this, you can now include the <em>navbar.php<\/em> like this.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">&lt;html&gt;\r\n   &lt;body&gt;  \r\n      <span style=\"color: #557799;\">&lt;?php<\/span> <span style=\"color: #008800; font-weight: bold;\">include<\/span>(<span style=\"background-color: #fff0f0;\">\"navbar.php\"<\/span>); <span style=\"color: #557799;\">?&gt;<\/span>\r\n      &lt;p&gt;This how to include a file&lt;\/p&gt;     \r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. The require() Function<\/strong><\/h4>\n<p>Similar to the include() function, the require function also copies a specified file into another file. However, the difference is on how they handle errors.\u00a0 When there require() function encounters a problem loading the file, the a fatal error is generated and the execution of the script is halted.<\/p>\n<p>For example let&#8217;s use the include() function and require() function to include an file that does not exist.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">&lt;html&gt;\r\n   &lt;body&gt;  \r\n      <span style=\"color: #557799;\">&lt;?php<\/span> <span style=\"color: #008800; font-weight: bold;\">require<\/span>(<span style=\"background-color: #fff0f0;\">\"nofile.php\"<\/span>); <span style=\"color: #557799;\">?&gt;<\/span>\r\n      &lt;p&gt;This how to include a file&lt;\/p&gt;     \r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>When this code executes, the require function encounters an error since the file does not exist. Program terminates. However, if you use the include() function, it would continued and output the text in the next line.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a way to allow you include the content one PHP file into another PHP file. To achieve this you will you either of &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[10,11],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-include","tag-require"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP - Include and Require | Kindson The Genius<\/title>\n<meta name=\"description\" content=\"PHP - Include and Require \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP - Include and Require | Kindson The Genius\" \/>\n<meta property=\"og:description\" content=\"PHP - Include and Require \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-11T19:43:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-29T01:29:15+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-include-and-require\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-include-and-require\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"headline\":\"PHP &#8211; Include and Require\",\"datePublished\":\"2020-12-11T19:43:16+00:00\",\"dateModified\":\"2026-10-29T01:29:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-include-and-require\\\/\"},\"wordCount\":346,\"keywords\":[\"include()\",\"require()\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-include-and-require\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-include-and-require\\\/\",\"name\":\"PHP - Include and Require | Kindson The Genius\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\"},\"datePublished\":\"2020-12-11T19:43:16+00:00\",\"dateModified\":\"2026-10-29T01:29:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"description\":\"PHP - Include and Require \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-include-and-require\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-include-and-require\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-include-and-require\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP &#8211; Include and Require\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\",\"name\":\"PHP Tutorial\",\"description\":\"Beginner to Expert With Practicals\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP - Include and Require | Kindson The Genius","description":"PHP - Include and Require \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/","og_locale":"en_US","og_type":"article","og_title":"PHP - Include and Require | Kindson The Genius","og_description":"PHP - Include and Require \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","og_url":"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/","og_site_name":"PHP Tutorial","article_published_time":"2020-12-11T19:43:16+00:00","article_modified_time":"2026-10-29T01:29:15+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"headline":"PHP &#8211; Include and Require","datePublished":"2020-12-11T19:43:16+00:00","dateModified":"2026-10-29T01:29:15+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/"},"wordCount":346,"keywords":["include()","require()"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/","url":"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/","name":"PHP - Include and Require | Kindson The Genius","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/#website"},"datePublished":"2020-12-11T19:43:16+00:00","dateModified":"2026-10-29T01:29:15+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"description":"PHP - Include and Require \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/php\/php-include-and-require\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/php\/"},{"@type":"ListItem","position":2,"name":"PHP &#8211; Include and Require"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/php\/#website","url":"https:\/\/kindsonthegenius.com\/php\/","name":"PHP Tutorial","description":"Beginner to Expert With Practicals","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/php\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/77\/revisions\/79"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}