Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,624 members, 7,820,210 topics. Date: Tuesday, 07 May 2024 at 11:26 AM

PHP File Inclusion - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / PHP File Inclusion (1235 Views)

Repairs All Corrupt PST Files In A New File / Pls,help Me Unlock Crack Or Remove This Password Protected File / Can I Create .php File Using "Fopen" Function. (2) (3) (4)

(1) (Reply) (Go Down)

PHP File Inclusion by maekhel(m): 1:52pm On Feb 08, 2015
There are times you might want to reference a particular file in another file. These is mostly common in OOP. PHP provides function which can be used to reference another file from a calling file. These function are include() and require() and we will be looking at them shortly. These functions help in creating functions, headers, footers, or elements that can be reused on multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. If there is any change required then instead of changing thousand of files just change included file.
The Include Function
The include() takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution. The include() has another variant called include_once(). What include_once does is to make sure the file is only included once so as to avoid referencing same file multiple times.

<?php
include('header.php');
?>

<?php
include_once('footer.php');
?>
The Require Function
The require() function takes all the text in a specified file and copies it into the file that uses the include function. Unlike the include(), the require() will terminate the execution of the script if there is any problem in loading the file. The require() also has another variant called the require_once(). This does exactly as the include_once does: make sure the file is only referenced once.

<?php
require('db.php');
?>

<?php
require_once('import.php');
?>
Conclusion
Now you have seen how to make use of the includes functions provided by PHP and seen their difference. I recommend using the require_once() when you are including a file because the script should not continue execution while your included failed.

I hope you find this tutorial helpful. You can leave your comments, questions and suggestions in the comment box below.
source: http://tutorialslodge.com/php-file-inclusion/
Re: PHP File Inclusion by tresz: 7:13pm On Feb 08, 2015
Good insight for newbies, but I agree not with you on supporting the use of require_once()
What if you wanted to include ad? If course I'll use include() cuz I want it execution to proceed if the file doesn't exists + that I might still call it in the same script.
So it depends on what and how you want to solve a problem.
Re: PHP File Inclusion by maekhel(m): 8:16pm On Feb 08, 2015
tresz:
Good insight for newbies, but I agree not with you on supporting the use of require_once()
What if you wanted to include ad? If course I'll use include() cuz I want it execution to proceed if the file doesn't exists + that I might still call it in the same script.
So it depends on what and how you want to solve a problem.
u right there
Re: PHP File Inclusion by CRAZYMADMAN(m): 8:52pm On Feb 10, 2015
Hello guys, what's wrong with this function



function testRequire($file) {
return require_once ($file .'html');

}

Re: PHP File Inclusion by 53cur3m0d3(m): 1:06am On Feb 11, 2015
I prefer using the include()
Re: PHP File Inclusion by CRAZYMADMAN(m): 12:14pm On Feb 13, 2015
CRAZYMADMAN:
Hello guys, what's wrong with this function





?
Re: PHP File Inclusion by 53cur3m0d3(m): 12:15pm On Feb 15, 2015
CRAZYMADMAN:
Hello guys, what's wrong with this function




Me thinks U're not invoking the function after setting it up. What exactly is that code supposed to do? What error message do U keep getting? I'm trying not to assume.
Re: PHP File Inclusion by CRAZYMADMAN(m): 9:03pm On Feb 15, 2015
53cur3m0d3:

Me thinks U're not invoking the function after setting it up. What exactly is that code supposed to do? What error message do U keep getting? I'm trying not to assume.

the problem is that when you try to use it to include a html file, it takes it to the very top of the page thereby disrupting any template structure already in place! I have gotten the solution, though
Re: PHP File Inclusion by 53cur3m0d3(m): 12:38am On Feb 16, 2015
CRAZYMADMAN:


the problem is that when you try to use it to include an html file, it takes it to the very top of the page thereby disrupting any template structure already in place! I have gotten the solution, though
Great... U might wanna share though
Re: PHP File Inclusion by CRAZYMADMAN(m): 8:02am On Feb 16, 2015
53cur3m0d3:

Great... U might wanna share though




function testRequire($file) {
ob_start();
require_once ($file .'html');
return ob_get_clean();

}
Re: PHP File Inclusion by 53cur3m0d3(m): 9:31am On Feb 16, 2015
CRAZYMADMAN:





function testRequire($file) {
ob_start();
require_once ($file .'html');
return ob_get_clean();

}
The Output Buffering function! Thanks...
Re: PHP File Inclusion by CRAZYMADMAN(m): 11:02am On Feb 16, 2015
53cur3m0d3:

The Output Buffering function! Thanks...


don't mention it.


But that gave enough headaches sha

1 Like

Re: PHP File Inclusion by Nobody: 4:09pm On Feb 18, 2015
php's version of import

(1) (Reply)

What Every DBA Needs To Know About The New Mysql 5.7! / What's The Largest Indigenous ICT Firm In Lagos / Csharper Interested In Web Development.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.