Posted on July 26th, 2007
18361 views
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 And "http://www.example.com/examplepage.php" contained: <?php 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. |