<?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>Learn PHP Online &#187; PHP Basics</title>
	<atom:link href="http://www.learnphponline.com/category/php-basics/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learnphponline.com</link>
	<description>Learn PHP Online</description>
	<lastBuildDate>Wed, 06 May 2009 01:02:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How To Find The Current URL In PHP</title>
		<link>http://www.learnphponline.com/php-basics/how-to-find-the-current-url-in-php</link>
		<comments>http://www.learnphponline.com/php-basics/how-to-find-the-current-url-in-php#comments</comments>
		<pubDate>Sun, 05 Apr 2009 19:04:11 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[find current url]]></category>
		<category><![CDATA[path to script]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[query string]]></category>
		<category><![CDATA[request uri]]></category>
		<category><![CDATA[server_name]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=114</guid>
		<description><![CDATA[How to find different components of the current URL in PHP.]]></description>
			<content:encoded><![CDATA[<p>In your valiant conquest of the web development industry, you will notice that some scripts will require that you know the current URL the user is browsing to provide certain services. A prime example would be in user management- where we make use of query strings to keep track of users. More practical solutions may even demand that we find the current URL to display relevant ads and increase conversion rates.</p>
<p>PHP has set forth certain global variables that makes this process painfully easy. We will be experimenting with several methods of finding certain parts in the URL within this tutorial. Respectively, they are:</p>
<ol>
<li><a href="#domain">Finding the current domain</a></li>
<li><a href="#script">Finding the path to the script</a></li>
<li><a href="#query">Finding the query string (if any)</a></li>
<li><a href="#uri">Using a special short cut method to tie things together</a></li>
</ol>
<h3 id="domain">Finding The Current Domain In PHP</h3>
<p>If you need the current domain, you can use this neat little snipped below:</p>
<pre>&lt;?php
</span><span style="color: rgb(255, 128, 0);"># Using HTTP_HOST

</span><span style="color: rgb(0, 0, 187);">$domain </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'HTTP_HOST'</span><span style="color: rgb(0, 119, 0);">];
echo </span><span style="color: rgb(0, 0, 187);">$domain</span><span style="color: rgb(0, 119, 0);">;
</span><span style="color: rgb(0, 0, 187);">?&gt;</span></pre>
<p>If we were to use this directly on this page, the output would be <strong>learnphponline.com</strong> – notice that it does not include the &#8216;http://&#8217; or &#8216;www.&#8217; prefixes. If you are trying to make a link, you could do so by concatenating these prefixes onto the HTTP_HOST server variable.</p>
<h3 id="script">Finding The Path To The Current Script</h3>
<p>If you need to link to the current page, we use the SCRIPT_NAME server variable. We see this in use a lot more than you would think. WordPress installations will link article titles to the same page for several reasons. First, it keeps things user friendly- but it is also great for search engine optimization. Don&#8217;t be afraid to follow their example such as the snippet below shows.</p>
<pre>&lt;?php
</span><span style="color: rgb(255, 128, 0);"># Using SCRIPT_NAME

</span><span style="color: rgb(0, 0, 187);">$path </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'SCRIPT_NAME'</span><span style="color: rgb(0, 119, 0);">];

echo </span><span style="color: rgb(221, 0, 0);">"Path To Script Example: &lt;a href='$path'&gt;An Article Title&lt;/a&gt;"</span><span style="color: rgb(0, 119, 0);">;

</span><span style="color: rgb(0, 0, 187);">?&gt;</span></pre>
<p>You will notice that the domain section and query string is left out. Instead we get the script path that links nicely to the current page.</p>
<h3 id="query">Finding The Query String In a URL</h3>
<p>The query string is important in passing variables or authorization information across several different pages in your website. You have probably noticed this before when logging into your favorite website and seen something to this effect: &#8220;TheWebsite.com/users/index.php?name=YourName&#8221; </p>
<p>Making a query string is actually quite easy. Make a simple PHP file and create a link to the current file, yet concatenate a ternary symbol and assign a variable like this:</p>
<ul>
<li>&lt;a href=&#8217;www.yoururl.com/index.php?variable=value&#8217;>Test it!&lt;/a></li>
</ul>
<p>This won&#8217;t do anything since we haven&#8217;t coded anything to work with the variable. But it will allow us to test the server variable below.</p>
<pre>&lt;?php
</span><span style="color: rgb(255, 128, 0);"># Using QUERY_STRING

</span><span style="color: rgb(0, 0, 187);">$queryString </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'QUERY_STRING'</span><span style="color: rgb(0, 119, 0);">];

echo </span><span style="color: rgb(221, 0, 0);">"Query: " </span><span style="color: rgb(0, 119, 0);">. </span><span style="color: rgb(0, 0, 187);">$queryString</span><span style="color: rgb(0, 119, 0);">;

</span><span style="color: rgb(0, 0, 187);">?&gt;</span></pre>
<h3 id="uri">Finding The Current URL With Request URI</h3>
<p>If you are using MOD REWRITE to make your URLs more user-friendly, there is still a way to get the original URL. By using the REQUEST_URI server variable, we can get the URL given to access the page. So be definition, we bypass any rewrite rules.</p>
<pre>&lt;?php
</span><span style="color: rgb(255, 128, 0);"># Using REQUEST_URI

</span><span style="color: rgb(0, 119, 0);">echo </span><span style="color: rgb(221, 0, 0);">"http://" </span><span style="color: rgb(0, 119, 0);">. </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'HTTP_HOST'</span><span style="color: rgb(0, 119, 0);">]  . </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'REQUEST_URI'</span><span style="color: rgb(0, 119, 0);">];

</span><span style="color: rgb(0, 0, 187);">?&gt;</span></pre>
<p>This saves a little bit of space over the previous examples, since REQUEST_URI can replace the script path and query string server variables. This is best used when you don&#8217;t need these variables separated, which you commonly do.</p>
<h3>Security Issues To Consider</h3>
<p>There are many ways to get the current URL, but the ones mentioned here are the safest. The server variable PHP_SELF is an example of a method that can result in cross-scripting attacks (XSS). Instead make sure you use the SCRIPT_NAME variable as we did above in our examples.</p>
<p>Also make note that header information can be faked. Any variable that includes &#8220;HTTP&#8221; in it has a potential to be untrustworthy data. There are always verification methods and alternatives to these pitfalls, so you aren&#8217;t without options.</p>
<p>A humorous example of how HTTP headers can be faked is with certain security software packages that rewrite referrer information that websites use for analytics. By setting your referrer field to something such as &#8220;FBI&#8221; or &#8220;CIA&#8221; you can effectively give the statistics-conscious webmaster a nice scare.</p>
<h4>Closing Comments</h4>
<p>As a last note, be sure to encode any URL information you make use of if you are using these server variables for your own coding experiments. Otherwise malicious users may take advantage of your inept security tactics and cause havoc on your database.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/php-basics/how-to-find-the-current-url-in-php/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Find Odd Or Even Numbers</title>
		<link>http://www.learnphponline.com/php-basics/find-odd-or-even-numbers</link>
		<comments>http://www.learnphponline.com/php-basics/find-odd-or-even-numbers#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:08:44 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[php even numbers]]></category>
		<category><![CDATA[php math]]></category>
		<category><![CDATA[php odd numbers]]></category>
		<category><![CDATA[php tutorial]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=33</guid>
		<description><![CDATA[Finding odd and even numbers in PHP.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: There comes the time in every web developer&#8217;s escapades that he or she needs to find an odd or even number. Perhaps for use in alternating color schemes in blog comments, or even for more serious applications such as error checking. Whatever the case may be, we can write up a simple function or IF statement to do the hard work for us.</p>
<h3>Find An Odd Or Even Number With The IF Statement</h3>
<p>We can check for whether or not a number is even or odd by using the modulus operator seen below.</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">7 </span><span style="color: #007700;">% </span><span style="color: #0000bb;">2</span><span style="color: #007700;">) {
  echo </span><span style="color: #dd0000;">"7 is odd"</span><span style="color: #007700;">;
} else {
  echo </span><span style="color: #dd0000;">"7 is even"</span><span style="color: #007700;">;
}  

</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p>We interpret the first line as &#8220;Divide 7 by two, and check for a remainder.&#8221; If there is a remainder (in our case we have 3.5, so a remainder of 0.5), then the number is odd. If there isn&#8217;t a remainder while dividing by two, then that number is even.</p>
<p>You could change the three sevens in the above script to eights, and you will notice the script outputs the number is even, since 8 divided by two is 4 with no remainder.</p>
<h3>Checking For Odd And Even Numbers With A PHP Function</h3>
<p>We can clean up so code here by using a function to do the dirty work for us. First we&#8217;ll stick the if statement into a function and make sure it works, then we&#8217;ll optimize our code once we get the concept up and running.</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">function </span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">) {
  if (</span><span style="color: #0000bb;">$num </span><span style="color: #007700;">% </span><span style="color: #0000bb;">2 </span><span style="color: #007700;">== </span><span style="color: #0000bb;">0</span><span style="color: #007700;">) {
  return </span><span style="color: #0000bb;">false</span><span style="color: #007700;">;
 } else {
    return </span><span style="color: #0000bb;">true</span><span style="color: #007700;">;
  }
}
</span><span style="color: #ff8000;">############################

</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">4</span><span style="color: #007700;">)) {
 echo </span><span style="color: #dd0000;">"This number is odd."</span><span style="color: #007700;">;
} else {
  echo </span><span style="color: #dd0000;">"This number is even."</span><span style="color: #007700;">;
}

</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p>The function will do exactly what we just did with our IF statement previously, only this time we can reuse our code and save quite a bit of space. We supplied the <strong>is_odd</strong> function with a plain number; if we wanted we could put in a variable. In fact, we&#8217;ll do just that while we optimize our code in the next example.</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">function </span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">){
 return </span><span style="color: #0000bb;">$num </span><span style="color: #007700;">% </span><span style="color: #0000bb;">2 </span><span style="color: #007700;">== </span><span style="color: #0000bb;">0 </span><span style="color: #007700;">? </span><span style="color: #0000bb;">false</span><span style="color: #007700;">:</span><span style="color: #0000bb;">true</span><span style="color: #007700;">;
}

</span><span style="color: #0000bb;">$number_To_Check </span><span style="color: #007700;">= </span><span style="color: #0000bb;">3</span><span style="color: #007700;">;
if (</span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$number_To_Check</span><span style="color: #007700;">)) {
  echo </span><span style="color: #dd0000;">"$number_To_Check is odd."</span><span style="color: #007700;">;
} else {
  echo </span><span style="color: #dd0000;">"$number_To_Check is even."</span><span style="color: #007700;">;
}
</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p>This time around we are saving even more space by using the ternary symbol (?). In this instance the left side of the colon is what gets executed if the statement is true, and the right side is what gets executed if the statement is false. Think of the colon as an &#8220;else&#8221; statement.We can use some pseudo-code to help understand this more:</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">function </span><span style="color: #0000bb;">odd_check</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">){
 (</span><span style="color: #0000bb;">Is this statement true</span><span style="color: #007700;">) ? </span><span style="color: #0000bb;">yes  </span><span style="color: #007700;">:  </span><span style="color: #0000bb;">no</span><span style="color: #007700;">;
}
</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p>Back to our case example: since the statement checks for an even number, when the statement is true we return the value &#8220;false&#8221; to indicate it isn&#8217;t an odd number.</p>
<h3>Further Optimization of The Odd Number-Finding Function</h3>
<p>Want to really impress the ladies? Try showing them the following function to find odd and even numbers.</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">function </span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">( </span><span style="color: #0000bb;">$num</span><span style="color: #007700;">)
{
  return( </span><span style="color: #0000bb;">$num </span><span style="color: #007700;">&amp; </span><span style="color: #0000bb;">1 </span><span style="color: #007700;">);
}
</span><span style="color: #ff8000;">########################

</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">7</span><span style="color: #007700;">)) {
    echo </span><span style="color: #dd0000;">"7 is odd"</span><span style="color: #007700;">;
} else {
    echo </span><span style="color: #dd0000;">"7 is even"</span><span style="color: #007700;">;
}
</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p>This time around we took the equation out. We can do this through using the AND operator (&amp;). If you aren&#8217;t up to speed on binary, consult the chart below to see how we count to five. (Note that we will not go into depth on binary, just give you enough information to understand the AND process.)</p>
<p><img style="margin: 0px auto; display: block; width: 383px; height: 267px;" title="Count To 5 In Binary" src="../binary-counting.jpg" alt="" /></p>
<p>Each place in the above numbers represents a different value. The furthest right number, as you might have guessed represents &#8220;1,&#8221; the next &#8220;2,&#8221; the next &#8220;4,&#8221; and so on. But notice something even more interesting: all the odd numbers have a &#8220;1&#8243; while the even numbers don&#8217;t!</p>
<p>The AND operator is going to compare two binary numbers. If both numbers have a &#8220;true&#8221; (the number 1) value, then the result is true. If either numbers have a 0 (or &#8220;false&#8221;), or both numbers are false, the resulting number is 0. Consult the diagram below for more information.</p>
<p><img style="margin: 0px auto; display: block; width: 383px; height: 267px;" title="Binary AND" src="../binary-and.jpg" alt="" /></p>
<p>Back to our equation. If the ($int &amp; 1) check returns true, the number is odd. Let&#8217;s say we put an odd number such as 3 in. We will compare it against 1, which is already an odd number. 0011 and 0001 can be used in an AND equation to get the result of 1, since both values are 1 (and thus true).</p>
<p>You don&#8217;t necessarily have to understand this jargon to make use of the function- but it&#8217;s good to get the basics of binary down if you haven&#8217;t already.</p>
<h4>Closing Comments</h4>
<p>Now that you can find odd and even numbers, you can accomplish all types of artsy things. You&#8217;ll see this in effect most often on blogs that alternate the background colors of comments to make for easy reading. Lyrics websites do the same thing, and you can even use the same principle to use in more complex equations (which we won&#8217;t get into in the scope of this article).</p>
<p>Try running the following code just for fun:</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">function </span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">)
{
  return( </span><span style="color: #0000bb;">$num</span><span style="color: #007700;">&amp; </span><span style="color: #0000bb;">1 </span><span style="color: #007700;">);
}

for (</span><span style="color: #0000bb;">$counter </span><span style="color: #007700;">= </span><span style="color: #0000bb;">0</span><span style="color: #007700;">; </span><span style="color: #0000bb;">$counter </span><span style="color: #007700;">&lt;= </span><span style="color: #0000bb;">20</span><span style="color: #007700;">; </span><span style="color: #0000bb;">$counter</span><span style="color: #007700;">++) {
  if (</span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">)) {
    echo </span><span style="color: #dd0000;">"&lt;div style='background-color:black;width:800px;height:30px;margin:0px auto;'&gt; &lt;/div&gt;"</span><span style="color: #007700;">;
  } else {

    echo </span><span style="color: #dd0000;">"&lt;div style='background-color:red;width:800px;height:30px;margin:0px auto;'&gt; &lt;/div&gt;"</span><span style="color: #007700;">;
  }
}
</span><span style="color: #0000bb;">?&gt;</span></span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/php-basics/find-odd-or-even-numbers/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>PHP Comments Tutorial</title>
		<link>http://www.learnphponline.com/php-basics/php-comments-tutorial</link>
		<comments>http://www.learnphponline.com/php-basics/php-comments-tutorial#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:04:42 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[commenting in php]]></category>
		<category><![CDATA[php comments]]></category>
		<category><![CDATA[php tutorial]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=28</guid>
		<description><![CDATA[How to make use of comments within PHP.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: Arrogance runs thick among the LearnPHPOnline.com programmers- we hate doing more work than we have to! Commenting is something that is normally ignored by new or lazy programmers, but both groups soon find commenting saves much more time than it takes.</p>
<p>Comments may only viewed by those who make changes to the code itself- the PHP engine doesn&#8217;t parse anything designated as a comment. A good example of this is seen below, where the first line isn&#8217;t being output to the browser, while the second is.</p>
<p><!-- code block 1 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">An Example Of The PHP Comment Syntax</p>
<pre><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">// This is an example of a comment- this is not being parsed!

</span><span style="color: #007700;">echo </span><span style="color: #dd0000;">"This is being parsed by the PHP engine!"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">?&gt;</span>
</span></pre>
<p><!-- code block 1 -->We, as able-bodied programmers and individuals, should invest our time into commenting for three primary reasons.</p>
<ol>
<li><strong>Employment</strong> &#8211; If you are working for an employer, or are planning to release your source code to the public, you will need to make use of comments. Employers demand it because if they were to lose you as an employee, they would have to hire someone new to peruse your code. This act with commenting can take hours, and without can take days. This same principle works with open source code, which needs to be readable by others so they know how to operate or modify your program correctly.</li>
<li><strong>Saving time</strong> &#8211; Even if you plan on keeping the code to yourself, you should make use of PHP comments because as your projects grow in scale, you will forget where you place certain bits of code or even what large blocks of code do. Most arrogant programmers do without comments at this stage, claiming they can remember the programs they make. In reality, a programmer will often come back to a certain code block months into the future, and quickly find they will need to take a few hours to reread their code and comment it properly.</li>
<li><strong>Troubleshooting</strong> &#8211; More intelligently, programmers use PHP comments to troubleshoot their applications. When trying to find which code block is causing an error, programmers can quickly section out new additions to the application with a comment block to see if the error subsides. If it does, that&#8217;s where the error lies. If not, the error lies elsewhere.</li>
</ol>
<h3>PHP Commenting Syntax</h3>
<p>You&#8217;ve finally come around, and you want to start commenting your code. Bravo! The first step is to learn how to use it. There are three different ways we can make comments; two of which can be seen below in the example.</p>
<p><!-- code block 2 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Two different Methods of Using PHP Comments</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">#This is an example comment

//This is another example comment

</span><span style="color: #007700;">echo </span><span style="color: #dd0000;">"Sentence 1."</span><span style="color: #007700;">; </span><span style="color: #ff8000;">//This is our first statement

</span><span style="color: #007700;">echo </span><span style="color: #dd0000;">"&lt;br /&gt;"</span><span style="color: #007700;">; </span><span style="color: #ff8000;">#This is a line break

</span><span style="color: #007700;">echo </span><span style="color: #dd0000;">"Sentence 2."</span><span style="color: #007700;">; </span><span style="color: #ff8000;">// This is our second statement

</span><span style="color: #0000bb;">?&gt;</span>
</span></pre>
<p><!-- code block 2 -->You&#8217;ll notice that both commenting styles, either the &#8220;#&#8221; or the &#8220;//&#8221; versions, are used on a single line. Also note we can use both behind an otherwise functional PHP statement, such as can be seen with our second half of the example. But if we wanted to format comments to span multiple lines, things would get tedious. Luckily, PHP developers can make use of a third way that spans multiple lines as seen below.</p>
<p><!-- code block 3 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Multi-Line Comments in PHP</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">#This is a

#single line comment

#on multiple lines.

/* This is an easier

way to write comments

on multiple lines */

</span><span style="color: #0000bb;">?&gt;</span>
</span></pre>
<p><!-- code block 3 -->In the above PHP commenting example we see that you can span multiple lines both ways, but the latter example is much easier to work with. These types of multiple line comments are usually placed at the top of a PHP file to explain what it&#8217;s for, what it accomplishes, and any specific information needed to run or edit the file correctly.</p>
<p>Some people like to showoff and get elaborate with their commenting styles. We&#8217;ll agree it&#8217;s great fun, since it actually helps out the readability of comments in many cases. Below is an example of what the LearnPHPOnline.com&#8217;s creative programming department came up with in all but a few seconds of their precious time.</p>
<p><!-- code block 4 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Fancy-Shmancy PHP Comments</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">#################################

####  My PHP File Version 2.2####

#################################

/* Be sure to edit the server 

password and username details 

to properly configure this 

script to work! */

</span><span style="color: #0000bb;">?&gt;</span>
</span></pre>
<p><!-- code block 4 -->Quite chauvinistic aren&#8217;t they?</p>
<h3>Using PHP Comments To Troubleshoot</h3>
<p>Troubleshooting is a vital part of programming, as we are sure the reader knows already. It&#8217;s always a great idea to take breaks every time a new code block is added to see if everything is still functional. If it isn&#8217;t, we have to get creative in how we find the root of the problem.</p>
<p>Below is an actual flawed script that we created. You will likely see the problem right off, but in a real-world environment, tracking such a problem down will be much more difficult.</p>
<p><!-- code block 5 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Troubleshooting PHP Code With Comments</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">//This script will output a variable to the browser

</span><span style="color: #0000bb;">$myVar1 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$myVar2 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix';

echo "</span><span style="color: #0000bb;">I want chicken</span><span style="color: #007700;">, </span><span style="color: #0000bb;">I want liver</span><span style="color: #007700;">,</span><span style="color: #dd0000;">" . $myVar1 . $myVar2 . " </span><span style="color: #0000bb;">please deliver</span><span style="color: #007700;">!</span><span style="color: #dd0000;">";

?&gt;</span>
</span></pre>
<p><!-- code block 5 -->When run, this script will give the error &#8220;Parse error: syntax error, unexpected T_STRING&#8221; &#8211; and even though the problem lies on line 4, it points us to line 6. So we&#8217;ll just comment out line 6, and we see that the problem still persists!</p>
<p><!-- code block 6 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Troubleshooting PHP Code With Comments</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">//This script will output a variable to the browser

</span><span style="color: #0000bb;">$myVar1 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$myVar2 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix';

#echo "</span><span style="color: #0000bb;">I want chicken</span><span style="color: #007700;">, </span><span style="color: #0000bb;">I want liver</span><span style="color: #007700;">,</span><span style="color: #dd0000;">" . $myVar1 . $myVar2 . " </span><span style="color: #0000bb;">please deliver</span><span style="color: #007700;">!</span><span style="color: #dd0000;">";

?&gt;</span>
</span></pre>
<p><!-- code block 6 -->Oh dear, it looks like the problem is still there once we run the application.</p>
<p>Now we&#8217;ll move the comment to line 4, and see what happens. Once we run the application, we see that the browser outputs this string &#8220;I want chicken, I want liver, meow mix please deliver!&#8221; We are missing the second meow mix, obviously, but the script still executed successfully. This means that the problem must lie on or near line 4. Upon further checking, we see that we used an apostrophe to close the variable assignment! Oops! Let&#8217;s fix it and see if it runs now.</p>
<p><!-- code block 7 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Troubleshooting PHP Code With Comments</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">//This script will output a variable to the browser

</span><span style="color: #0000bb;">$myVar1 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$myVar2 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix"</span><span style="color: #007700;">;

echo </span><span style="color: #dd0000;">"I want chicken, I want liver," </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$myVar1 </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$myVar2 </span><span style="color: #007700;">. </span><span style="color: #dd0000;">" please deliver!"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p><!-- code block 7 --><span style="color: #000000;">Success! We now have the correct string output to the browser.</span></p>
<h3>One Last Tip For PHP Comments</h3>
<p>One last thing we need to stress is to use PHP comments in selection structures, such as CASE or IF. If you have an IF statement, always be sure to comment what each branch does, even if you don&#8217;t use one of the choices. An example is below.</p>
<p><!-- code block 8 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP Comments And Selection Structures</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">//This script showcases comments in an IF structure

</span><span style="color: #0000bb;">$clientAge </span><span style="color: #007700;">= </span><span style="color: #0000bb;">18</span><span style="color: #007700;">;

if (</span><span style="color: #0000bb;">$clientAge </span><span style="color: #007700;">&lt; </span><span style="color: #0000bb;">18</span><span style="color: #007700;">) {

echo </span><span style="color: #dd0000;">"You are not able to view this material until you are 18 or older."</span><span style="color: #007700;">;

} else {

</span><span style="color: #ff8000;">//Don't show a notice

</span><span style="color: #007700;">}

</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p><!-- code block 8 --><span style="color: #000000;">Even though we don&#8217;t need a comment in the else branch, we should put one there because it does amazing things for readability. Once programmers start nesting IF statements inside each other, documenting each IF branch is absolutely vital in understanding how an application functions.</span></p>
<h4>Closing Comments</h4>
<p>Commenting is a vital part of programming, and it serves more purposes than what one would think as we can very well see. Always try to keep documentation on your code where possible, if not for yourself, then for your employers who don&#8217;t like seeing undocumented work from their employees.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/php-basics/php-comments-tutorial/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP Include Tutorial</title>
		<link>http://www.learnphponline.com/php-basics/php-include-tutorial</link>
		<comments>http://www.learnphponline.com/php-basics/php-include-tutorial#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:03:23 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Basics]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=26</guid>
		<description><![CDATA[How to include remote files within PHP 4 and PHP 5.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: The include statement built into PHP is going to save web developers a heap of time in making their web application. What we&#8217;re going to see in this tutorial is how we use the include statement to duplicate repetitive tasks, clean up code, and overall expand our knowledge on the PHP language. Onward!</p>
<h3>PHP Include Tutorial &#8211; Basics Of The Include Language Construct</h3>
<p>It should be noted that, much like the Echo statement in PHP, Include is not considered an actual function. Although it would behave like a function, we call it a language construct since it is &#8220;built into&#8221; PHP. We consider it an integral part of the language much like the IF statement! (You may notice some language constructs listed as functions- but this is only to improve documentation.)</p>
<p>The primary usage of this language construct in particular is to retrieve a remote file for inclusion into the currently running script. We are going to use the Include construct for three primary reasons:</p>
<ol>
<li><strong>Readability</strong> &#8211; We use the Include construct for replacing many lines of code with but one line of code. This increases the readability of an application, and ultimately lets us troubleshoot bugs quicker than ever before. We use the Include construct for replacing many lines of code with but one line of code. This increases the readability of an application, and ultimately lets us troubleshoot bugs quicker than ever before.</li>
<li><strong>Save Time</strong> &#8211; We save time by using the Include() construct in a very ingenious way. If we were to change a link in our navigation menu, we would have to update that same link hundreds of time on different subpages. But if we used an Include() construct, we could just edit one file and the results on other pages would be instant!</li>
<li><strong>Reliability</strong> &#8211; If an included file doesn&#8217;t exist or has malfunctioned, the server will tell us quite promptly. This is superb for troubleshooting! Error reporting is sometimes seen as a security risk, however, since it commonly shows semi-confidential information in the error.</li>
</ol>
<h3>PHP Include Tutorial &#8211; Examples Of Including A File</h3>
<p>Before we can do dabble with an example, we need to learn how the Include syntax works. Using the Include construct is actually quite easy; we just need to know the URL of the code to be input, wrap it in parenthesis, and add the include statement.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">An Example Of The PHP Include Syntax</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
    <span style="color: red;">include</span>("header.php");
    <span style="color: red;">include</span> 'footer.php';

    <span style="color: green;">// Each Way Is Correct, But The First Example Is More Readable</span>
<span style="color: blue;">?&gt;</span></pre>
<p>In the above example we are calling to two different files, in which both are located in the same directory as the page being viewed. Both statements will work, but we personally like the first example since we view it as more readable.</p>
<p>If the file we wanted to include is in the parent directory, we simply use the &#8220;<strong>../</strong>&#8221; notation as seen below.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Including A PHP File From A Parent Directory</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
    <span style="color: red;">include</span>("../header.php");

    <span style="color: green;">// Include A File From Parent Directory</span>
<span style="color: blue;">?&gt;</span></pre>
<p>Note that every time we use the &#8220;../&#8221; notation we go up one level. But things don&#8217;t have to get complicated! We can simplify things by simply using the entire URL as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">An Easier Way To Include A PHP File</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
    <span style="color: red;">include</span>("http://www.YourUrl.com/includes/header.php");

    <span style="color: green;">// Easy File Inclusion</span>
<span style="color: blue;">?&gt;</span></pre>
<h3>PHP Include Tutorial &#8211; An Include Example</h3>
<p>To get started we are going to need at least two files. First, we will take an already-made index.php file as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Contents Of index.php &#8211; Copy And Paste</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;html&gt;
&lt;title&gt;</span>A Basic PHP Website Using Include<span style="color: blue;">&lt;/title&gt;</span>

<span style="color: blue;">&lt;p</span> <span style="color: red;">style</span>="<span style="color: purple;">text-align: center;padding: 10px;"&gt;</span>
<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>="<span style="color: purple;">#</span>"&gt;Home<span style="color: blue;">&lt;/a&gt;</span>
<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>="<span style="color: purple;">#</span>"&gt;Subpage 1<span style="color: blue;">&lt;/a&gt;</span>
<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>="<span style="color: purple;">#</span>"&gt;Subpage 2<span style="color: blue;">&lt;/a&gt;</span>
<span style="color: blue;">&lt;/p&gt;</span>

<span style="color: blue;">&lt;p</span> <span style="color: red;">style</span>="<span style="color: purple;">text-align: center;border: 1px dotted blue;</span>"&gt;Welcome to our website! The links above are being used in includes- saving us time that can be best used for developing better features for this website.<span style="color: blue;">&lt;/p&gt;

<span style="color: blue;">&lt;/body&gt;
&lt;/html&gt;</span>

	</span></pre>
<p>Now create a file called &#8220;header.php&#8221; in another window. We are going to take out some of the contents of the index.php file and replace it with our include statement. Our header.php file should look like the following:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Contents Of header.php &#8211; Copy And Paste</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php
	&lt;a</span> <span style="color: red;">href</span>="#"&gt;Home<span style="color: blue;">&lt;/a&gt;
	&lt;a</span> <span style="color: red;">href</span>="#"&gt;Subpage 1<span style="color: blue;">&lt;/a&gt;
	&lt;a</span> <span style="color: red;">href</span>="#"&gt;Subpage 2<span style="color: blue;">&lt;/a&gt;
?&gt;</span></pre>
<p>Now we have two separate files. But we have a problem- how do we get the URLs from the header.php file into the index.php file? Simple! Review the code below to see where we put the include statement:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Contents Of header.php &#8211; Copy And Paste</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;html<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;title<span style="color: blue;">&gt;</span></span>A Basic PHP Website Using Include<span style="color: blue;">&lt;/title<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;body<span style="color: blue;">&gt;</span></span>

<span style="color: blue;">&lt;p</span><span style="color: red;"> style</span>="<span style="color: purple;">text-align: center;padding: 10px;</span>"<span style="color: blue;">&gt;</span>
<span style="color: red;">include</span>("<span style="color: purple;">header.php</span>"); ?<span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;/p</span><span style="color: blue;">&gt;</span>

<span style="color: blue;">&lt;p</span><span style="color: red;"> style</span>="<span style="color: purple;">text-align: center;border: 1px dotted blue;</span>"<span style="color: blue;">&gt;</span>
Welcome to our website! The links above are being used in includes- saving us time that can be best used for developing better features for this website.
<span style="color: blue;">&lt;/p</span><span style="color: blue;">&gt;</span>

<span style="color: blue;">&lt;/body<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;/html<span style="color: blue;">&gt;</span></span></pre>
<p>Now upload both files to a same directory and test it out! If you&#8217;d like, you can change around the contents of the header.php file, upload it to your web server, and see the changes in real-time. This may not seem like a big time saver with only one page to use it on, but if we were to have a website filled with 100 pages, this would be quite the little time saver!</p>
<h3>PHP Include Tutorial: Dealing With Variable Scope</h3>
<p>Variable scope sounds like a mean phrase to anyone who doesn&#8217;t know what it is. The scope of a variable is just how far it &#8220;reaches&#8221;- or basically where it can and can&#8217;t be accessed. Variable scope can be seen when looking at two separate PHP scripts. If they are not connected in any way, then the variables of the first script will not interfere with variables in the second. (And vice versa)</p>
<p>We deal with scope in the Include construct quite simply because we are merging two files- so if we are using the same variable name in both scripts, which one gets precedence?</p>
<p>In our previous example we are not dealing with scope since we aren&#8217;t using any variables. But we can change that with the following edits to both index.php and header.php:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">header.php &#8211; Edits To Show Variable Scope</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
  <span style="color: navy;">$home</span> = "<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>='#'<span style="color: blue;">&gt;</span>Home<span style="color: blue;">&lt;a<span style="color: blue;">&gt;</span></span>";
  <span style="color: navy;">$sub1</span> = "<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>='#'<span style="color: blue;">&gt;</span>Subpage 1<span style="color: blue;">&lt;a<span style="color: blue;">&gt;</span></span>";
  <span style="color: navy;">$sub2</span> = "<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>='#'<span style="color: blue;">&gt;</span>Subpage 2<span style="color: blue;">&lt;a<span style="color: blue;">&gt;</span></span>";
<span style="color: blue;">?</span><span style="color: blue;">&gt;</span></pre>
<p>We just declared our three variables as our links, so that we may see which variables take precedence in our index.php file. Our index.php file should be updated as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Contents Of index.php</p>
<pre style="border: 1px solid black; padding: 10px;">
<span style="color: blue;">&lt;html<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;title<span style="color: blue;">&gt;</span></span>A Basic PHP Website Using Include<span style="color: blue;">&lt;/title<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;body<span style="color: blue;">&gt;</span></span>

<span style="color: blue;">&lt;p</span><span style="color: red;"> style</span>="<span style="color: purple;">text-align: center;padding: 10px;</span>"<span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;?php</span> 

<span style="color: navy;">$home</span> = "";
<span style="color: navy;">$sub1</span> = "";
<span style="color: navy;">$sub2</span> = "";
<span style="color: red;">include</span>("header.php"); 

<span style="color: blue;">echo</span> "$home $sub1 $sub2";
?<span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;/p</span><span style="color: blue;">&gt;</span>

<span style="color: blue;">&lt;p</span><span style="color: red;"> style</span>="<span style="color: purple;">text-align: center;border: 1px dotted blue;</span>"<span style="color: blue;">&gt;</span>Welcome to our website! The links above are being used in includes- saving us time that can be best used for developing better features for this website.<span style="color: blue;">&lt;/p</span><span style="color: blue;">&gt;</span>

<span style="color: blue;">&lt;/body<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;/html<span style="color: blue;">&gt;</span></span></pre>
<p>So what do you think the script will output to the browser? As you can see, we declared each variable as an empty string before we included the file. But in the include of the file, we stated that the variables should contain links.</p>
<p>Upon testing this script, one can see that the output is going to be successful in displaying the correct navigation links. From this example it is seen that an included file will indeed overwrite variable values unless otherwise coded.</p>
<p>It should also be made clear that an include file is only going to have access to variables and functions that have been declared before the line used to include the file. In the following example we will declare a variable and reference it within the include file. (No need to test this out unless you want- you can easily see the results with the two short snippets below)</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">index.php &#8211; Testing Variable Scope</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
  <span style="color: navy;">$hello</span> = "hello world!";
  <span style="color: red;">include</span>("<span style="color: blue;">echo</span>.php");
?<span style="color: blue;">&gt;</span></pre>
<p>Now for the echo.php file:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">echo.php &#8211; Testing Variable Scope</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
  <span style="color: blue;">echo</span>  <span style="color: navy;">$hello</span>;
?<span style="color: blue;">&gt;</span></pre>
<p>Once this script is run, we will see that &#8220;hello world!&#8221; is output to the screen. If we were to put the variable after the include line, we wouldn&#8217;t get any results at all.</p>
<h3>PHP Include Tutorial &#8211; Two Major Security Concerns</h3>
<p>PHP is considered a generally secure language since it hides the contents of the PHP code from the user. (Go ahead and view the source of the HTML of pages we have created- you won&#8217;t see PHP code!) If we take this protection away, anyone can see the contents of our scripts. Luckily you have to make the mistake of not using the PHP extension for this to happen.</p>
<p>It is sometimes common to see some use plain text or even a .INC file type for their includes. If you use the wrong extension or remove the PHP tags from the header.php file we were working with, you&#8217;ll notice that the plain text is output to the screen if you try to access header.php directly. This goes to show that when we use includes, always remember to use the PHP tags to enclose the data if it is using any type of sensitive data. (Or any time at all, since it builds good programming practice!)</p>
<p>The second major security concern is including files from a remote location. If we are only using local resources on our website, such as template files we have created, there is no reason to fret. But if we are pulling information from other websites, which is sometimes illegal in many cases anyway, the remote website can inject code into our websites and get confidential information with ease. As such, developers should only agree to use includes from remote resources if they are to be trusted without doubt.</p>
<h4>Closing Comments</h4>
<p>As we get more familiar with the Include statement, we can get a little more skilled in the way we use it. From using Includes in functions or even using IF statements to selectively include correct files, there is much to expand on here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/php-basics/php-include-tutorial/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>PHP Echo Vs Print</title>
		<link>http://www.learnphponline.com/php-basics/php-echo-vs-print</link>
		<comments>http://www.learnphponline.com/php-basics/php-echo-vs-print#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:01:47 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[echo]]></category>
		<category><![CDATA[php echo]]></category>
		<category><![CDATA[php print]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[print]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=24</guid>
		<description><![CDATA[How to differentiate between the popular Echo and Print commands in PHP.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: We have established that PHP is a web development language that can make developers a hefty sum of money. Before we can start learning the language, we need to know a few things. What exactly is PHP, where did it come from, and what exactly does learning the language do for developers?</p>
<h3>PHP Echo Vs Print: Which Should Developers Use?</h3>
<p>The biggest triviality that plagues most newcomers to PHP is the fact that there are two commands that do (what appears to be) the exact same thing. Print and Echo both output data to the screen in a similar fashion- so why have two different commands for the same thing?</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP Echo Vs Print Diagram</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
     <span style="color: red;">print</span> "Hello World! &lt;br /&gt;";
     <span style="color: red;">echo</span> "Hello World! &lt;br /&gt;";
     <span style="color: green;">// The above outputs the text "Hello World!" on two separate lines.
     // Notice they are identical in output!</span>

     <span style="color: red;">print</span> ("Hello World! &lt;br /&gt;");
     <span style="color: red;">echo</span> ("Hello World! &lt;br /&gt;");
     <span style="color: green;">// The above are just the same, with parenthesis.
     // Notice both can act like functions, but note they actually aren't.</span>
<span style="color: blue;">?&gt;</span></pre>
<p>In all actuality, Echo and Print differ based on how they are structured. Print returns a value much like a normal function would. But despite common belief, Print is not a function, as we can see by the fact that it doesn&#8217;t require parenthesis to work (Not to be confused with Printf). Print and Echo are actually both called language constructs, although this isn&#8217;t to say that we can&#8217;t make Print act like a function.</p>
<h3>PHP Echo Vs Print: Which Is Faster?</h3>
<p>Developers need to ask the all-important question; &#8220;Why on Earth would I ever need to return a value from a string of data?&#8221; This is a good question, and the easy answer is you probably will never need to. The fact remains to some that returning a value degrades system performance- but is it enough to worry over?</p>
<p>By looping a large block of text multiple times, we can measure how long the two language constructs take to print out data. In an exclusive LearnPHPOnline.com test, we found that through an extended amount of iterations through a loop, Echo did indeed turn out to be the winner in speed! Whereas it took around 550ms for Print, the complete iteration took around 450ms for Echo. (For those without a calculator or a quick noggin, that&#8217;s almost 20% difference.)</p>
<p style="font-size: 10px; color: gray; margin-bottom: -7px; text-indent: 15px; margin-left: 85px;">Percent Of A Full Second (1,000ms)</p>
<p><img style="margin: 0px auto; display: block; width: 142px; height: 249px;" title="PHP Echo Vs Print" src="../php-echo-vs-print.png" alt="" /></p>
<p>Of course results depend on certain conditions, and the fact that we had to iterate the blocks of texts to an unimaginable amount of times to see a result shows that the difference really is marginal. It&#8217;s actually mentioned through a supporting page on the PHP.net homepage that developers should pick what suits them best- performance isn&#8217;t a real issue.</p>
<p>As a last note on speed, it&#8217;s recommended that developers add strings together via parameters- not through concatenation or multiple Echo calls. Instead of using new Echo commands to help organize code, separate them with commas (Make certain you aren&#8217;t using concatenation- this actually slows the process)! Calling the Echo or Print command multiple times will also degrade the performance of the script, although marginally, as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">A Special Note On Concatenation Vs Parameters</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
     <span style="color: red;">echo</span> "Hello" . "World! &lt;br /&gt;";
<span style="color: green;">// Concatenation slows down the process because PHP must add strings together</span>

     <span style="color: red;">echo</span> "Hello" , "&lt;br /&gt;";
     <span style="color: red;">echo</span> "World" , "&lt;br /&gt;";
<span style="color: green;">// Calling Echo multiple times still isn't as good as using Echo parameters solely</span>

     <span style="color: red;">echo</span> "Hello" , "World!" , "&lt;br /&gt;";
<span style="color: green;">// Correct! In a large loop, this could save a couple of seconds in the long run!</span>
<span style="color: blue;">?&gt;</span></pre>
<h3>PHP Echo Vs Print: The Conclusion</h3>
<p>So what do we use? Echo of course! But not because of speed, and certainly not because we have anything against pseudo-functions that are disguised as language constructs. So why do most PHP developers go for echo, when the benefits are very marginal?</p>
<p>Easy! It sounds cool! Not to mention the fact that the word Echo has one less letter in it that Print- and that&#8217;s saving our left pointing finger from having to press the &#8220;T&#8221; key each time we want to use the language construct in question.</p>
<p>It&#8217;s human nature to be lazy (Or have a certain appreciation for cool-sounding words), and that&#8217;s exactly the reason why you&#8217;ll see the majority of PHP developers use Echo over Print. The speed benefit is just icing on the cake.</p>
<h4>Closing Comments</h4>
<p><img style="display: block; float: right;" title="Oh, sweet nostalgia!" src="../dinosaur-tee.png" alt="" align="right" /></p>
<p>All jokes aside, it really makes no difference as to which command is used. We find that although most developers will use Echo, some of the older programmers use Print because it reminds them of their earlier programming years (Back when print was a command in early programming languages, where new language constructs weren&#8217;t stealing their glory).</p>
<p style="text-indent: 0px;"><strong>Bottom line</strong>: It&#8217;s up to your tastes, and whether or not you like pressing the &#8220;T&#8221; key for nostalgic value.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/php-basics/php-echo-vs-print/feed</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
	</channel>
</rss>
