🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

PHP 7 Intermediate Essential Guide- PHP include_once & require_once

In this blog, I am going to Explain How to include_once & require_once in PHP. So within PHP, It’s actually possible to include PHP people files within other PHP files. It means when you use include_once & require_once more than one in any PHP file and you calling script it will be shown or print only one time if found more than once then it will be ignored.

So I’ve created a few other PHP files. And typically we’re going to use this for header footer and different files. So as you can see here within the header I’ve got what’s called page header and I’ve actually included some code in here as well.

And the same thing for the footer so got the page footer so let’s just writing that code into “footer.php“. And I’m also including some I should call the void then. And setting a variable called footer with this is the footer.

So now I can go over to my main index page and if I had an “header.php” set up so I’ve got my header here and title and then I’ve got my body. And in this body, I always wanted to have a header. And I want to have a footer so these are just going to be content headers and footers. Normally I know what you’re probably thinking well why wouldn’t I put this into the header and this in the footer so he can do that as well.

First and then I’m also going to call out that variable that’s being set up within PHP. So we’ll just see what our output is and we can see that we get that initial content and then we get the value of head.
There are also some additional ones where you could include it once. So if we had something like require or include and again to work almost identically.

So that’s the difference between required once and require. And the same thing for include and include once. So they’re going to work relatively the same way. Including the requires.

<?php
// name of file is header.php
$header = "This is header section"; //$header is a variable
echo $header; //echo for print
?>
<?php
// name of file is index.php
include_once('header-footer/header.php'); //include header.php in this file
include_once('header-footer/header.php'); // more than one
echo '<br>'; // this is break
echo '<b>This is body section</b>';
echo '<br>'; // this is break
require_once('header-footer/footer.php'); //include footer.php
require_once('header-footer/footer.php'); //more than one
// echo $header;
?>
view raw index.php hosted with ❤ by GitHub

Output-

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.