School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,023 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,185 people online right now. Registration is fast and FREE... Join Now!




Using Include and Require

 
Reply to this topicStart new topic

> Using Include and Require, An introduction to using include and require in PHP

Seanmcn
Group Icon



post 22 Sep, 2009 - 01:26 PM
Post #1


Today we are going to learn all about include and require! Include and require are handy to use not only in your php applications, but in general website design for sidebar’s and what not! So.. yeah.. ph34r.gif Moving on!

Include and Require include a file within another PHP script. There are four ways of doing this:
CODE

<?php
include();
include_once();
require();
require_once();
?>


Now using them is seriously simple. Lets say you have sidebar.php with your links? All you have do is put the following in your sidebar of your website:
CODE
<?php include(”sidebar.php”); ?>


Then you can have sidebar.php which includes links
Example:
CODE

<a href="index.php">Home </a>
<a href="about.php"> About </a>
<a href="contact.php">Contact</a>

One thing to remember is how to control directories in PHP. For instance, if my file “sidebar.php” is in a folder called includes you put in:
CODE
<?php include(”includes/sidebar.php”); ?>


Or to take another route of it, if my file “sidebar.php” is in the /includes directory and the file I want the sidebar to show on is in a folder of that: /data I would put in:
CODE
<?php include(”../includes/sidebar.php”); ?>


Now why are there four different ways to include them I hear you ask! Well they are pretty simple.

Include and Require are practically the same, apart from the way they give out errors. Include will continue parsing your script and just shove out an error. Require however will stop the whole script to give you an error. Really it’s all down to what you want.

Include_once and require_once you probably guess only allow you to include the file once. This is not only more secure but stops from silly errors where the file was just included and variables were overwritten halfway through the script. Again they give out the same errors as include and require.

Note: Include_once is meant to be significantly faster than require_once in larger projects.

So lets go over one of each!
CODE
<?php include(”sidebar.php”); ?>

CODE
<?php include_once(”sidebar.php”); ?>

CODE
<?php require(”sidebar.php”); ?>

CODE
<?php require_once(”sidebar.php”); ?>


We really are spoilt for choice aren’t we! wink2.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 08:13AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month