<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chris Clarke&#039;s Blog &#187; tutorial</title>
	<atom:link href="http://sorrowfulunfounded.com/tags/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://sorrowfulunfounded.com</link>
	<description>Official Weblog of Christopher Clarke</description>
	<lastBuildDate>Thu, 10 Nov 2011 04:32:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Introduction to PHP: Includes</title>
		<link>http://sorrowfulunfounded.com/blog/2009/06/11/introduction-to-php-includes/</link>
		<comments>http://sorrowfulunfounded.com/blog/2009/06/11/introduction-to-php-includes/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 12:24:29 +0000</pubDate>
		<dc:creator>Chris Clarke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[include_once]]></category>
		<category><![CDATA[incude]]></category>
		<category><![CDATA[require]]></category>
		<category><![CDATA[require_once]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://sorrowfulunfounded.com/?p=358</guid>
		<description><![CDATA[This tutorial will introduce you to the PHP include statement, and its almost identical sibling, require. The include statement is used to insert the contents of one file into the contents of another. Usage: 1 2 3 &#60;?php include &#34;/path/to/file.php&#34;; ?&#62; The included file is essentially merged with the file in which the include takes place, [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will introduce you to the <a href="http://au2.php.net/manual/en/function.include.php">PHP include</a> statement, and its almost identical sibling, require.</p>
<p>The include statement is used to insert the contents of one file into the contents of another.<br />
<span id="more-358"></span><br />
<strong>Usage:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;/path/to/file.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The included file is essentially merged with the file in which the include takes place, and the result in effect is one file containing the contents of both.</p>
<p>For example, we have two files. One is called &#8220;functions.php&#8221;, and the other, &#8220;display.php&#8221;. We are going to include &#8220;functions.php&#8221; in to &#8220;display.php&#8221;.</p>
<p>The contents of each script will be as follows.</p>
<p><strong>functions.php:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> nice_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/y'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>display.php:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'functions.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Today\'s Date is: '</span><span style="color: #339933;">,</span>nice_date<span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Essentially, what we are saying is that the contents of display.php should actually be:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* including functions.php */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> nice_date<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/y'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* finished including functions.php */</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Today\'s Date is: '</span><span style="color: #339933;">,</span>nice_date<span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>and the output is as so:</p>
<blockquote><p>Today&#8217;s Date is 11/06/09.</p></blockquote>
<p>You will notice that the PHP opening and closing tags (&lt;?php and ?&gt;) were maintained.PHP automatically closes its tags before and opens them after the inclusion has taken place. This is because PHP does not assume that the file included only contained PHP, for example it may only contain HTML or CSS. </p>
<p>The code within &#8220;functions.php&#8221; was executed just as if it had been in the PHP file containing the include statement in the first place.  &#8220;display.php&#8221; has access to any methods/functions, variables and other things defined in &#8220;functions.php&#8221;, and &#8220;functions.php&#8221; has access to any of those which are specified in &#8220;display.php&#8221; or other files it might include prior to &#8220;functions.php&#8221; inclusion.</p>
<p><strong>How does the require statement differ from the include statement?</strong></p>
<p>With include, if the file does not exist, your PHP script will not halt, and will continue to execute until it reaches the end. In the case of require, the script will cease execution and a fatal error will occur if the file cannot be found. You should use require to include any file you absolutely depend on like a &#8220;config.php&#8221; file containing your database access information.</p>
<p>In usage, require only differs in the text used. It is otherwise identical to include.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require</span> <span style="color: #0000ff;">&quot;/path/to/file.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>What about require_once and include_once? How are they different?</strong></p>
<p>Appending _once to either require or include will only allow the file to be included once. Future include statements referencing said file will not get included again.</p>
<p>That is the conclusion of my first tutorial &#8211; an introduction to PHP includes. If you have any questions, corrections, suggestions for tutorials, or general comments, please feel free to comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://sorrowfulunfounded.com/blog/2009/06/11/introduction-to-php-includes/feed/</wfw:commentRss>
		<slash:comments>158</slash:comments>
		</item>
	</channel>
</rss>

