{"id":896,"date":"2021-04-06T02:33:18","date_gmt":"2021-04-06T02:33:18","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=896"},"modified":"2025-04-06T07:50:14","modified_gmt":"2025-04-06T07:50:14","slug":"php-include_once","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-include_once\/","title":{"rendered":"PHP include_once"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the PHP <code>include_once<\/code> construct to include a file once.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-include_once-construct'>Introduction to the PHP include_once construct <a href=\"#introduction-to-the-php-include_once-construct\" class=\"anchor\" id=\"introduction-to-the-php-include_once-construct\" title=\"Anchor for Introduction to the PHP include_once construct\">#<\/a><\/h2>\n\n\n\n<p>In the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-include-file\/\">include<\/a><\/code> tutorial, you learned how to load the code from another file using the <code>include<\/code> construct.<\/p>\n\n\n\n<p>Sometimes, you may have a file that is included more than once.<\/p>\n\n\n\n<p>If the included file has a function, you&#8217;ll get a fatal error because the function is already redeclared in the first load. For example:<\/p>\n\n\n\n<p>Suppose that you have the following project directory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">.\n\u251c\u2500\u2500 inc\n\u2502   \u2514\u2500\u2500 functions.php\n\u2514\u2500\u2500 index.php<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>functions.php<\/code> has the <code>dd()<\/code> function definition:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">dd<\/span><span class=\"hljs-params\">($data)<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'&lt;pre&gt;'<\/span>;\n\tvar_dump($data);\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'&lt;\/pre&gt;'<\/span>;\n\t<span class=\"hljs-keyword\">die<\/span>();\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And in the <code>index.php file<\/code>, you include the <code>functions.php<\/code> file twice:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">'inc\/functions.php'<\/span>;\n<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">'inc\/functions.php'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>PHP will issue the following error if you run the <code>index.php<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Fatal error: Cannot redeclare dd() (previously declared in .\\inc\\functions.php:3) in .\\inc\\functions.php on line 3<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Likewise, if the included file outputs some HTML elements, you&#8217;ll see them more once on the page. To see how it happens, let&#8217;s create two new files <code>header.php<\/code> and <code>footer.php<\/code> in the <code>inc<\/code> directory:<\/p>\n\n\n\n<p><code>header.php<\/code><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;!DOCTYPE html&gt;\n&lt;html lang=<span class=\"hljs-string\">\"en\"<\/span>&gt;\n\t&lt;head&gt;\n\t\t&lt;meta charset=<span class=\"hljs-string\">\"UTF-8\"<\/span> \/&gt;\n\t\t&lt;meta name=<span class=\"hljs-string\">\"viewport\"<\/span> content=<span class=\"hljs-string\">\"width=device-width, initial-scale=1.0\"<\/span> \/&gt;\n\t\t&lt;title&gt;PHP <span class=\"hljs-keyword\">include_once<\/span>&lt;\/title&gt;\n\t&lt;\/head&gt;\n\t&lt;body&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><code>footer.php<\/code><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>index.php<\/code> file, if you include the <code>header.php<\/code> file twice, you&#8217;ll see that the page will have two headers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">'inc\/header.php'<\/span> <span class=\"hljs-meta\">?&gt;<\/span>\n<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">'inc\/header.php'<\/span> <span class=\"hljs-meta\">?&gt;<\/span>\n\n&lt;h1&gt;PHP <span class=\"hljs-keyword\">include_once<\/span> Demo&lt;\/h1&gt;\n\n\n<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">'inc\/footer.php'<\/span> <span class=\"hljs-meta\">?&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To avoid including a file more than once, you can use the <code>include_once<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">include_once<\/span> <span class=\"hljs-string\">'path_to_file'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>include_once<\/code> behaves like the <code>include<\/code> statement except that if the file is included again, the <code>include_once<\/code> won&#8217;t load the file and returns <code>true<\/code>.<\/p>\n\n\n\n<p>Simply put, the <code>include_once<\/code> loads the file just once, regardless of how many times the file is included.<\/p>\n\n\n\n<p>In the example above, if you use the <code>include_once<\/code> construct, the script will work properly:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">include_once<\/span> <span class=\"hljs-string\">'inc\/header.php'<\/span> <span class=\"hljs-meta\">?&gt;<\/span>\n<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">include_once<\/span> <span class=\"hljs-string\">'inc\/header.php'<\/span> <span class=\"hljs-meta\">?&gt;<\/span>\n\n&lt;h1&gt;PHP <span class=\"hljs-keyword\">include_once<\/span> Demo&lt;\/h1&gt;\n\n<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">include_once<\/span> <span class=\"hljs-string\">'inc\/footer.php'<\/span> <span class=\"hljs-meta\">?&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='why-use-the-php-include_once-construct'>Why use the PHP include_once construct <a href=\"#why-use-the-php-include_once-construct\" class=\"anchor\" id=\"why-use-the-php-include_once-construct\" title=\"Anchor for Why use the PHP include_once construct\">#<\/a><\/h2>\n\n\n\n<p>Image that you have a file called <code>index.php<\/code> that loads two other files:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Logger.php<\/li>\n<li>Database.php<\/li>\n<\/ul>\n\n\n\n<p>The <code>Database.php<\/code> file also loads the <code>Logger.php<\/code> file. In this case, the <code>Logger.php<\/code> file is used twice, once in the <code>Database.php<\/code> file and another in the <code>index.php<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" class=\"wp-image-916\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/04\/php-include_once.svg\" alt=\"php include_once\" \/><\/figure>\n<\/div>\n\n\n\n<p>In this case, you need to use the <code>include_once<\/code> construct to load the <code>Logger.php<\/code> file to make it work properly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the PHP <code>include_once<\/code> statement to load a file once.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"896\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-include_once\/\"\n\t\t\t\tdata-post-title=\"PHP include_once\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"896\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-include_once\/\"\n\t\t\t\tdata-post-title=\"PHP include_once\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the PHP include_once construct to include a file once<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":74,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-896","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=896"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/896\/revisions"}],"predecessor-version":[{"id":3136,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/896\/revisions\/3136"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}