<?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>PHP Starter &#187; Beginner</title>
	<atom:link href="http://phpstarter.net/tag/beginner/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpstarter.net</link>
	<description>PHP Tips &#38; Tools From Starters to Experts</description>
	<lastBuildDate>Fri, 25 Jun 2010 14:14:24 +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>Making Google Static Maps a Little More Dynamic with PHP</title>
		<link>http://phpstarter.net/2010/03/making-google-static-maps-a-little-more-dynamic-with-php/</link>
		<comments>http://phpstarter.net/2010/03/making-google-static-maps-a-little-more-dynamic-with-php/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 14:00:24 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=501</guid>
		<description><![CDATA[If you have messed with the Google Maps API, you know that the JavaScript and API keys can be a real headache. Yes, there are very complex implementations that you can use, but what if you want&#8230;just a map? If you want just a map with no dynamic interface, Google Static Maps is just for [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>If you have messed with the <a href="http://code.google.com/apis/maps/">Google Maps API</a>, you know that the JavaScript and API keys can be a real headache.  Yes, there are <a href="http://www.wunderground.com/wundermap/">very</a> <a href="http://www.packagemapping.com/">complex</a> <a href="http://sites.google.com/site/gmapsmania/100thingstodowithgooglemapsmashups">implementations</a> that you can use, but what if you want&#8230;just a map?  If you want just a map with no dynamic interface, Google Static Maps is just for you.  I will show how easy it is to use, and then spice it up with some PHP-powered enhancements.</p>
<p><span id="more-501"></span></p>
<p>Check out how easy this is.  If I want a map centered at a my zip code, I can create a JPEG image for that area with this link:</p>
<p><code>http://maps.google.com/maps/api/staticmap?sensor=false&#038;center=46311&#038;zoom=14&#038;size=600x400</code></p>
<p>And it generates this:</p>
<p><img src="http://maps.google.com/maps/api/staticmap?sensor=false&#038;center=46311&#038;zoom=14&#038;size=600x400" alt="" /></p>
<p>No API keys, no JavaScript, and the URL is even short enough to not have to wrap on the page.  As long as you don&#8217;t need the typical panning and zooming features, this is the best option.  There are a limited but practical set of features including map types (satellite, hybrid, regular, etc), markers, lines, shapes, custom icons, and all in different image formats.  View the <a href="http://code.google.com/apis/maps/documentation/staticmaps/">Google Static Maps API</a> page for the full details.</p>
<h3>Making it a Little More Dynamic</h3>
<p>This is a site on PHP.  I can&#8217;t show you how to create static maps and leave it at that.  There are several good reasons why we might want to dynamically generate that URL. </p>
<p><b>Simple &#8220;You Are Here&#8221; Map</b></p>
<p>For starters, how about a map that shows your local area based on your IP address geolocation?  This sample below uses the code from a previous article that showed how to <a href="http://phpstarter.net/2008/12/how-to-get-the-geographic-location-of-an-ip-address/">determine the geographic location from any IP address</a>.</p>
<p style="text-align: right; "><a href="/samples/501/geoip.php" target="_blank">Run This Example</a></p><pre name="code" class="brush: php">&lt;?php

/* replace with your own DB connection code */
require('../includes/database.php');
$db = db_connect();

/* get the IP address and make sure it is an unsigned integer */
$ip = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR']));

/* fetch the location id and info */
$query = &quot;SELECT CityLocation.* FROM CityBlocks INNER JOIN CityLocation ON CityLocation.locId = CityBlocks.locId WHERE $ip BETWEEN CityBlocks.startIpNum AND CityBlocks.endIpNum LIMIT 1&quot;;
$result = mysql_query($query, $db) or die(mysql_error());
$location = mysql_fetch_assoc($result);

?&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Static Map for Your Location&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt;Static map for your town:&lt;/h2&gt;
&lt;p&gt;If you are placed in the middle of an ocean, your IP address is probably not in the database.  The blue marker is your specific IP location (probably not exact).&lt;/p&gt;
&lt;img src=&quot;http://maps.google.com/maps/api/staticmap?sensor=false&amp;center=&lt;?=$location['postalCode']?&gt;&amp;markers=color:blue|&lt;?=$location['latitude']?&gt;,&lt;?=$location['longitude']?&gt;&amp;zoom=13&amp;size=600x400&quot; /&gt;

&lt;h2&gt;Details from IP GeoLocation:&lt;/h2&gt;
&lt;pre&gt;&lt;?php var_dump($location); ?&gt;&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p><b>Show Your Position With Other Objects</b></p>
<p>We already know how to find a list of the closest items in our area from this post: <a href="http://phpstarter.net/2009/01/locate-the-nearest-radar-station-and-display-radar-images/">Locate the Nearest Radar Station and Display Radar Images</a>.  Now, let&#8217;s apply it to generating a Static Google Map.  This uses the &#8220;Implicit Positioning&#8221; feature where you can just list a bunch of points, and the map will automatically create a zoom level and center based on the created points.</p>
<p style="text-align: right; "><a href="/samples/501/radar_sites.php" target="_blank">Run This Example</a></p><pre name="code" class="brush: php">&lt;?php

/* replace with your own DB connection code */
require('../includes/database.php');
$db = db_connect();

/* get the IP address and make sure it is an unsigned integer */
$ip = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR']));

/* fetch the location id */
$query = &quot;SELECT locId FROM CityBlocks 
			WHERE $ip BETWEEN startIpNum AND endIpNum LIMIT 1&quot;;
$result = mysql_query($query, $db) or die(mysql_error());
$row = mysql_fetch_assoc($result);

/* now fetch the location */
$locId = $row['locId'];
$query = &quot;SELECT * FROM CityLocation WHERE locId = $locId LIMIT 1&quot;;
$result = mysql_query($query, $db) or die(mysql_error());
$location = mysql_fetch_assoc($result);

/* offset the coordinates by 3, and find the closest station */
$lat = $location['latitude'] + 3;
$lon = $location['longitude'] - 3;
$query = &quot;SELECT *, SQRT(POW(69.1 * (lat - $lat), 2) + 
			POW(69.1 * ($lon - lon) * COS(lat / 57.3 ), 2 )) AS distance 
			FROM RadarSites ORDER BY distance ASC LIMIT 5&quot;;
$result = mysql_query($query, $db) or die(mysql_error());

$qs = '';
for ($i = 0; $i &lt; mysql_num_rows($result); $i++)
{
	$row = mysql_fetch_assoc($result);
	$rows[] = $row;
	
	/* remember - we want roughly the center of the radar coverage, not the top-left corner */
	$lat = $row['lat'] - 3;
	$lon = $row['lon'] + 3;
	
	/* forming the query string for the image URL */
	$markers[] = '&amp;markers=color:blue|label:' . $i . '|' . $lat . ',' . $lon;
}

$markers = implode('', $markers);

?&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Static Map for Your Location&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt;5 Closest Radar Sites to You&lt;/h2&gt;
&lt;p&gt;Red marker is you.&lt;/p&gt;
&lt;img src=&quot;http://maps.google.com/maps/api/staticmap?sensor=false&amp;maptype=roadmap&lt;?=$markers?&gt;&amp;markers=color:red|&lt;?=$location['latitude']?&gt;,&lt;?=$location['longitude']?&gt;&amp;size=600x400&quot; /&gt;

&lt;h2&gt;Details from IP GeoLocation:&lt;/h2&gt;
&lt;pre&gt;&lt;?php var_dump($location); ?&gt;&lt;/pre&gt;

&lt;h2&gt;Radar Sites:&lt;/h2&gt;
&lt;pre&gt;&lt;?php var_dump($rows); ?&gt;&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>These examples are meant to get your creative juices flowing.  Have some other cool concepts for Google Static Maps?  Post them below.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2010/03/making-google-static-maps-a-little-more-dynamic-with-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Separating the Application &amp; Presentation and Alternative Syntax</title>
		<link>http://phpstarter.net/2009/03/separating-the-application-presentation-and-alternative-syntax/</link>
		<comments>http://phpstarter.net/2009/03/separating-the-application-presentation-and-alternative-syntax/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 16:31:47 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=414</guid>
		<description><![CDATA[One of the biggest pet peeves I have when looking at (and unfortunately &#8211; working with) other people&#8217;s PHP code is the way they integrate their code into the HTML. Many people don&#8217;t take the time to organize, and they end up with a PHP &#038; HTML mess. I believe that it is critical to [...]


Related posts:<ul><li><a href='http://phpstarter.net/2010/03/making-google-static-maps-a-little-more-dynamic-with-php/' rel='bookmark' title='Permanent Link: Making Google Static Maps a Little More Dynamic with PHP'>Making Google Static Maps a Little More Dynamic with PHP</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p>One of the biggest pet peeves I have when looking at (and unfortunately &#8211; working with) other people&#8217;s PHP code is the way they integrate their code into the HTML.  Many people don&#8217;t take the time to organize, and they end up with a PHP &#038; HTML mess.  I believe that it is critical to separate the application logic from the presentation.  In other words &#8211; do the bulk of the application operations in a script file that has <strong>no text/HTML output</strong>.  When that processing is done, send the data to the presentation file that takes the data and outputs that along with the static HTML for that page.  In this article, I will present an easy way to do it using no template engines.</p>
<p><span id="more-414"></span></p>
<p>I have some perfect textbook examples that I would love to post of very badly-formatted code, but because of privacy and confidentially reasons with my clients, I can&#8217;t do that.  You will just have to take my word for it &#8211; I have seen some major hackjobs thrown together by the most obvious of n00bs I didn&#8217;t even know existed.</p>
<p>So the main thing to remember is this: Think of your web application as having two layers &#8211; <strong>application</strong> &#038; <strong>presentation</strong>.  The only job for the presentation layer is to format the data into the HTML and send it to the browser.  Everything else falls to the application layer.</p>
<h3>Bad Example</h3>
<p>I decided to write a script that would utilize a database table in my sample database.  This example uses data from the article on how to <a href="http://phpstarter.net/2009/01/locate-the-nearest-radar-station-and-display-radar-images/">Locate the Nearest Radar Station and Display Radar Images</a>.  This script will take your IP&#8217;s location, and show the 10 closest radar stations to that location &#8211; but displaying the results in way that should make you cringe.</p>
<p style="text-align: right; "><a href="/samples/414/fail.php" target="_blank">Run This Example</a></p><pre name="code" class="brush: php:collapse">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;IP Geolocation Example&lt;/title&gt;
&lt;meta name=&quot;generator&quot; content=&quot;Bluefish 1.0.7&quot;&gt;
&lt;meta name=&quot;author&quot; content=&quot;Andrew Wells&quot;&gt;
&lt;meta name=&quot;date&quot; content=&quot;2008-12-26T17:05:21-0600&quot;&gt;
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=UTF-8&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
/* replace with your own DB connection code */
require('../includes/database.php');
$db = db_connect();

/* get the IP address and make sure it is an unsigned integer */
$ip = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR']));

/* fetch the location id */
$query = &quot;SELECT locId FROM CityBlocks WHERE $ip BETWEEN startIpNum AND endIpNum LIMIT 1&quot;;
$result = mysql_query($query, $db) or die(mysql_error());
$row = mysql_fetch_assoc($result);

/* now fetch the location */
$locId = $row['locId'];
$query = &quot;SELECT * FROM CityLocation WHERE locId = $locId LIMIT 1&quot;;
$result = mysql_query($query, $db) or die(mysql_error());
$location = mysql_fetch_assoc($result);

echo '&lt;h2&gt;Details on IP Location:&lt;/h2&gt;';
echo '&lt;b&gt;Location ID:&lt;/b&gt; ' . $location['locId'] . &quot;&lt;br /&gt;\n&quot;;
echo '&lt;b&gt;Country:&lt;/b&gt; ' . $location['country'] . &quot;&lt;br /&gt;\n&quot;;
echo '&lt;b&gt;Region:&lt;/b&gt; ' . $location['region'] . &quot;&lt;br /&gt;\n&quot;;
echo '&lt;b&gt;City:&lt;/b&gt; ' . $location['city'] . &quot;&lt;br /&gt;\n&quot;;
echo '&lt;b&gt;Postal Code:&lt;/b&gt; ' . $location['postalCode'] . &quot;&lt;br /&gt;\n&quot;;
echo '&lt;b&gt;Latitude:&lt;/b&gt; ' . $location['latitude'] . &quot;&lt;br /&gt;\n&quot;;
echo '&lt;b&gt;Longitude:&lt;/b&gt; ' . $location['longitude'] . &quot;&lt;br /&gt;\n&quot;;
echo '&lt;b&gt;Metro Code:&lt;/b&gt; ' . $location['metroCode'] . &quot;&lt;br /&gt;\n&quot;;
echo '&lt;b&gt;Area Code:&lt;/b&gt; ' . $location['areaCode'] . &quot;&lt;br /&gt;\n&quot;;

/* offset the coordinates by 3, and find the closest station */
$lat = $location['latitude'] + 3;
$lon = $location['longitude'] - 3;
$query = &quot;SELECT *, SQRT(POW(69.1 * (lat - $lat), 2) + 
			POW(69.1 * ($lon - lon) * COS(lat / 57.3 ), 2 )) AS distance 
			FROM RadarSites ORDER BY distance ASC LIMIT 10&quot;;
$result = mysql_query($query, $db) or die(mysql_error());

echo'&lt;h2&gt;Radar Site Details&lt;/h2&gt;&lt;table width=&quot;800&quot;&gt;';
echo '&lt;tr&gt;';
echo '&lt;th&gt;Station ID&lt;/th&gt;';
echo '&lt;th&gt;Lat&lt;/th&gt;';
echo '&lt;th&gt;Lon&lt;/th&gt;';
echo '&lt;th&gt;Updated&lt;/th&gt;';
echo '&lt;th&gt;Distance&lt;/th&gt;';
echo '&lt;/tr&gt;';
for ($i = 0, $n = mysql_num_rows($result); $i &lt; $n; $i++)
{
	$radar = mysql_fetch_assoc($result);
	echo '&lt;tr&gt;';
	echo '&lt;td&gt;' . $radar['id'] . '&lt;/td&gt;';
	echo '&lt;td&gt;' . $radar['lat'] . '&lt;/td&gt;';
	echo '&lt;td&gt;' . $radar['lon'] . '&lt;/td&gt;';
	echo '&lt;td&gt;' . $radar['updated'] . '&lt;/td&gt;';
	echo '&lt;td&gt;' . $radar['distance'] . '&lt;/td&gt;';
	echo '&lt;/tr&gt;';
}
echo '&lt;/table&gt;';
?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>This isn&#8217;t a worst cast scenario &#8211; I&#8217;ve seen worse, but I would like to point out the PHP code block in the middle of the header and footer HTML.  Notice all the application work I&#8217;m doing in lines 13-67 that is right in the middle of the HTML output.  Looking further, you can see that I am using echo statements to output the table HTML and other code.  This is <strong>bad</strong>.  It makes it hard to change the HTML without messing with the PHP, and it makes it next to impossible for a designer to work with it.</p>
<h3>Good Example</h3>
<p>Here is a better example with most of the logic separated from the HTML.</p>
<p style="text-align: right; "><a href="/samples/414/win.php" target="_blank">Run This Example</a></p><pre name="code" class="brush: php:collapse">&lt;?php
/* replace with your own DB connection code */
require('../includes/database.php');
$db = db_connect();

/* get the IP address and make sure it is an unsigned integer */
$ip = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR']));

/* fetch the location id */
$query = &quot;SELECT locId FROM CityBlocks WHERE $ip BETWEEN startIpNum AND endIpNum LIMIT 1&quot;;
$result = mysql_query($query, $db) or die(mysql_error());
$row = mysql_fetch_assoc($result);

/* now fetch the location */
$locId = $row['locId'];
$query = &quot;SELECT * FROM CityLocation WHERE locId = $locId LIMIT 1&quot;;
$result = mysql_query($query, $db) or die(mysql_error());
$location = mysql_fetch_assoc($result);

/* offset the coordinates by 3, and find the closest station */
$lat = $location['latitude'] + 3;
$lon = $location['longitude'] - 3;
$query = &quot;SELECT *, SQRT(POW(69.1 * (lat - $lat), 2) + 
			POW(69.1 * ($lon - lon) * COS(lat / 57.3 ), 2 )) AS distance 
			FROM RadarSites ORDER BY distance ASC LIMIT 10&quot;;
$result = mysql_query($query, $db) or die(mysql_error());

for ($i = 0, $n = mysql_num_rows($result); $i &lt; $n; $i++)
{
	$radars[] = mysql_fetch_assoc($result);
}
?&gt;
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;IP Geolocation Example&lt;/title&gt;
&lt;meta name=&quot;generator&quot; content=&quot;Bluefish 1.0.7&quot;&gt;
&lt;meta name=&quot;author&quot; content=&quot;Andrew Wells&quot;&gt;
&lt;meta name=&quot;date&quot; content=&quot;2008-12-26T17:05:21-0600&quot;&gt;
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=UTF-8&quot;&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h2&gt;Details on IP Location:&lt;/h2&gt;
&lt;b&gt;Location ID:&lt;/b&gt; &lt;?=$location['locId']?&gt;&lt;br /&gt;
&lt;b&gt;Country:&lt;/b&gt; &lt;?=$location['country']?&gt;&lt;br /&gt;
&lt;b&gt;Region:&lt;/b&gt; &lt;?=$location['region']?&gt;&lt;br /&gt;
&lt;b&gt;City:&lt;/b&gt; &lt;?=$location['city']?&gt;&lt;br /&gt;
&lt;b&gt;Postal Code:&lt;/b&gt; &lt;?=$location['postalCode']?&gt;&lt;br /&gt;
&lt;b&gt;Latitude:&lt;/b&gt; &lt;?=$location['latitude']?&gt;&lt;br /&gt;
&lt;b&gt;Longitude:&lt;/b&gt; &lt;?=$location['longitude']?&gt;&lt;br /&gt;
&lt;b&gt;Metro Code:&lt;/b&gt; &lt;?=$location['metroCode']?&gt;&lt;br /&gt;
&lt;b&gt;Area Code:&lt;/b&gt; &lt;?=$location['areaCode']?&gt;&lt;br /&gt;

&lt;h2&gt;Radar Site Details&lt;/h2&gt;&lt;table width=&quot;800&quot;&gt;
&lt;tr&gt;
	&lt;th&gt;Station ID&lt;/th&gt;
	&lt;th&gt;Lat&lt;/th&gt;
	&lt;th&gt;Lon&lt;/th&gt;
	&lt;th&gt;Updated&lt;/th&gt;
	&lt;th&gt;Distance&lt;/th&gt;
&lt;/tr&gt;
&lt;?php foreach ($radars as $radar): ?&gt;
&lt;tr&gt;
	&lt;td&gt;&lt;?=$radar['id']?&gt;&lt;/td&gt;
	&lt;td&gt;&lt;?=$radar['lat']?&gt;&lt;/td&gt;
	&lt;td&gt;&lt;?=$radar['lon']?&gt;&lt;/td&gt;
	&lt;td&gt;&lt;?=$radar['updated']?&gt;&lt;/td&gt;
	&lt;td&gt;&lt;?=$radar['distance']?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php endforeach; ?&gt;

&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>In lines 46-54, I am using PHP shorthand tags to output PHP variables in a cleaner way.  The other thing you will notice is that there is no getting around the loop structure that is needed to output the 10 radar sites.  We can do it without the curly braces though, and with neater &#8220;tags&#8221; in the HTML to control the loop.</p>
<p>Another thing to mention &#8211; I would even have the PHP block in a different file, but I combined them for the simplicity of the example.</p>
<h3>PHP Shorthand Tags and Alternative Syntax</h3>
<p>Since the presentation files are to be more focused on the HTML, and won&#8217;t be very PHP-intense, we can use simplified tags.  To output PHP variables in HTML without the PHP opening/closing tags, we use the shorthand method:</p>
<pre class="brush: php">
&lt;!-- old method --&gt;
&lt;html&gt;
&lt;?php
    echo $some_var;
?&gt;
&lt;/html&gt;

&lt;!-- better method --&gt;
&lt;html&gt;
&lt;?=$some_var?&gt;
&lt;/html&gt;
</pre>
<p>If we need to have a control structure, such as an if statement or loop, we do it this way:</p>
<pre class="brush: php">
&lt;html&gt;
&lt;?php if ($condition): ?&gt;
&lt;p&gt;Some content here&lt;/p&gt;
&lt;?php endif; ?&gt;
&lt;/html&gt;
</pre>
<p>Here is a loop example:</p>
<pre class="brush: php">
&lt;html&gt;
&lt;?php for ($i = 0; $i &lt; 10; $i++): ?&gt;
&lt;p&gt;Some content here with the current index of &lt;?=$i?&gt;&lt;/p&gt;
&lt;?php endfor; ?&gt;
&lt;/html&gt;
</pre>
<p>So there you have it &#8211; a few tips for you to create more readable code.  I have read that doing it this way with several PHP opening/closing tags can hurt performance by a hair, but I prefer losing a few CPU cycles over several hours debugging later down the road.  The choice is yours. <img src='http://phpstarter.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>


<p>Related posts:<ul><li><a href='http://phpstarter.net/2010/03/making-google-static-maps-a-little-more-dynamic-with-php/' rel='bookmark' title='Permanent Link: Making Google Static Maps a Little More Dynamic with PHP'>Making Google Static Maps a Little More Dynamic with PHP</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/03/separating-the-application-presentation-and-alternative-syntax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Basic PHP/MySQL Actions</title>
		<link>http://phpstarter.net/2009/02/basic-phpmysql-actions/</link>
		<comments>http://phpstarter.net/2009/02/basic-phpmysql-actions/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 06:00:22 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=357</guid>
		<description><![CDATA[Using databases to store user identification, image information, and so much more for dynamic websites is the easiest way to ease maintenance. MySQL is the most popular choice of database, and in this article, I'll explain the most basic functions: connecting and using a MySQL database and its respective tables.


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Using databases to store user identification, image information, and so much more for dynamic websites is the easiest way to ease maintenance.  MySQL is the most popular choice of database, and in this article, I&#8217;ll explain the most basic functions: connecting and using a MySQL database and its respective tables.<span id="more-357"></span>The first step in using a MySQL database is to make sure your host has the necessities set up.  Typically, your web host&#8217;s control panel will allow the creation of a database/user pair which will allow you to connect.  However, most do not allow the creation of these directly through a PHP script.  Check with your host to see how you can get your MySQL database running.</p>
<p>The next step, and probably the first in your running script is connecting to the user and database, using the following two functions:</p>
<pre class="brush: php">

&lt;?php

...

mysql_connect(host, user, password);

mysql_select_db(database [, link]);

...

?&gt;
</pre>
<p>Again, your host will work with you with respect to establishing the user, password, and database, and <em>host</em> is typically &#8220;localhost&#8221; when accessing the database on the same server as your website, but check with your host if you are having problems.  The link, which is optional, would refer to the variable holding your <strong>mysql_connect()</strong> reference.</p>
<p>Now you have a database ready to go, but without a table, there&#8217;s no place for your streams of data to go, so let&#8217;s set one up.  Most of our queries to MySQL are going to be run through the appropriately named <strong>mysql_query()</strong> function, and the text within will change with each different application.  To create a table, we run a query like this:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;CREATE TABLE table_name(

column_one ATTRIBUTES,

column_two ATTRIBUTES,

...

)&quot;);

mysql_close(link);

?&gt;
</pre>
<p>You should take the time before creating a table to decide what all needs to be stored and what would be the best manner to do so, and then use one of the column types and attributes name <a href="http://dev.mysql.com/doc/refman/5.1/en/create-table.html" target="_blank">at this resource</a>.  Some may seem confusing, but there are plenty of elaborations and tutorials available to describe the uses of each type.  The <strong>mysql_close()</strong> function simply closes our the defined connection and is typically unnecessary as the connection will be closed at the end of the script anyway.</p>
<p>With a table available for queries, we can begin adding and working with our data.  The main abilities available with the <strong>mysql_query()</strong> function our <strong>SELECT</strong>, <strong>SHOW</strong>, <strong>DESCRIBE</strong>, <strong>EXPLAIN</strong>, <strong>INSERT</strong>, <strong>UPDATE</strong>, <strong>DELETE</strong>, and <strong>DROP</strong>.  The first four return a resource (explained in detail in a minute) and the latter four return a boolean value depending on the success.</p>
<p><strong>SHOW</strong>, <strong>DESCRIBE</strong>, and <strong>EXPLAIN</strong> are honestly rarely used within the context of a functioning, user-run website.  They are moreso used on the administrator&#8217;s end and even then are not as functional as the others.  <strong>DELETE</strong> and <strong>DROP</strong> should certainly only be used on the administrator&#8217;s end and probably, again, only through MySQL itself and not through a publicly-available executable file.  Only through proper authentication should someone be able to delete a record or even an entire MySQL table.</p>
<p>Let&#8217;s start with <strong>INSERT</strong>, as that is where you must start to use the other functions.  To insert data into a table, run the function like this:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;INSERT INTO `table_name` (col1, col2, ...)

VALUES(&#039;val1&#039;, &#039;val2&#039;, ...)&quot;) or die(mysql_error());

...

?&gt;
</pre>
<p>The column names in the first set of parentheses should correspond with the correct value in the second set.  You may also opt, with each execution of <strong>mysql_query()</strong> to use the <strong>mysql_error()</strong> function to report an error should one occur, though hopefully you&#8217;ve worked your script well and an error is handled by your own error-checking.  However, some things do slip by and happen unpreventably.</p>
<p>Next is the <strong>SELECT</strong> application.  Just as it sounds, you select records from an identified table and can then use the data however you wish, by displaying it, checking it against variables, etc.  Either way, a <strong>SELECT</strong> application looks like this:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

$getRows = mysql_query(&quot;SELECT * FROM `table_name` WHERE `col_name`=&#039;val&#039;&quot;);

while ($result = mysql_fetch_array($getRows)) {

echo $result[&#039;col_name&#039;];

...

}

...

?&gt;
</pre>
<p>The <strong>*</strong> can be replaced with each column you wish to extract, separated by commas, and the WHERE clause is in the query is optional but should be used when looking for particular records.  You should note that <strong>SELECT</strong> returns a resource, so we have to loop through that resource using the <strong>mysql_fetch_array()</strong>, <strong>mysql_fetch_assoc()</strong>, <strong>mysql_fetch_row()</strong> or an appropriate alternative.  Then the data from the records is stored in an array where each column is referenced by its respective name.</p>
<p>To delete a record (or records) use a similar construct to the <strong>SELECT</strong> application, but no looping or variable-calling is necessary.  It simply works through the query:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;DELETE FROM `table_name` WHERE `col_name`=&#039;val&#039;&quot;);

...

?&gt;
</pre>
<p>To delete an entire table, use the <strong>DROP</strong> application, though realize that every record will be obliterated:</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;DROP TABLE `table_name`&quot;);

...

?&gt;
</pre>
<p>Lastly, editing records is fairly easy though it can be more cumbersome.  Using the <strong>UPDATE</strong> application like so can allow for the altering of preexisting rows.</p>
<pre class="brush: php">

&lt;?php

//Connect to MySQL before running any queries, every time

mysql_query(&quot;UPDATE `table_name`

SET `col_name` = &#039;val&#039;, `col2_name` = &#039;val2&#039;, ...

WHERE `col_name` = &#039;val&#039;&quot;);

...
?&gt;
</pre>
<p>You can change as many columns as you need, separating each with a comma, but be careful with spacing because the success of the update can depend highly on the spacing as I have seen in the past, though I can&#8217;t find a good reference on what the exact spacing should look like.</p>
<p>With these basic applications in tow, little cannot be accomplished with your MySQL database.  With a dynamic, user-driven website these will certainly be integrated into your website.  To keep a more watchful eye on your database and perform certain one-time actions on your tables, you may also choose to employ a GUI like <a href="http://www.phpmyadmin.net" target="_blank">PHPMyAdmin</a> or something similar which your host may already have available through the control panel.</p>
<p>Happy querying!</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/02/basic-phpmysql-actions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Explosive PHP Application</title>
		<link>http://phpstarter.net/2009/02/explosive-php-application/</link>
		<comments>http://phpstarter.net/2009/02/explosive-php-application/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 06:00:18 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=341</guid>
		<description><![CDATA[Sometimes a complex PHP-built website can become tedious when dealing with minor changes to the template and HTML markup.  Changing, for instance, a set of links (navigation) or other integral parts of a website, can become quite the lengthy process.  In this article, I'll share a technique that I sometimes use to make something as simple as a website navigation a little more automated using an explosive function.


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Sometimes a complex PHP-built website can become tedious when dealing with minor changes to the template and HTML markup.  Changing, for instance, a set of links (navigation) or other integral parts of a website, can become quite the lengthy process.  In this article, I&#8217;ll share a technique that I sometimes use to make something as simple as a website navigation a little more automated using an explosive function.<span id="more-341"></span>So maybe you&#8217;ve caught on to the references to explosions and are thinking that something&#8217;s about to get dangerous, and really, this application is quite safe and easy to use.  The function itself is properly named &#8220;explode&#8221;:</p>
<p><span>
<pre class="brush: php">

&lt;?php

...

$arr = explode(delimiter, string [,limit]);

...

?&gt;
</pre>
<p>Now you may be thinking: that&#8217;s all well in good but what in the world is a delimiter?  I thought the same way before I truly understood the function.  The delimiter is just the common separator between each element.  For instance, if we wanted to explode a sentence into an array with each element as a separate word from the string, the delimiter would be an empty space (&#8221; &#8220;).  The optional &#8220;limit&#8221; is a limit of the number of elements to allow to be created.  If it is left empty or set to &#8220;-1,&#8221; no limit will be set.  Otherwise, once the limit is reached, the last element will be the rest of the unexploded string.</p>
<p>Now this is all good and well unless you have an awesome application of the function, and believe me, there are many, but my favorite is the dynamic navigation idea.  I always get sick of having to add in all the extra template elements when altering a website&#8217;s navigation, and with this simple idea that can cease.</p>
<p>Here&#8217;s a basic run-down of what will happen:  first, we&#8217;ll define a string holding all the link names and source locations.  Then, we&#8217;ll explode that string to an array of all link name-source pairs which we will in turn explode individually to produce a great link.  Here&#8217;s an example:</p>
<pre class="brush: php">

&lt;?php

...

$links = &quot;Home:index.php,About Us:about.php,Contact Us:contact.php,Portfolio:portfolio.php&quot;;

$links = explode(&quot;,&quot;,$links);

echo &quot;&lt;ul&gt;&quot;;

foreach ($links as $link) {

$link = explode(&quot;:&quot;,$link);

echo &quot;&lt;li&gt;&lt;a href=&#039;&quot;.$link[1].&quot;&#039;&gt;&quot;.$link[0].&quot;&lt;/a&gt;&lt;/li&gt;&quot;;

}

echo &quot;&lt;/ul&gt;&quot;;

?&gt;
</pre>
<p>Be aware of the order in which we are exploding the elements.  If you explode incorrectly, you could end up with inaccurate pairs.  With this application, to update the navigation, instead of sifting through what could become complex HTML, you simply need to edit the &#8220;links&#8221; variable and let the PHP take care of business for you.  Nothing complex, but certainly useful.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/02/explosive-php-application/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Understanding Decision Structures</title>
		<link>http://phpstarter.net/2009/01/understanding-decision-structures/</link>
		<comments>http://phpstarter.net/2009/01/understanding-decision-structures/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 06:00:53 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=61</guid>
		<description><![CDATA[Decision structures form the basis of all programming languages; just as they sound, these constructs move the program down a certain path depending on variables which are checked.  This gives a program functionality and the ability to handle different input and analyze the data within the script. Though the term sounds complex, decision structures just [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Decision structures form the basis of all programming languages; just as they sound, these constructs move the program down a certain path depending on variables which are checked.  This gives a program functionality and the ability to handle different input and analyze the data within the script.<br />
<span id="more-61"></span>Though the term sounds complex, decision structures just help the script make a decision.  If something is true/false, do this.  If not, do this.  All decision structures come down to a true or false situation, and usually a comparison is made that results in one of the two choices.  There are several comparison operators, and it takes a basic understanding of logic to fully utilize decision structures.</p>
<p>The comparision operators are:</p>
<table border="0">
<tbody>
<tr>
<td>==</td>
<td>($a == $b)</td>
<td>Returns true if $a equals $b</td>
</tr>
<tr>
<td>===</td>
<td>($a === $b)</td>
<td>Returns true if $a equals $b and is of the same type (int, string, etc)</td>
</tr>
<tr>
<td>!=</td>
<td>($a != $b)</td>
<td>Returns true if $a does not equal $b</td>
</tr>
<tr>
<td>!==</td>
<td>($a !== $b)</td>
<td>Returns true if $a does not equal $b or is not of the same type</td>
</tr>
<tr>
<td>&lt;</td>
<td>($a &lt; $b)</td>
<td>Returns true if $a is less than $b</td>
</tr>
<tr>
<td>&gt;</td>
<td>($a &gt; $b)</td>
<td>Returns true if $a is greater than $b</td>
</tr>
<tr>
<td>&lt;=</td>
<td>($a &lt;= $b)</td>
<td>Returns true if $a is less than or equal to $b</td>
</tr>
<tr>
<td>&gt;=</td>
<td>($a &gt;= $b)</td>
<td>Returns true if $a is greater than or equal to $b</td>
</tr>
</tbody>
</table>
<p>Logical operators are a bit different but are also used in decision structures.  There are five different logical operators, but for the sake of this article, in truth three are the most important.</p>
<table border="0">
<tbody>
<tr>
<td>&amp;&amp; , and</td>
<td>($a &amp;&amp; $b)</td>
<td>Returns true if A and B are both be true</td>
</tr>
<tr>
<td>|| , or</td>
<td>($a || $b)</td>
<td>Returns true if A or B (or both) is (are) true</td>
</tr>
<tr>
<td>xor</td>
<td>($a xor $b)</td>
<td>Returns true if A or B is true and the other is false</td>
</tr>
</tbody>
</table>
<p>For more information on the reason behind two variations of &#8220;and&#8221; and &#8220;or&#8221; operators, check out the manual on <a href="http://us.php.net/manual/en/language.operators.precedence.php">operator precedence</a>.</p>
<p>Now let&#8217;s get on to the decision-making part of programming with PHP.  There are two main ways to do this, and we will start with the if-else statements.</p>
<p>Basically, this comprises the main idea of decision making.  It handles like this:  &#8220;If THIS is true, do something.  Otherwise (else), do something else.&#8221;  Using the operators defined above, you can make a basic script:</p>
<pre class="brush: php">
if ($a == $b) {
echo &quot;The variables are equal.&quot;;
}
else {
echo &quot;The variables are unequal.&quot;;
}
?&gt;
</pre>
<p>This would be great if there were always just two choices, but sometimes there are several choices between these two.  Think about a school grading scale.  The following would assign a grade based on a ten-point scale.  Since there are more than two possibilities, we will add some elseif statements inbetween.</p>
<pre class="brush: php">

&lt;?php

$grade = 93;

if ($grade &gt;= 90) {
$letter-grade = &quot;A&quot;;
}
elseif ($grade &gt; 80) {
$letter-grade = &quot;B&quot;;
}
elseif ($grade &gt; 70) {
$letter-grade = &quot;C&quot;;
}
elseif ($grade &gt; 60) {
$letter-grade = &quot;D&quot;;
}
else {
$letter-grade = &quot;F&quot;;
}
?&gt;
</pre>
<p>Notice that we work downward to make sure the grade only fits in one category.  It&#8217;s important to think about your script logically before setting out to write it.  Now, the type of structure described above will handle any situation, but in truth it isn&#8217;t always the best choice.  Suppose you wanted to create a script that would change a certain heading based on the date, if it is a holiday or not.  While an if-else/elseif structure would work, there is an alternative, the &#8220;switch&#8221; construct which focuses on just one variable rather than comparison between two or more.</p>
<pre class="brush: php">

&lt;?php
$date = ...; //Obtain date in the form &quot;mm/dd&quot;

switch($date) {
case &quot;01/01&quot;:
echo &quot;Happy New Year&quot;;
break;
case &quot;02/14&quot;:
echo &quot;Happy Valentine&#039;s Day&quot;;
break;
case &quot;05/10&quot;:
echo &quot;Happy Mother&#039;s Day&quot;;
break;
default:
echo &quot;Happy Day!&quot;;
break;
}
?&gt;
</pre>
<p>Obviously, hundreds of other holidays can be added in as a different &#8220;case,&#8221; but the idea is captured with this short script.  The &#8220;default&#8221; case acts as an &#8220;else&#8221; in this structure, catching anything that did not match any case.</p>
<p>With these two ideas, it is easy to build fairly impressive scripts with PHP, and you will even find that the idea carries over to most different programming languages with tiny changes in syntax  and operators.  Combined with more complex variables handled by user input and organized sessions, you will see that decision structures are truly the core of a programming language.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/01/understanding-decision-structures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Encrypting Password &amp; Other Sensitive Information</title>
		<link>http://phpstarter.net/2009/01/encrypting-password-other-sensitive-information/</link>
		<comments>http://phpstarter.net/2009/01/encrypting-password-other-sensitive-information/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 12:00:00 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=59</guid>
		<description><![CDATA[Encrypting sensitive data is extremely important. You, as the webmaster or database administrator, needs to make sure that the data is safe, even if it falls in the wrong hands. In this article, I will show you how to use MD5 and SHA1 encryption in PHP. Salting your Passwords Examples in this article will be [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Encrypting sensitive data is extremely important.  You, as the webmaster or database administrator, needs to make sure that the data is safe, even if it falls in the wrong hands.  In this article, I will show you how to use MD5 and SHA1 encryption in PHP.</p>
<p><span id="more-59"></span></p>
<h3>Salting your Passwords</h3>
<p>Examples in this article will be with salted passwords.  Salting the password lessons the risk of dictionary attacks.  For example, if I were to use a password of &#8216;password&#8217;.  That hash is common and very susceptible to a dictionary attack.  However, if the application salted all passwords with a random string like &#8216;as87f56safg8&#8242;, then the resulting stored password would be the encrypted version of &#8216;as87f56safg8password&#8217;, which is not susceptible to a dictionary attack, regardless of how weak of a password provided by the end-user.</p>
<h3>MD5 Encryption</h3>
<p>Using PHP&#8217;s <a href="http://us.php.net/manual/en/function.md5.php">md5()</a> function allows you to easily create a password hash.</p>
<p>Here is the recommended way to encrypt a password:</p>
<pre class="brush: php">
&lt;?php
	$salt = &quot;7dA+U^@&#039;aF7FLvJ&quot;;
	$password = &#039;secret&#039;;

	$hash = md5($salt . $password);
	echo $hash;

	/* returns: ade1373f24d328d9229d57f284871cd0 */
?&gt;
</pre>
<p>Since this is a one-way hash, you can&#8217;t decode it to compare to verify with a user-supplied password.  Instead, take the user-supplied password, encrypt it with the same md5() function &#038; salt, and compare it to the hash stored in your database.  If the hashes match, then the supplied password is correct.</p>
<h3>SHA1 Encryption</h3>
<p>This is very similar to md5(), except using another algorithm.</p>
<pre class="brush: php">
&lt;?php
	$salt = &quot;7dA+U^@&#039;aF7FLvJ&quot;;
	$password = &#039;secret&#039;;

	$hash = sha1($salt . $password);
	echo $hash;

	/* returns: be58ee5f202d6a9bfcd13ad4ac3552b1367e30db */
?&gt;
</pre>
<p>There is an alternative PHP function, <a href="http://us.php.net/manual/en/function.crypt.php">crypt()</a>, but I find these other two functions easier to use. </p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/01/encrypting-password-other-sensitive-information/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling File Uploads</title>
		<link>http://phpstarter.net/2009/01/handling-file-uploads/</link>
		<comments>http://phpstarter.net/2009/01/handling-file-uploads/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 06:00:43 +0000</pubDate>
		<dc:creator>Kurtis</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=221</guid>
		<description><![CDATA[There are very few websites in existence that do not allow, for whatever function, uploading files via the browser. Whether for forum avatars, a gallery script, or even a school project, websites across the World Wide Web employ a myriad of upload scripts, including the ever-popular MySpace, Facebook, and YouTube. With this script and step-by-step [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>There are very few websites in existence that do not allow, for whatever function, uploading files via the browser.  Whether for forum avatars, a gallery script, or even a school project, websites across the World Wide Web employ a myriad of upload scripts, including the ever-popular MySpace, Facebook, and YouTube.  With this script and step-by-step explanation, your website can now allow users to add their own files.</p>
<p><span id="more-221"></span></p>
<p>An upload script can seem like a daunting task, a process of several steps and error-handling, and in truth, there is a lot to take into account, but the uploading step itself is very simple.  Before we even begin with the form, I suggest (for those not too afraid) checking some settings in your <em>php.ini</em> file, typically located in your PHP root folder.  This step isn&#8217;t necessary, so feel free to move on now.  Find the following lines and check the settings as explained below:</p>
<ul>
<li><strong>file_uploads</strong>: Be sure this is set to <em>On</em> or your uploads will fail every time</li>
<li><strong>upload_max_filesize</strong>: Must be at least the maximum file size you wish to allow for uploads</li>
<li><strong>post_max_size</strong>: Represents the total amount of data allowed to be passed from a form, including file uploads, so I recommend setting this higher than the <strong>upload_max_filesize</strong></li>
</ul>
<p>For most typical PHP setups, the settings should work as they are, but for more advanced users I always recommend learning exactly what that <em>php.ini </em>file does with respect to certain PHP applications.  With those settings taken care of, let&#8217;s move on to setting up the basic HTML form.  For the purpose of this example, I&#8217;ll use the most basic markup possible.</p>
<pre class="brush: html">
&lt;html&gt;
&lt;body&gt;
&lt;form method=&quot;post&quot; action=&quot;upload.php&quot; enctype=&quot;multipart/form-data&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;500000&quot; /&gt;
&lt;input type=&quot;file&quot; name=&quot;file&quot; /&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Three things to keep in mind are the following specifications:</p>
<ul>
<li><strong>enctype=&#8221;multipart/form-data&#8221;</strong>: This attribute must be set as shown or the form will not process properly.  This is different from most basic PHP forms.</li>
<li><strong>&lt;input type=&#8221;hidden&#8221;&#8230;</strong>: Specifies the maximum file size, in bytes, allowed to be uploaded.  You can omit this line if you specify a maximum in your <em>php.ini</em> file (as described earlier).</li>
<li><strong>&lt;input type=&#8221;file&#8221;&#8230;</strong>: Notice we are using the &#8220;file&#8221; type here, again, mandatory for file uploads.  This line will provide the input field with the &#8220;Browse&#8221; button.</li>
</ul>
<p>Certainly, the HTML form is the easiest part, so let&#8217;s move on to actually handling the file and moving it to its final destination through the PHP processing file.  Above we designated that as &#8220;upload.php.&#8221;</p>
<p>First of all, we&#8217;ll set two variables, arrays actually, which will be very handy a little later on in the script: the possible PHP error messages and the allowed file extensions.</p>
<pre class="brush: php">

&lt;?php

$errors = array(1 =&gt; &quot;The file exceeds the maximum file size specification.&quot;,
2 =&gt; &quot;The file exceeds the maximum file size specification.&quot;,
3 =&gt; &quot;The file was not fully uploaded.&quot;,
4 =&gt; &quot;The file was not properly uploaded.&quot;,
6 =&gt; &quot;An error occurred during uploading.&quot;,
7 =&gt; &quot;An error occurred during uploading.&quot;,
8 =&gt; &quot;An error occurred during uploading.&quot;);

$allowedExtensions = array(&quot;jpeg&quot;,&quot;jpg&quot;,&quot;gif&quot;,&quot;png&quot;);

?&gt;
</pre>
<p>I suggest leaving the error messages as they are (despite the odd numbering), but feel free to edit the allowed extensions to suit your needs.  As we move through the script, the error messages will be further explained.</p>
<p>The next step in my upload script is to create two functions with very specific, well, functions.  The first will be used when the upload fails:</p>
<pre class="brush: php">

function uploadFail($error) {
header(&quot;refresh: 3; url=index.php&quot;);
echo &quot;&lt;p&gt;&quot;.$error.&quot;  You are now being returned to the upload form.&lt;/p&gt;&quot;;
}
</pre>
<p>This will return the user to the upload form just after displaying the error message which is passed to the function.  The next function will obtain the file&#8217;s extension:</p>
<pre class="brush: php">

function getExtension($file) {
$file = explode(&quot;.&quot;,$file);
return strtolower($file[count($file)-1]);
}
</pre>
<p>Certainly there are several methods for obtaining the file&#8217;s extension, but this is the shortest one I could put together.  Our last stop before starting the handling is one more array which is completely optional.  I use it to ease the process of getting the file&#8217;s attributes:</p>
<pre class="brush: php">

$file = array(&quot;tmp&quot; =&gt; $_FILES[&#039;file&#039;][&#039;tmp_name&#039;],
&quot;name&quot; =&gt; $_FILES[&#039;file&#039;][&#039;name&#039;],
&quot;type&quot; =&gt; $_FILES[&#039;file&#039;][&#039;type&#039;],
&quot;size&quot; =&gt; $_FILES[&#039;file&#039;][&#039;size&#039;],
&quot;error&quot; =&gt; $_FILES[&#039;file&#039;][&#039;error&#039;]);
$file[&#039;ext&#039;] = getExtension($file[&#039;name&#039;]);
</pre>
<p>This will remove the need for such long lines each time an attribute is needed, though this step is not necessary.  Note that we used our extension-finding function to do just that, and also be aware that the file type (MIME type) is available if you wish to check the type that way.</p>
<p>Finally, we&#8217;re here!  We are ready to begin handling the file and checking for errors.  First we&#8217;ll make sure the page is accessed through the form by making sure the &#8216;submit&#8217; button was pressed:</p>
<pre class="brush: php">

if (!isset($_POST[&#039;submit&#039;])) { uploadFail(&quot;You may only access this page through the form.&quot;); }
</pre>
<p>Next, we&#8217;ll check for those PHP errors we specified earlier.  If the variable is set to &#8217;0,&#8217; that means there is no error.  Otherwise, let&#8217;s reference the array we set earlier and return to the form.  Note that the inherent PHP error returns an integer based on the error encountered.  Our descriptions match pretty well what the problem is, but you can read more extensive descriptions <a href="http://us.php.net/manual/en/features.file-upload.errors.php" target="_blank">in the manual</a>.</p>
<pre class="brush: php">

else {
if ($file[&#039;error&#039;] != 0) { uploadFail($errors[$file[&#039;error&#039;]]); }
</pre>
<p>The last step in checking the file is to reference it against the extensions we listed earlier, so let&#8217;s do that now:</p>
<pre class="brush: php">

else {
if (!in_array($file[&#039;ext&#039;],$allowedExtensions)) { uploadFail(&quot;Uploaded file (&quot;.$file[&#039;name&#039;].&quot;) is of a disallowed type.&quot;); }
</pre>
<p>If the file makes it this far, that means everything checks out and we just have two steps left: setting the final file name and moving it to the &#8216;files&#8217; folder.</p>
<pre class="brush: php">

else { //Now the file is fine; let&#039;s finish the upload
//Make sure file name is not already in use
while (file_exists(&quot;files/&quot;.$file[&#039;name&#039;])) {
$file[&#039;name&#039;] = time().&quot;-&quot;.$file[&#039;name&#039;];
}
//Finally move the file to the proper place
if(move_uploaded_file($file[&#039;tmp&#039;],&quot;files/&quot;.$file[&#039;name&#039;])) {
header(&quot;Location: success.php&quot;);
}
</pre>
<p>Notice we use the <a href="http://php.net/fileexists" target="_blank">file_exists()</a> function to make sure a file of the same name isn&#8217;t already located in the folder, and if so, we rename it something sure to be more unique.  Then, we use the <a href="http://www.php.net/moveuploadedfile" target="_blank">move_uploaded_file()</a> function to move the temporary file to its final resting place, and we take the user to the &#8216;success&#8217; page where they are greeted with much love and praise for finishing the entire process.  You can create a custom file however you wish and reference it in your &#8216;upload&#8217; file.</p>
<p>All together, the &#8216;upload&#8217; file can seem complicated, but it isn&#8217;t too tough.  By establishing easy-to-use functions and variables early in the process, we were able to simplify the task and break it down into manageable parts.  The finished script looks like this:</p>
<pre class="brush: php">

&lt;?php
//The following array will hold all PHP error messages
$errors = array(1 =&gt; &quot;The file exceeds the maximum file size specification.&quot;,
2 =&gt; &quot;The file exceeds the maximum file size specification.&quot;,
3 =&gt; &quot;The file was not fully uploaded.&quot;,
4 =&gt; &quot;The file was not properly uploaded.&quot;,
6 =&gt; &quot;An error occurred during uploading.&quot;,
7 =&gt; &quot;An error occurred during uploading.&quot;,
8 =&gt; &quot;An error occurred during uploading.&quot;);
$allowedExtensions = array(&quot;jpeg&quot;,&quot;jpg&quot;,&quot;png&quot;,&quot;gif&quot;,&quot;txt&quot;);
//This function will return the user to the form, displaying the proper error message
function uploadFail($error) {
header(&quot;refresh: 3; url=index.php&quot;);
echo &quot;&lt;p&gt;&quot;.$error.&quot;  You are now being returned to the upload form.&lt;/p&gt;&quot;;
}
//This function will get the file&#039;s extension
function getExtension($file) {
$file = explode(&quot;.&quot;,$file);
return strtolower($file[count($file)-1]);
}
//The following array will hold all of our file information
$file = array(&quot;tmp&quot; =&gt; $_FILES[&#039;file&#039;][&#039;tmp_name&#039;],
&quot;name&quot; =&gt; $_FILES[&#039;file&#039;][&#039;name&#039;],
&quot;type&quot; =&gt; $_FILES[&#039;file&#039;][&#039;type&#039;],
&quot;size&quot; =&gt; $_FILES[&#039;file&#039;][&#039;size&#039;],
&quot;error&quot; =&gt; $_FILES[&#039;file&#039;][&#039;error&#039;]);
$file[&#039;ext&#039;] = getExtension($file[&#039;name&#039;]);
//Processing begins here
//Make sure the page is accessed through the form
if (!isset($_POST[&#039;submit&#039;])) { uploadFail(&quot;You may only access this page through the form.&quot;); }
else {
if ($file[&#039;error&#039;] != 0) { uploadFail($errors[$file[&#039;error&#039;]]); } //Check for PHP errors
else {
//Check file extension
if (!in_array($file[&#039;ext&#039;],$allowedExtensions)) { uploadFail(&quot;Uploaded file (&quot;.$file[&#039;name&#039;].&quot;) is of a disallowed type.&quot;); }
else { //Now the file is fine; let&#039;s finish the upload
//Make sure file name is not already in use
while (file_exists(&quot;files/&quot;.$file[&#039;name&#039;])) {
$file[&#039;name&#039;] = time().&quot;-&quot;.$file[&#039;name&#039;];
}
//Finally move the file to the proper place
if(move_uploaded_file($file[&#039;tmp&#039;],&quot;files/&quot;.$file[&#039;name&#039;])) {
header(&quot;Location: success.php&quot;);
}
else { uploadFail($errors[6]); }
}
}
}
?&gt;
</pre>
<p>Now you&#8217;re ready to make your website even more dynamic!  By allowing file uploads, your visitors have the chance to add their own unique contribution to a website they&#8217;ll enjoy even more!</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2009/01/handling-file-uploads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sending Data Between Pages</title>
		<link>http://phpstarter.net/2008/12/sending-data-between-pages/</link>
		<comments>http://phpstarter.net/2008/12/sending-data-between-pages/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 14:00:48 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://phpstarter.net/?p=57</guid>
		<description><![CDATA[When a visitor navigates through a website, the server doesn&#8217;t know who you are from one page to the next. In other words, if you read this page, and then click on another article or link, the server can&#8217;t remember who you are! It is up to the developer to retain any data between page [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>When a visitor navigates through a website, the server doesn&#8217;t know who you are from one page to the next.  In other words, if you read this page, and then click on another article or link, the server can&#8217;t remember who you are!  It is up to the developer to retain any data between page requests, and we will cover the best ways to do it in this article.</p>
<p><span id="more-57"></span></p>
<h3>Retaining Data With PHP Sessions</h3>
<p>PHP has a built-in sessions feature that makes it the easiest to retain data from one page to the next.  As I said before, the server doesn&#8217;t know who you are from one page request to the next&#8230;unless you remind it every time.  That&#8217;s what PHP Sessions does.  When you start a session, PHP sends the session id to the client&#8217;s browser in the form of a cookie.  Then the browser sends this cookie back to the server on every page request.  In order to start or maintain a session, we only need this statement:</p>
<pre class="brush: php">
&lt;?php
session_start();
?&gt;
</pre>
<p>All you have to do is put that one line of code at the top of your pages to hold a session on your website.  This single line of code will start a new session or maintain the current one, depending if the site visitor has an active session or not.  If you don&#8217;t put this line of code at the very top, you may see this warning:</p>
<p><code>Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/user/php/test.php:3) in /home/user/php/test.php on line 4</code></p>
<p><code>Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/user/php/test.php:3) in /home/user/php/test.php on line 4</code></p>
<p>If you get any of these warnings, make sure that you aren&#8217;t sending any output before you run the session_start() statement.</p>
<p>Now that your session is active, storing and manging the data is just as easy.  All the data is stored in a single global array: $_SESSION.  Everything that is stored in there will be retained as long as the session is active.</p>
<p>When you are ready to destroy a session, like when a user logs out, simply run session_destroy().  There are many other items to know about sessions, like what to do if the visitor&#8217;s browser doesn&#8217;t accept cookies.  All that and more can be answered by visiting <a href="http://us3.php.net/manual/en/book.session.php">this page</a> in the PHP manual.</p>
<h3>Sending Data in the URL</h3>
<p>Sending data in the URL is probably the easiest way to send data from one page to the other.  Take this example URL: http://www.example.com?<strong>variable=value&#038;second=something</strong>.  The bolded text is the &#8220;query string&#8221;.  The question mark separates the query string from the URL, and each variable/value pair is separated by an ampersand (&#038;).  These variables become part of the $_GET array, so in our example URL, $_GET['variable'] equals &#8216;value&#8217;, and $_GET['second'] equals &#8216;something&#8217;.</p>
<p>More information on $_GET can be found <a href="http://us3.php.net/manual/en/reserved.variables.get.php">here</a>.</p>
<h3>Sending Data via a Form</h3>
<p>This method is for sending data that can&#8217;t be sent directly in the URL.  This could be because it&#8217;s sensitive data, or maybe because you want your URLs to be clean.  You need to build an HTML form first to send the data.</p>
<pre class="brush: html">
&lt;form method=&quot;post&quot; action=&quot;/path/to/script.php&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;foo&quot; value=&quot;bar&quot; /&gt;
&lt;input type=&quot;text&quot; name=&quot;test_field&quot; value=&quot;&quot; /&gt;
&lt;input type=&quot;submit&quot; /&gt;
&lt;/form&gt;
</pre>
<p>Similar to the $_GET array, all data will go into a $_POST array.  So if our example was submitted, $_POST['foo'] would equal &#8216;bar&#8217;, and $_POST['test_field'] would equal whatever was typed in the text field.</p>
<p>More information on $_POST can be found <a href="http://us3.php.net/manual/en/reserved.variables.post.php">here</a>.</p>
<p>Have something to say or a question to ask?  Post a comment.  Please also use the contact form if there is an article you would like to see.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2008/12/sending-data-between-pages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding PHP Tags &amp; Code Blocks</title>
		<link>http://phpstarter.net/2008/11/understanding-php-tags-code-blocks/</link>
		<comments>http://phpstarter.net/2008/11/understanding-php-tags-code-blocks/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 02:25:04 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://stage.phpstarter.net/?p=31</guid>
		<description><![CDATA[PHP was originally designed as a tool to make a website&#8217;s HTML more dynamic, so it&#8217;s no wonder that it mixes well with existing HTML in your website. There are several options for defining your PHP blocks, and we will cover them all here. Most Common Tag and How It&#8217;s Used In a previous PHP [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>PHP was originally designed as a tool to make a website&#8217;s HTML more dynamic, so it&#8217;s no wonder that it mixes well with existing HTML in your website.  There are several options for defining your PHP blocks, and we will cover them all here.</p>
<p><span id="more-31"></span></p>
<p><strong>Most Common Tag and How It&#8217;s Used</strong></p>
<p>In a <a href="/2008/11/your-first-php-script/">previous PHP article</a>, I had a sample script.  Here it is again:</p>
<pre class="brush: php">
&lt;?php
    /* output our intro message */
    echo &#039;It works!&#039;;
?&gt;
</pre>
<p>This is the most common PHP tag, and the most supported.  Generally, this will be the only tag you need to worry about.  Here would be another example in the context of a typical HTML page:</p>
<pre class="brush: php">
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Sample PHP Script w/ HTML&lt;/title&gt;
&lt;meta name=&quot;generator&quot; content=&quot;Bluefish 1.0.7&quot;&gt;
&lt;meta name=&quot;author&quot; content=&quot;Andrew Wells&quot;&gt;
&lt;meta name=&quot;date&quot; content=&quot;2008-11-29T13:23:32-0600&quot;&gt;
&lt;meta name=&quot;copyright&quot; content=&quot;2008 PHPStarter&quot;&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;A sample script showing how to mix PHP &amp; HTML.&quot;&gt;
&lt;meta name=&quot;description&quot; content=&quot;php, html, phpstarter&quot;&gt;
&lt;meta name=&quot;ROBOTS&quot; content=&quot;INDEX, FOLLOW&quot;&gt;
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=UTF-8&quot;&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php
    /* output our intro message */
    echo &#039;It works!&#039;;
?&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>PHP only parses the code in the PHP tags, so the other HTML is left alone, and we result in an HTML page like this:</p>
<pre class="brush: php">
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Sample PHP Script w/ HTML&lt;/title&gt;
&lt;meta name=&quot;generator&quot; content=&quot;Bluefish 1.0.7&quot;&gt;
&lt;meta name=&quot;author&quot; content=&quot;Andrew Wells&quot;&gt;
&lt;meta name=&quot;date&quot; content=&quot;2008-11-29T15:55:40-0600&quot;&gt;
&lt;meta name=&quot;copyright&quot; content=&quot;2008 PHPStarter&quot;&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;A sample script showing how to mix PHP &amp; HTML.&quot;&gt;
&lt;meta name=&quot;description&quot; content=&quot;php, html, phpstarter&quot;&gt;
&lt;meta name=&quot;ROBOTS&quot; content=&quot;INDEX, FOLLOW&quot;&gt;
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml; charset=UTF-8&quot;&gt;
&lt;/head&gt;
&lt;body&gt;

It works!
&lt;/body&gt;
</pre>
<p><strong>Variables with Multiple Blocks &#038; Short Tags</strong></p>
<p>PHP function and variable declarations do stay intact between blocks of code, so this example would output &#8216;Hello, Andrew!&#8217;.</p>
<pre class="brush: php">
&lt;?php
    $username = &#039;Andrew&#039;;
?&gt;
&lt;p&gt;Hello, &lt;?php echo $username; ?&gt;!&lt;/p&gt;
</pre>
<p>This can get pretty confusing with several blocks of PHP mixed in with HTML.  To help keep code neater, there is a shorthand way to output variables in HTML:</p>
<pre class="brush: php">
&lt;?php
    $username = &#039;Andrew&#039;;
?&gt;
&lt;p&gt;Hello, &lt;?=$username?&gt;!&lt;/p&gt;
</pre>
<p>In the above example, the code to output the username is quite condensed using &#8220;short tags&#8221;.  Although this method is convenient, it is generally not recommended if you plan to redistribute your code to other servers, as some might have this option disabled.</p>
<p><strong>ASP-Style Tags</strong></p>
<p>I don&#8217;t know why PHP provides this as an option.  I strongly discourage its use and I have it disabled on all of my servers.  Nevertheless, it&#8217;s a working option for you to use.</p>
<pre class="brush: php">
&lt;%
    /* output our intro message */
    echo &#039;It works!&#039;;
%&gt;
</pre>
<p><strong>One Last Alternate PHP Opening/Closing Tag</strong></p>
<p>Many developers don&#8217;t know this, but you can also use the <a href="http://www.w3schools.com/TAGS/tag_script.asp">HTML script tag</a> to define your PHP code.  This is the non-standard way to use this tag because this tag is normally used for client-side scripting, or scripts that are ran by the client&#8217;s browser.  PHP is a server-side language, or a scripting language that is parsed by the server, and the clients are only served the results.  I&#8217;m guessing that this method was implemented because WYSIWYG editors like MS FrontPage gets confused when it sees regular PHP tags, but recognize this script tag.  Here is an example:</p>
<pre class="brush: php">
&lt;script language=&quot;php&quot;&gt;
    /* output our intro message */
    echo &#039;It works!&#039;;
&lt;/script&gt;
</pre>
<p>So there you have it.  Inserting PHP code into your HTML pages is now a no-brainer. <img src='http://phpstarter.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2008/11/understanding-php-tags-code-blocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Your First PHP Script</title>
		<link>http://phpstarter.net/2008/11/your-first-php-script/</link>
		<comments>http://phpstarter.net/2008/11/your-first-php-script/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 17:56:55 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://stage.phpstarter.net/?p=26</guid>
		<description><![CDATA[Since PHP is a scripting language, creating your first PHP script is as easy as creating and saving a text file. In this article, you are going to be shown how simple it is to create your first PHP script. Here is the code we will be working with. Every line will be explained in [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Since PHP is a scripting language, creating your first PHP script is as easy as creating and saving a text file.  In this article, you are going to be shown how simple it is to create your first PHP script.</p>
<p><span id="more-26"></span></p>
<p>Here is the code we will be working with.  Every line will be explained in detail:</p>
<pre class="brush: php">
&lt;?php
    /* output our intro message */
    echo &#039;It works!&#039;;
?&gt;
</pre>
<p>Copy that code in a text file, save it as text.php, and then upload it to your PHP-enabled web server, and you should see &#8220;It works!&#8221; in your web browser when you call up that script.</p>
<p><strong>Lines 1 &#038; 4: PHP Tags</strong></p>
<p>All PHP code has to be within PHP tags, like this: &lt;?php (php code goes here) ?&gt;</p>
<p>As you can see, the PHP tags can be in the same line in the code as I just did, or they can be on different lines, as in the first example.  We need to surround all PHP code in these tags because it is possible to mix PHP &#038; HTML in the same file.</p>
<p><strong>Line 2: Comment</strong></p>
<p>It&#8217;s best to get into the habit of commenting your code now.  There are several ways to comment your code:</p>
<pre class="brush: php">
/* this comment can
be
on
multiple
lines */

# single-line comment

// single-line comment
</pre>
<p>Lines 1-5 shows the best way write a comment that covers multiple lines.  Lines 7 &#038; 8 only work on one line as the example shows.</p>
<p><strong>Line 3: &#8216;echo&#8217; Statement</strong></p>
<p>The <a href="http://www.php.net/manual/en/function.echo.php">echo</a> statement is what sends data to the browser.  There is another statement that accomplishes the same thing: <a href="http://us3.php.net/manual/en/function.print.php">print</a>.  There are <a href="http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40">tiny differences</a> between the two statements, but for our introductory article, it&#8217;s safe to assume that they are pretty much the same.</p>
<p>So there you have it, a simple script, explained.  <a href="/tag/beginner/">Read more articles</a> tagged for beginners for more great PHP articles for beginners.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://phpstarter.net/2008/11/your-first-php-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
