Publishing System Settings Logout Login Register
A Helpful Tutorial on require_once()
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on July 26th, 2007
18356 views
PHP Coding
A Tutorial on the Functions of require_once()

Welcome to my tutorial on how the require_once() PHP function works!  This is a very simple yet very handy function that is commonly used on large sites to create a style or navigation update - and they only have to change the code on one page and the rest is automatically updated!

A. The Transformation

The require_once() function transforms into the content that would normally be displayed on the page listed within the perimeters.

For example, if you wrote:
<?php
require_once("http://www.example.com/examplepage.php");
?>


And "http://www.example.com/examplepage.php" contained:
<?php
echo 'Hello!  Welcome to my tutorial!';
?>


Then the require_once() would transform into and display "Hello!  Welcome to my tutorial!".

B. The To-Be-Displayed Location

The require_once() syntax is very simple:
require_once(location)

Location: The URL of the code to be displayed.

The require_once() functions puts everything on the page of the specified location right where you put the require_once() code.  The reason why (in the example in section A) the only thing displayed on the page would be "Hello! Welcome to my tutorial!" is because PHP code does not show up when you view the page source, and thus nobody can ever tell you used require_once()!

So, the require_once() did copy over everything on examplepage.php, the PHP code is just hidden, as it is supposed to be/

It is advised that you always put the complete URL in the require_once() perimeter ("http://www.yoursite.com/examplepage.php" and not just "examplepage.php") because URLs build on eachother based on their current location.  If you put the entire URL in the perimeters, then the code will know exactly where to look for what code to carry over.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Rcty

Hi!

I don't write many tutorials, and I'm really good at getting into PHP problems that even my really experienced PHP coding friends have to scour to find! However, I promise that I put legit content on my tutorials. :)
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top