<?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>Who... What... Where... &#187; Variables</title>
	<atom:link href="http://www.yeltuor.com/articles/tag/variables/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.yeltuor.com</link>
	<description>General things I find interesting</description>
	<lastBuildDate>Sun, 02 Aug 2009 09:58:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP &#8211; Basic</title>
		<link>http://www.yeltuor.com/articles/script/php-basic/</link>
		<comments>http://www.yeltuor.com/articles/script/php-basic/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 22:30:37 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[Scripting Snippets]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Conditional]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[Operators]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Variables]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=41</guid>
		<description><![CDATA[PHP is a server-side scripting language which allow for the creation dynamic and interactive webpages, standing for PHP: Hypertext Preprocessor
It is the widely-used, free, and efficient alternative to competitors such as Microsoft&#8217;s ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code of a page.
PHP is often used [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p>PHP is a server-side scripting language which allow for the creation dynamic and interactive webpages, standing for <strong>P</strong>HP: <strong>H</strong>ypertext <strong>P</strong>reprocessor</p>
<p>It is the widely-used, free, and efficient alternative to competitors such as Microsoft&#8217;s ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code of a page.</p>
<p>PHP is often used together with the <a class="external text" title="Apache Web Server" rel="nofollow" href="http://www.apache.org/" target="_blank">Apache</a> web server on various operating systems. It also supports ISAPI and can be used with Microsoft&#8217;s IIS on Windows.</p>
<p>This article will go through some useful PHP syntax and uses.</p>
<p><a title="Basic_Syntax" name="Basic_Syntax"></a></p>
<h3>Basic Syntax</h3>
<p>Basic Syntax included in a PHP page and how in integrates with HTML</p>
<p>A piece of PHP code will always start with the tag <strong>&lt;?php</strong> and end with <strong>?&gt;</strong></p>
<p>Using the <strong>echo &#8221; &#8221; ;</strong> tag displays standard HTML that the browser will display. To echo multiple strings of variables (explained later) use a dot <strong>.</strong> &lt;HTML&gt; &lt;BODY&gt; &lt;?php echo &#8220;&lt;h1&gt; Hello World &lt;/h1&gt;&#8221;; echo &#8220;Hello&#8221; . &#8220;World&#8221;; ?&gt; &lt;/BODY&gt; &lt;/HTML&gt;</p>
<p><a title="Comments" name="Comments"></a></p>
<h4>Comments</h4>
<p>Adding comments in PHP is just as important as any other programming language. within PHP there are two different methods for adding comments, through the <strong>//</strong> syntax for a single line, or to add a block of comment use <strong>/*</strong> to start, and <strong>*/</strong> to finish the block</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
//Single Comment<br />
/*<br />
Start of comment block<br />
Still commenting<br />
*/<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Variables" name="Variables"></a></p>
<h4>Variables</h4>
<p>Variables are not typed in PHP, when referencing variables in PHP they just start with the <strong>$</strong> symbol.</p>
<p>The snippet of code assigns a variable, then uses the <strong>echo</strong> command to output its value</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
$var1=&#8221;Hello World&#8221;;<br />
echo $var1;<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Operators" name="Operators"></a></p>
<h4>Operators</h4>
<p>PHP has the several standard operators which can be used, see table below</p>
<table style="width: 50%;" border="1">
<caption>PHP Operators </caption>
<tbody>
<tr>
<th>Operator</th>
<th>Action</th>
</tr>
<tr>
<td>+</td>
<td>Add</td>
</tr>
<tr>
<td>-</td>
<td>Subtract</td>
</tr>
<tr>
<td>*</td>
<td>Multiply</td>
</tr>
<tr>
<td>/</td>
<td>Divide</td>
</tr>
<tr>
<td>%</td>
<td>Modulus</td>
</tr>
<tr>
<td>++</td>
<td>Increment</td>
</tr>
<tr>
<td>&#8211;</td>
<td>Decrement</td>
</tr>
<tr>
<td>=</td>
<td>Equals</td>
</tr>
<tr>
<td>==</td>
<td>Is equal to</td>
</tr>
<tr>
<td>!=</td>
<td>Is NOT equal to</td>
</tr>
<tr>
<td>&lt;</td>
<td>Less than</td>
</tr>
<tr>
<td>&gt;</td>
<td>Greater than</td>
</tr>
<tr>
<td>&lt;=</td>
<td>Less than or equal</td>
</tr>
<tr>
<td>&gt;=</td>
<td>Greater than or equal</td>
</tr>
<tr>
<td>&amp;&amp;</td>
<td>and</td>
</tr>
<tr>
<td>||</td>
<td>or</td>
</tr>
<tr>
<td>!</td>
<td>not</td>
</tr>
</tbody>
</table>
<h3>Conditional Statements</h3>
<p>Conditional statements help make PHP a dynamic language, being able to put if statements into webpages allows for the generation of dynamic HTML. The common statements are described below:</p>
<p><a title="If_statement" name="If_statement"></a></p>
<h4>If statement</h4>
<p>The most common statement, the syntax for this is shown below</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
if ($x==$y)<br />
echo $x . &#8221; does equal &#8221; . $y;<br />
elseif ($x &lt; $y)<br />
echo $x . &#8221; is less than &#8221; . $y;<br />
else<br />
echo $x . &#8221; does not equal &#8221; . $y;<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Looping_Statements" name="Looping_Statements"></a></p>
<h3>Looping Statements</h3>
<p>Loops are used to save repetition of code, instead a piece of code is used once and told to be repeated a number of times, or until a condition is met.</p>
<p><a title="While_statement" name="While_statement"></a></p>
<h4>While statement</h4>
<p>The While statement should be used when the number of iterations is unknown, the syntax is shown below</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
while ($x != $y)<br />
{<br />
$x++;<br />
echo &#8220;X is &#8221; . $x;<br />
}<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p>The other method of writing a While loop is the <strong>do</strong> &#8230; <strong>while</strong> loop. In this example the code is always run once before the <strong>do</strong> statement is reached.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
do<br />
{<br />
$x&#8211;;<br />
echo &#8220;X is &#8221; . $x;<br />
}<br />
while ($x &gt; 15);<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="For_Loop" name="For_Loop"></a></p>
<h4>For Loop</h4>
<p>This statement is used when the number of repetitions required is known.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
for ($i=1; $i&lt;=5; $i++)<br />
{<br />
echo &#8220;i is &#8221; . $i;<br />
}<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Foreach_Loop" name="Foreach_Loop"></a></p>
<h4>Foreach Loop</h4>
<p>This is very similar to the For Loop above, however in this case the loop is done through an array which is passed into the loop.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
$arr1=array(&#8220;dayone&#8221;, &#8220;daytwo&#8221;, &#8220;daythree&#8221;);<br />
foreach ($arr1 as $value)<br />
{<br />
echo &#8220;Value is &#8221; . $value;<br />
}<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Arrays" name="Arrays"></a></p>
<h3>Arrays</h3>
<p>Arrays are very useful data types which are commonly under used. an array can consist of any type of data that is required, is provides a better way of storing the data and opens up easier ways of using the data when in this format.</p>
<p>There are three different types of array:</p>
<ul>
<li>Numeric Arrays &#8211; array where there is a number that references the position in the array when being accessed</li>
<li>Associative arrays &#8211; array where values are used to reference the location within the array</li>
<li>Multidimensional arrays &#8211; Simply an array with has more than one array within it</li>
</ul>
<p><a title="Numeric_Array" name="Numeric_Array"></a></p>
<h4>Numeric Array</h4>
<p>Two different methods of creating these types of arrays, both yield the same result</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
$arr1=array(&#8220;dayone&#8221;, &#8220;daytwo&#8221;, &#8220;daythree&#8221;);</p>
<p>$arr2[0]=vaule1;<br />
$arr2[1]=vaule2;<br />
$arr2[2]=vaule3;</p>
<p>//Referencing the array is done the same way<br />
echo $arr1[0];<br />
echo $arr2[1];<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Associative_Array" name="Associative_Array"></a></p>
<h4>Associative Array</h4>
<p>This array type does not use standard numbering that numeric array does (0, 1, 2, 3&#8230;), here the array entries are referenced by user defined values. Again there are two different ways of creating this array type.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
$arr1=array(&#8220;dayone&#8221;=&gt;28, &#8220;daytwo&#8221;=&gt;25, &#8220;daythree&#8221;=&gt;20);</p>
<p>$arr2['monday']= &#8220;11&#8243;;<br />
$arr2['tuesday']=&#8221;12&#8243;;<br />
$arr2['wednesday']=&#8221;13&#8243;;</p>
<p>//Referencing the array is done the same way<br />
echo $arr1['dayone'];<br />
echo $arr2['tuesday'];<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Multidimensional_Array" name="Multidimensional_Array"></a></p>
<h4>Multidimensional Array</h4>
<p>The Multidimensional array is the most complicated of them all, when implemented a grid like array is formed. When implementing a multidimensional array the sub array of each array item does not have to be the same size, as shown below.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?php<br />
$streets = array(<br />
&#8220;Highstreet&#8221;=&gt;array(<br />
&#8220;No1&#8243;,<br />
&#8220;No5&#8243;,<br />
&#8220;No10&#8243;,<br />
),<br />
&#8220;MiddleStreet&#8221;=&gt;array(<br />
&#8220;No100&#8243;<br />
),<br />
&#8220;LowStreet&#8221;=&gt;array(<br />
&#8220;No33&#8243;,<br />
&#8220;No34&#8243;,<br />
&#8220;No35&#8243;<br />
)<br />
);<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="Forms" name="Forms"></a></p>
<h3>Forms</h3>
<p>With the use of HTML forms and PHP, information can be passed between pages easily through using the different methods available.</p>
<p>A basic HTML form looks like the following:</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;FORM action=&#8221;page2.php&#8221; method=&#8221;POST&#8221;&gt;<br />
Enter value1: &lt;input type=&#8221;text&#8221; name=&#8221;value1&#8243; /&gt;<br />
Enter value2: &lt;input type=&#8221;text&#8221; name=&#8221;value2&#8243; /&gt;<br />
&lt;input type=&#8221;submit&#8221; /&gt;<br />
&lt;/FORM&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p>Once submitted to <em>page2.php</em> the values can be shown with the following code:</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
Hello World&lt;br&gt;<br />
&lt;?php echo $_POST["value1"]; ?&gt;&lt;br&gt;<br />
&lt;?php echo $_POST["value2"]; ?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p>The syntax <strong>$_POST</strong> changes to <strong>$_GET</strong> depending on the form method. The final piece of syntax is <strong>$_REQUEST</strong>, this contains values for $_GET, $_POST and $_COOKIE (discussed later)</p>
<p>Form <em>Get</em> method displays the data transferred between pages in the address bar, the advantage of this is the new page can be book-marked and each time opened the same data will appear. The disadvantage the limit of 100 characters per variable.</p>
<p>Form <em>Post</em> method does not display the data transferred between pages in the address bar, instead the data is piped directly between pages. in this case the new page can&#8217;t be book-marked successfully, however the variable size limit is not enforced.<br />
Many thanks to the tutorials where this information came from, <a class="external text" title="W3Schools" rel="nofollow" href="http://www.w3schools.com/php/default.asp" target="_blank">W3Schools</a> &amp; <a class="external text" title="PHP .net" rel="nofollow" href="http://uk.php.net/manual/en/introduction.php" target="_blank">PHP</a></div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/array/" title="Array" rel="tag">Array</a>, <a href="http://www.yeltuor.com/articles/tag/conditional/" title="Conditional" rel="tag">Conditional</a>, <a href="http://www.yeltuor.com/articles/tag/loop/" title="Loop" rel="tag">Loop</a>, <a href="http://www.yeltuor.com/articles/tag/operators/" title="Operators" rel="tag">Operators</a>, <a href="http://www.yeltuor.com/articles/tag/php/" title="PHP" rel="tag">PHP</a>, <a href="http://www.yeltuor.com/articles/tag/variables/" title="Variables" rel="tag">Variables</a>, <a href="http://www.yeltuor.com/articles/tag/web/" title="Web" rel="tag">Web</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.yeltuor.com/articles/script/php-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
