<?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; PHP</title>
	<atom:link href="http://www.yeltuor.com/articles/tag/php/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; Email Advanced</title>
		<link>http://www.yeltuor.com/articles/how-to/php-email-advanced/</link>
		<comments>http://www.yeltuor.com/articles/how-to/php-email-advanced/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 22:48:10 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=63</guid>
		<description><![CDATA[For a simple introduction to the mail() function see the  PHP_Advanced article. This article describes some of the more advanced features  that can be achieved through the mail function.

Assign Names to email addresses
When receiving an email you will notice that the To field often  contains a name rather than the email address [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p>For a simple introduction to the <strong>mail()</strong> function see the  PHP_Advanced article. This article describes some of the more advanced features  that can be achieved through the <em>mail</em> function.</p>
<p><a title="Assign_Names_to_email_addresses" name="Assign_Names_to_email_addresses"></a></p>
<h3>Assign Names to email addresses</h3>
<p>When receiving an email you will notice that the <em>To</em> field often  contains a name rather than the email address it was send to.</p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?PHP<br />
mail(&#8216;bob@email.com&#8217;, &#8216;Test email&#8217;,<br />
&#8216;This is a test email&#8217;,<br />
&#8220;To: Bob Jones &lt;bob@email.com&gt;\n&#8221; .<br />
&#8220;From: Jane Jones &lt;jane@email.com&gt;\n&#8221; .<br />
&#8220;cc: Another Person &lt;another@email.com&gt;\n&#8221; .<br />
&#8220;Bcc: Yet Another &lt;more@email.com\n&gt;&#8221;);<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p><a title="HTML_Emails" name="HTML_Emails"></a></p>
<h3>HTML Emails</h3>
<p>The next stage is sending HTML email messages, this allows for standard HTML  tags to be used when composing the message content. When sending a message in  HTML it must be declared that is it HTML in the header of the email, this is  done through both the <em>Content-type:</em> and <em>MIME-Version headers&#8217;:</em></p>
<blockquote><p>&lt;HTML&gt;<br />
&lt;BODY&gt;<br />
&lt;?PHP<br />
mail(&#8216;bob@email.com&#8217;, &#8216;Test email&#8217;,<br />
&#8216;&lt;html&gt;&lt;body&gt;&lt;b&gt;Hello! World&lt;/b&gt; \n &lt;i&gt;this is a test email&lt;/i&gt;&lt;/body&gt;&lt;/html&gt;&#8217;,<br />
&#8220;MIME-Version: 1.0\n&#8221; .<br />
&#8220;Content-type: text/html; charset=iso-8859-1&#8243;);<br />
?&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;</p></blockquote>
<p>The <em>MIME-Version</em> (<strong>M</strong>ulitpurpose  <strong>I</strong>nternet <strong>M</strong>ail <strong>E</strong>xtensions)  header indicates that the email follows the internet standards, following that  the <em>Content-type</em> header can declare the format being used;  <em>text/html;</em> followed by the <a class="external text" title="Character-sets" rel="nofollow" href="http://www.iana.org/assignments/character-sets" target="_blank">character set</a> being used <em>charset=iso-8859-1</em></p>
<p><a title="Mixed_Format_Emails" name="Mixed_Format_Emails"></a></p>
<h3>Mixed Format Emails</h3>
<p>Although the majority of email clients support HTML email messages, there are  some that don&#8217;t. The mixed format ensures that the email clients that do support  it see the HTML formatted message, where as the ones that don&#8217;t see a plain text  version.</p>
<p>The technique involved is to actually send two versions of the message and  rely on the email client to read and understand <em>Content-Type:  multipart/alternative;</em> header which will make the client only display the  supported version.</p>
<blockquote><p>*** PHP code not fully completed yet ***</p></blockquote>
<p><a title="Emailing_Attachments" name="Emailing_Attachments"></a></p>
<h3>Emailing Attachments</h3>
<p>Emailing file Attachments work in the same way that mixed format email  messages do. The header <em>Content-Type: multipart/mixed;</em> is used and the  message split into two parts; one the message and the other the file  attachment(s).</p>
<p>This is more complicated than previous email examples, all the steps required  are explained below. The examples assume that the email details including the  file to be emailed have been submitted to the PHP page from another page.</p>
<blockquote><p>$to      = $_POST['to'];<br />
$from    = $_POST['from'];<br />
$subject = $_POST['subject'];<br />
$message = $_POST['message'];</p></blockquote>
<p><a title="Attributes_of_the_file_attachment" name="Attributes_of_the_file_attachment"></a></p>
<h4>Attributes of the file attachment</h4>
<p>The first stage is to extract the required attributed from the file that has  been passed. The file details in PHP are stored in an array named  <em>$_FILES</em> which are extracted to variables.</p>
<blockquote><p>// example: /tmp/phpfile12345 &#8211; tmp file name and loc where uploaded<br />
$file_loc  = $_FILES['fileatt']['tmp_name'];<br />
// example: text/text &#8211; will vary depending on file type<br />
$file_type = $_FILES['fileatt']['type'];<br />
// example: mywork.txt &#8211; always the name of the file<br />
$file_name = $_FILES['fileatt']['name'];</p></blockquote>
<p><a title="Extract_data_from_file_attachment" name="Extract_data_from_file_attachment"></a></p>
<h4>Extract data from file attachment</h4>
<p>The data within the file is required to be placed into a variable then used  Base64 encoding to convert (possible) binary data into text. The <a class="external text" title="PHP function is_uploaded_file" rel="nofollow" href="http://uk.php.net/is_uploaded_file" target="_blank">is_uploaded_file</a> function is  used to ensure that the file <em>was</em> in fact uploaded by an http get  command, this helps to ensure no malicious activity.</p>
<blockquote><p>if (is_uploaded_file($file_loc)) {<br />
// Read the file in &#8216;rb&#8217; read binary<br />
$file = fopen($file_loc,&#8217;rb&#8217;);<br />
$filedata = fread( $file <em>(comma)</em> filesize ($file_loc));<br />
// Base64 encode the file data<br />
$filedata = chunk_split(base64_encode($filedata));</p></blockquote>
<p>The data is now in a format that is ready to be emailed, the next stage is  producing the standard mail parameters.</p>
<p><a title="Producing_mail_function" name="Producing_mail_function"></a></p>
<h4>Producing mail function</h4>
<p>The basic mail parameters are set in the same manor, the diffrences come in  the <em>header</em> and <em>message</em> parameters</p>
<p>The header parameter contains the <em>MIME</em> version, the  <em>Content-Type: multipart/mixed;</em> declares that there will be an  attachment, finally the boundary string (containing random text) is used as a  marker to split the message into the two sections.</p>
<blockquote><p>&#8220;\nMIME-Version: 1.0\n&#8221; .<br />
&#8220;Content-Type: multipart/mixed;\n&#8221; .<br />
&#8221; boundary=\&#8221;==Multipart_Boundary_x45985365x\&#8221;";</p></blockquote>
<p>The message section starts with a declaration which MIME compatible email  clients will not show, next is the Multipart Boundary string denoting the  beginning of the first section. Following this the usual header information is  declared, following by the desired message text.</p>
<blockquote><p>&#8220;This is a multi-part message in MIME format. you should not see this\n\n&#8221; .<br />
&#8220;&#8211;==Multipart_Boundary_x45985365x\n&#8221; .<br />
&#8220;Content-Type: text/plain; charset=\&#8221;iso-8859-1\&#8221;\n&#8221; .<br />
&#8220;Content-Transfer-Encoding: 7bit\n\n&#8221; .<br />
&#8220;This is the message contents, there should be a file attached to this message&#8221;</p></blockquote>
<p>After the text of the message, the next part is the message attachment which  follows the same format as above.</p>
<blockquote><p>&#8220;&#8211;==Multipart_Boundary_x45985365x\n&#8221; .<br />
&#8220;Content-Type: {$file_type};\n&#8221; .<br />
&#8221; name=\&#8221;{$file_} \n&#8221; .<br />
&#8220;Content-Disposition: attachment;\n&#8221; .<br />
&#8221; filename=\&#8221;{$file_name}\&#8221;\n&#8221; .<br />
&#8220;Content-Transfer-Encoding: base64\n\n&#8221; .<br />
$filedata . &#8220;\n\n&#8221; .<br />
&#8220;&#8211;==Multipart_Boundary_x45985365x&#8211;\n&#8221;;</p></blockquote>
<p>Then message should always have the message boundary string followed by  <strong>&#8211;</strong> to signify the end.</p>
<p>To see the fully working source code, please see <a class="external text" title="PHP Email Code" rel="nofollow" href="http://www.yeltuor.com/s/files/PHP_Mail.rar">here</a></p>
<p>For more information on different MIME types see <a class="external text" title="MIMe types" rel="nofollow" href="http://www.ietf.org/rfc/rfc2045.txt" target="_blank">here</a><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> &amp; <a class="external text" title="Sitepoint" rel="nofollow" href="http://www.sitepoint.com/article/advanced-email-php" target="_blank">sitepoint</a></div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/email/" title="Email" rel="tag">Email</a>, <a href="http://www.yeltuor.com/articles/tag/php/" title="PHP" rel="tag">PHP</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/how-to/php-email-advanced/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Advanced</title>
		<link>http://www.yeltuor.com/articles/script/php-advanced/</link>
		<comments>http://www.yeltuor.com/articles/script/php-advanced/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 22:37:40 +0000</pubDate>
		<dc:creator>shane</dc:creator>
				<category><![CDATA[Scripting Snippets]]></category>
		<category><![CDATA[Cookies]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Files]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Sessions]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.yeltuor.com/?p=58</guid>
		<description><![CDATA[This article will guide you through some of the more complex features of PHP. a more basic introduction article is also available.

User Data
When browsing the internet you will come across some websites that store information about you or your visits to the site. There are two main methods of storing this data, these are through [...]]]></description>
			<content:encoded><![CDATA[<div class="content">
<p>This article will guide you through some of the more complex features of PHP. a more basic introduction article is also available.</p>
<p><a title="User_Data" name="User_Data"></a></p>
<h3>User Data</h3>
<p>When browsing the internet you will come across some websites that store information about you or your visits to the site. There are two main methods of storing this data, these are through <strong>Cookies</strong> and <strong>Sessions</strong>.</p>
<p><a title="Cookies" name="Cookies"></a></p>
<h4>Cookies</h4>
<p>Cookies are the older of the two methods, it consists of a client side file that the browser writes to. With PHP you can create new cookies and also retrieve existing values, examples of both are below</p>
<p>To create new cookies the <strong>setcookie</strong> function is used, the main parameters are:</p>
<ul>
<li>Name &#8211; The Name of the cookie</li>
<li>Value &#8211; Data that the cookie is to store</li>
<li>Expire &#8211; Time &amp; Date when the cookie will expire</li>
</ul>
<p>When creating cookies in PHP you must call the setcookie function <strong>before</strong> any HTML tags are used in the page, an example is shown below:</p>
<blockquote><p>
	 &lt;?PHP<br />
	setcookie(&#8220;yeltuor&#8221;, &#8220;MyValue&#8221;, time()+86400);<br />
	?&gt;<br />
	&lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	The cookie named yeltuor has been &#8217;set&#8217;<br />
	It will expire 24 Hours (86400 seconds) from now.<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>With the cookie now created, the next stage is to read back the values stored in it. The values in the cookie can be called in the same way variables are:</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	//Check whether the cookie named yeltuor is set<br />
	if (isset($_COOKIE["yeltuor"]))<br />
	echo &#8220;Welcome back &#8221; . $_COOKIE["yeltuor"] . &#8220;&lt;br&gt;&#8221;;<br />
	else<br />
	echo &#8220;Welcome newbe &lt;br&gt;&#8221;;<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p><a title="Sessions" name="Sessions"></a></p>
<h4>Sessions</h4>
<p>A Sessions is a server side file that is written to, again PHP support the creation and retrieval of session data. When a session is created a unique <em>session ID</em> is used to reference it, this is all taken care within the underlying PHP code.</p>
<p>The advantage of using sessions is that the user can&#8217;t view or edit the data within them; unlike the client side cookie&#8217;s. Before using sessions on a PHP page, the <em>session_start();</em> function must be called, after this a session can be created, however like the cookie functions all session calls must be done before any HTML code on the page:</p>
<p>Below is an example of setting up a server side session:</p>
<blockquote><p>
	 &lt;?PHP<br />
	//start session<br />
	session_start();<br />
	//if session variable total doesn&#8217;t exist create one<br />
	if(!isset($_SESSION['total']))<br />
	$_SESSION['total'] = &#8216;0&#8242;;<br />
	?&gt;<br />
	&lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	The session variable total set to 0, unless it already existed.<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>To reference existing session variables and dispose of variables that are no longer required, the following code can be used:</p>
<blockquote><p>
	 &lt;?PHP<br />
	//start session<br />
	session_start();<br />
	?&gt;<br />
	&lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	echo &#8220;The total is &#8221; . $_SESSION['total'];<br />
	//Finished with session variable, disposing of it<br />
	unset($_SESSION['total']);<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>For the security minded individuals, a list of more advanced session functions that are available can be seen <a class="external text" title="PHP Net" rel="nofollow" href="http://uk.php.net/session" target="_blank">here</a>.</p>
<p><a title="Files" name="Files"></a></p>
<h3>Files</h3>
<p>PHP supports various file related operations, some examples are below:</p>
<p>Opening a file is done with the <strong>fopen( );</strong> function, the first parameter is the filename, the second is the mode to open the file in:</p>
<table style="width: 70%;" border="1">
<tbody>
<tr>
<th>Mode</th>
<th>Description</th>
</tr>
<tr>
<td>r</td>
<td>Read Only, pointer at the beginning of the file</td>
</tr>
<tr>
<td>r+</td>
<td>Read and Write, pointer at the beginning of the file</td>
</tr>
<tr>
<td>w</td>
<td>Write Only, existing file will be truncated or new file will be created.</td>
</tr>
<tr>
<td>w+</td>
<td>Write and Read, existing file will be truncated or new file will be created.</td>
</tr>
<tr>
<td>a</td>
<td>Write Only, Places pointer at end of the file or new file will be created.</td>
</tr>
<tr>
<td>a+</td>
<td>Write and Read, Places pointer at end of the file or new file will be created.</td>
</tr>
<tr>
<td>x</td>
<td>Write Only, If the file already exists the <em>fopen( )</em> function will return <em>FALSE</em>, or a new file will be created.</td>
</tr>
<tr>
<td>x</td>
<td>Write and Read, If the file already exists the <em>fopen( )</em> function will return <em>FALSE</em>, or a new file will be created.</td>
</tr>
</tbody>
</table>
<p>Some examples of using the fopen( ) function are below:</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	//Open file hello.txt, in the case of an error exit<br />
	$f=fopen(&#8220;hello.txt&#8221;,&#8221;r&#8221;) or exit(&#8220;Cant open file&#8221;);<br />
	//if reached the end of the file<br />
	if (feof($f))<br />
	echo &#8220;End of file&#8221;;<br />
	//Loop through file 1 char at a time, echo value<br />
	while (!feof($f))<br />
	{<br />
	$x=fgetc($f);<br />
	echo $x;<br />
	}<br />
	//Close file handle when finished<br />
	fclose($f) ;<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>This function below accepts a parameter of a filename and location and returns the file data as a variable called <strong>contents</strong>:</p>
<blockquote><p>
	 function fileContents($filename){<br />
	// get contents of a file into a string<br />
	$f = fopen($filename, &#8220;r&#8221;);<br />
	$contents = fread($f, filesize($filename)) ;<br />
	fclose ($f);<br />
	return $contents;<br />
	}
</p></blockquote>
<p><a title="Functions" name="Functions"></a></p>
<h3>Functions</h3>
<p>Functions are used in the same way that procedures are used in other programming languages, they are used to aid with the re-use of code. examples of using functions can be seen below:</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	function echoHelloWorld($name)<br />
	{<br />
	echo &#8220;Hello World my name is &#8221; . $name;<br />
	}<br />
	//Using the created function<br />
	echoHelloworld(&#8220;Borris&#8221;)<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>Functions can also return values as an output by the <strong>return</strong> command from within the function.</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	function addnum($num1,$num2)<br />
	{<br />
	$tot = $num1 + $num2;<br />
	return $tot;<br />
	}<br />
	//Using the function addnum<br />
	echo &#8220;1 + 2 = &#8221; . addNum(1,2);<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p><a title="Require" name="Require"></a></p>
<h3>Require</h3>
<p>The require function works in the same way as a function, except that it is used to call another file which is inserted into the current page. This is useful when a <em>header</em> is being used;</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	require(&#8220;header.php&#8221;)<br />
	?&gt;<br />
	&lt;h3&gt;More text here.&lt;/h3&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p><a title="Email" name="Email"></a></p>
<h3>Email</h3>
<p>PHP has built in email support by using the <strong>mail()</strong> function, it requires the usual email parameters;</p>
<table style="width: 70%;" border="1">
<tbody>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
<tr>
<td>to</td>
<td>The recipients address in the form of abc@xyz.com</td>
</tr>
<tr>
<td>subject</td>
<td>Subject of the email</td>
</tr>
<tr>
<td>message</td>
<td>Content of the email; use the <em>\n</em> command for a new line</td>
</tr>
<tr>
<td>headers</td>
<td>An optional parameter which may contain and Cc&#8217;s of Bcc&#8217;s</td>
</tr>
<tr>
<td>parameters</td>
<td>Additional parameters that the sendmail program accepts i.e. From</td>
</tr>
</tbody>
</table>
<p>An example of the code for a mail function with error checking is shown below, to send emails to multiple recipients, a comma: <em>,</em> should be used as a delimiter.</p>
<blockquote><p>
	 &lt;HTML&gt;<br />
	&lt;BODY&gt;<br />
	&lt;?PHP<br />
	$to = &#8220;bob@email.com&#8221;;<br />
	$subject = &#8220;Test email&#8221;;<br />
	$message = &#8220;Hello! World \n this is a test email&#8221;;<br />
	$from = &#8220;tom@email.com&#8221;;<br />
	$headers = &#8220;From: $from&#8221;;<br />
	if (mail($to,$subject,$message,$headers))<br />
	echo &#8220;Mail Sent sucessfully&#8221;;<br />
	else<br />
	echo &#8220;Failed to Send Mail&#8221;;<br />
	?&gt;<br />
	&lt;/BODY&gt;<br />
	&lt;/HTML&gt;
</p></blockquote>
<p>This is very basic example of using emailing, however for a more complete guide to PHP emailing please see PHP Mail article.</p></div>

	Tags: <a href="http://www.yeltuor.com/articles/tag/cookies/" title="Cookies" rel="tag">Cookies</a>, <a href="http://www.yeltuor.com/articles/tag/email/" title="Email" rel="tag">Email</a>, <a href="http://www.yeltuor.com/articles/tag/files/" title="Files" rel="tag">Files</a>, <a href="http://www.yeltuor.com/articles/tag/php/" title="PHP" rel="tag">PHP</a>, <a href="http://www.yeltuor.com/articles/tag/sessions/" title="Sessions" rel="tag">Sessions</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-advanced/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
