<?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>Tiposaurus &#187; PHP</title>
	<atom:link href="http://www.tiposaurus.co.uk/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tiposaurus.co.uk</link>
	<description>Web Development and Web Hosting Tutorials</description>
	<lastBuildDate>Wed, 02 May 2012 20:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>How to use the query string in PHP</title>
		<link>http://www.tiposaurus.co.uk/2012/04/19/how-to-use-the-query-string-in-php/</link>
		<comments>http://www.tiposaurus.co.uk/2012/04/19/how-to-use-the-query-string-in-php/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 01:04:10 +0000</pubDate>
		<dc:creator>Steven</dc:creator>
				<category><![CDATA[Basics]]></category>
		<category><![CDATA[introductions]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[query string]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://discomoose.org/2005/10/19/how-to-use-the-query-string-in-php/</guid>
		<description><![CDATA[This article tells you how to pass variables to a PHP page using the query string, and how to access them from that page. Introduction Have you ever seen a URL which looked like &#34;www.example.com/page.php?mode=1&#38;style=red&#34;? Well, this page is being passed variables and their values through the query string, here the variables &#34;mode&#34; and &#34;style&#34; [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start -->
<p>This article tells you how to pass variables to a PHP page using the query string, and how to access them from that page.</p>
<h3>Introduction</h3>
<p>Have you ever seen a URL which looked like &quot;www.example.com/page.php?mode=1&amp;style=red&quot;? Well, this page is being passed variables and their values through the query string, here the variables &quot;mode&quot; and &quot;style&quot; are being passed, with values &quot;1&quot; and &quot;red&quot; respectively. The question mark indicates the start of the query string and the ampersand, &amp;, symbol seperates variable=value assignments. The page is then able to read these variables and react according to them, for example to display them to the user.</p>
<h3>Passing a query string to a page</h3>
<p>There are two ways to pass a QueryString to a page, the first is to enter it manually, for example:</p>
<pre>&lt;a href="page.php?mode=1"&gt;Mode 1&lt;/a&gt;</pre>
<p>The above HTML creates a link to a page passing the variable mode with the value &quot;1&quot;.</p>
<p>An alternative, and perhaps more useful, way is to use HTML forms. The main thing to remember with forms is that you need to use the GET method to send information via the query string, for example:</p>
<pre>&lt;form method="GET" action="page.php"&gt;
Please enter your name: &lt;input type="text" name="username" /&gt;
&lt;input type="submit" value="submit" /&gt;
&lt;/form&gt;</pre>
<p>This code displays a simple HTML form with a text box and a submit button. When the user enters their name and presses submit, the information in the name box is passed to a page called &quot;page.php&quot; (specified in the form's action attribute) via the query string. So, if I entered &quot;mooseh&quot; and pressed submit, the form will call page.php?username=mooseh. Remember that &quot;username&quot; was what we entered as the text input's name attribute.</p>
<h3>Accessing a query string element in a PHP page</h3>
<p>In PHP, all the information passed via the query string is held in the $_GET super global array (prior to PHP version 4.1.0 it was called $HTTP_GET_VARS, but that's now depreciated). To access an item, type $_GET['varName'], where varName is the name of the variable in the query string.<br />
To demonstrate, let's create page.php to process the information from the form above:</p>
<pre>&lt;?
if($_GET['username'] == "")
{
    // no username entered
    echo "You did not enter a name.";
}
else
{
    echo "Hello, " . $_GET[’username’];
}
?&gt;</pre>
<p>So now, me typing in mooseh and pressing submit will generate the reply &quot;Hello, mooseh&quot;.</p>
<h3>Listing the contents of $_GET</h3>
<p>Should you wish to find out everything that's in the query string, you can do so with the following code which displays the variables next to their values in a table:</p>
<pre>&lt;table border="1"&gt;
&lt;tr&gt;&lt;th&gt;variable&lt;/th&gt; &lt;th&gt;value&lt;/th&gt;&lt;/tr&gt;
&lt;?
foreach($_GET as $variable =&gt; $value)
{
    echo "&lt;tr&gt;&lt;td&gt;" . $variable . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $value . "&lt;/td&gt;";
}
?&gt;
&lt;/table&gt;</pre>
<h3>Conclusion</h3>
<p>This article showed you how to pass information between pages with the query string, and a use which allows you to add some increased interactivity. There are a lot of other uses, for example selecting which section of a site to display based on a variable in the query string, and processing form data.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.tiposaurus.co.uk/2012/04/19/how-to-use-the-query-string-in-php/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>How to Find the Current URL with PHP</title>
		<link>http://www.tiposaurus.co.uk/2012/04/19/how-to-find-the-current-url-with-php/</link>
		<comments>http://www.tiposaurus.co.uk/2012/04/19/how-to-find-the-current-url-with-php/#comments</comments>
		<pubDate>Thu, 19 Apr 2012 23:00:55 +0000</pubDate>
		<dc:creator>Steven</dc:creator>
				<category><![CDATA[Basics]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://discomoose.org/2005/11/02/how-to-find-the-current-url-with-php/</guid>
		<description><![CDATA[The full URL to a page comes in three parts: The domain name, the path to the file then the filename, and the query string. For example, take the URL http://www.example.com/example/page.php?name=Bob. The three parts of this are: 1. The domain name: www.example.com 2. The path to the page: /example/page.php 3. The query string: name=Bob So [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start -->
<p>The full URL to a page comes in three parts: The domain name, the path to the file then the filename, and the query string. For example, take the URL http://www.example.com/example/page.php?name=Bob. The three parts of this are:</p>
<p>1. The domain name: www.example.com<br />
2. The path to the page: /example/page.php<br />
3. The query string: name=Bob</p>
<p>So how do you find it all out with your own PHP scripts?</p>
<p><span id="more-31"></span></p>
<p>All of the information we need is stored in the $_SERVER array, which is accessible from anywhere in your PHP script (and as such is called a superglobal variable), it works like a normal array and the keys we wish to retrieve the values of are 'HTTP_HOST', 'SCRIPT_NAME' and 'QUERY_STRING' for the three different parts of the url. Alternatively, if we don't need to have the path to the page and the query string seperate, we can use 'REQUEST_URI'.</p>
<p>The following code should let you find it:</p>
<pre>&lt;?php
// find out the domain:
$domain = $_SERVER['HTTP_HOST'];
// find out the path to the current file:
$path = $_SERVER['SCRIPT_NAME'];
// find out the QueryString:
$queryString = $_SERVER['QUERY_STRING'];
// put it all together:
$url = "http://" . $domain . $path . "?" . $queryString;
echo "The current URL is: " . $url . "
";

// An alternative way is to use REQUEST_URI instead of both
// SCRIPT_NAME and QUERY_STRING, if you don't need them seperate:
$url2 = "http://" . $domain . $_SERVER['REQUEST_URI'];
echo "The alternative way: " . $url2;
?&gt;</pre>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.tiposaurus.co.uk/2012/04/19/how-to-find-the-current-url-with-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Find a visitor&#039;s IP Address with PHP</title>
		<link>http://www.tiposaurus.co.uk/2012/04/18/find-a-visitors-ip-address-with-php/</link>
		<comments>http://www.tiposaurus.co.uk/2012/04/18/find-a-visitors-ip-address-with-php/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 23:00:25 +0000</pubDate>
		<dc:creator>Steven</dc:creator>
				<category><![CDATA[Basics]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[ip address]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://discomoose.org/2005/11/01/find-a-visitors-ip-address-with-php/</guid>
		<description><![CDATA[Do you want to know the IP address of a visitor? This can be useful for many reasons, such as tracking site usage or blocking access to specific people. Here's how you can find it using PHP. In a PHP page, within the PHP tags, &#60;?php ... ?&#62;, you can retrieve the IP address of [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start -->
<p>Do you want to know the IP address of a visitor? This can be useful for many reasons, such as tracking site usage or blocking access to specific people. Here's how you can find it using PHP.</p>
<p>In a PHP page, within the PHP tags, &lt;?php ... ?&gt;, you can retrieve the IP address of a user through the server variables array, which is contains information about the user and the server environment and is accessible from anywhere in your PHP script. To do this, you use the code $_SERVER['REMOTE_ADDR']. It works like a normal array, where REMOTE_ADDR is the key, and the IP address of the visitor is the value associated with that key.</p>
<h3>Display The IP Address</h3>
<p>So, if you want to display the IP Address to the user then the following page will suffice:</p>
<pre>&lt;?php
echo "Hello! Your IP Address is: " . $_SERVER['REMOTE_ADDR'];
?&gt;</pre>
<h3>Exploring the $_SERVER array</h3>
<p>You might be wondering what other information you can get from $_SERVER, well you can find out with the following script which displays all the variables in it along with their values if set, in a HTML table:</p>
<pre>&lt;table border="1"&gt;
&lt;tr&gt;&lt;th&gt;Variable&lt;/th&gt;&lt;th&gt;Value&lt;/th&gt;&lt;/tr&gt;
&lt;?
foreach( $_SERVER as $key =&gt; $value )
{
echo "&lt;tr&gt;&lt;td&gt;" . $key . "&lt;/td&gt;";
echo "&lt;td&gt;" . $value . "&lt;/td&gt;&lt;/tr&gt;";
}
?&gt;
&lt;/table&gt;</pre>
<p>Finally, the following code works harder to find the true IP of the user by checking for proxies:</p>
<pre>function userIP()
{
//This returns the True IP of the client calling the requested page
// Checks to see if HTTP_X_FORWARDED_FOR
// has a value then the client is operating via a proxy
$userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
if($userIP == "")
{
$userIP = $_SERVER['REMOTE_ADDR'];
}
// return the IP we've figured out:
return $userIP;
}</pre>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.tiposaurus.co.uk/2012/04/18/find-a-visitors-ip-address-with-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Write Text on to an existing PNG image using PHP</title>
		<link>http://www.tiposaurus.co.uk/2012/04/18/write-text-on-to-an-existing-png-image-using-php/</link>
		<comments>http://www.tiposaurus.co.uk/2012/04/18/write-text-on-to-an-existing-png-image-using-php/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 07:21:27 +0000</pubDate>
		<dc:creator>Steven</dc:creator>
				<category><![CDATA[Image Handling]]></category>
		<category><![CDATA[image handling]]></category>
		<category><![CDATA[image manipulation]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development. png]]></category>

		<guid isPermaLink="false">http://discomoose.org/2005/10/19/write-text-on-to-an-existing-png-image-using-php/</guid>
		<description><![CDATA[This article will tell you how to write text on an existing PNG image using PHP using PHP's image manipulation functions. The image functions require the GD library to be installed. This article is fairly basic, however some more detail on the basic functions used is included in the article, "write text on a dynamically [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start -->
<p>This article will tell you how to write text on an existing PNG image using PHP using PHP's image manipulation functions. The image functions require the GD library to be installed. This article is fairly basic, however some more detail on the basic functions used is included in the article, "write text on a dynamically generated image in PHP"</p>
<p>First, we need the source image. In this example, we're using an image called image.png (with dimension 150x150, though this code will work with any size of image) which is shown below:<br />
<a href="http://www.tiposaurus.co.uk/2012/04/write-text-on-to-an-existing-png-image-using-php/image/" rel="attachment wp-att-293"><img class="alignnone size-full wp-image-293" title="image" src="http://www.tiposaurus.co.uk/wp-content/uploads/2005/10/image.png" alt="source image" width="150" height="150" /></a><br />
This image is going to be kept in the same directory on the webserver as the php file we're using to manipulate it.</p>
<p>Now it's time for PHP code to load the image. We make use of the function imagecreatefrompng() to get the image into PHP to manipulate. This takes the path to the image as a parameter. If the image can't be loaded then we'll tell it to die() and stop interpreting the page, however there are better ways of dealing with this error.</p>
<pre>$im = imagecreatefrompng("image.png");
// if there's an error, stop processing the page:
if(!$im)
{
die("");
}</pre>
<p>Next, we'll define some colours for use in the image using the imagecolorallocate() function:</p>
<pre>$red_background = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);</pre>
<p>Now we get the dimensions of the image, using the imagesx() and imagesy(), which take the image resource as a parameter and return the width and height respectively:</p>
<pre>// get the width and the height of the image
$width = imagesx($im);
$height = imagesy($im);</pre>
<p>The next step is to draw a black rectangle on to the image to ensure that the text we write is visible. This step is optional, as you could write the text directly on to the image.<br />
The coordinate system used in PHP is that the top left of an image is (0,0) and the bottom right is ($width, $height). We're going to draw the rectangle across the bottom 20px of the image, so we pass imagefilledrectangle two sets of coordinates: (0, $height-20) and ($width, $height). The coordinates we're passing - as the 2nd to 4th parameters) tell the method to draw a rectangle from a point on the left hand side, 20 pixels from the bottom, to the very bottom right of the image. The rectangle is then automatically filled black.</p>
<pre>// draw a black rectangle across the bottom, say, 20 pixels of the image:
imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);</pre>
<p>Now it's time to write the text on the image. In this example, we will write the text in the middle of the rectangle we just created. First we declare two variables, $font and $text, to store the int representing the system font we're using and the text we're going to write to the image, respectively.</p>
<pre>$font = 4; // store the int ID of the system font we're using in $font
$text = "tiposaurus"; // store the text we're going to write in $text</pre>
<p>Then we will calculate where the left edge of the text is. This is done by using imagefontwidth() to get the width of the system font we're using (which is fixed width, so all characters have the same width), then multiplying that by the number of characters in the string we're writing. This finds the length of the text in pixels. We then subtract that from the total width of the image and divide by two to find the left position, which we store in the variable $leftTextPos:</p>
<pre>// calculate the left position of the text:
$leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2;</pre>
<p>Now we write the text to the image, using the imagestring() function passing the variables we created earlier as parameters (NB. As height we're using the image height minus 18 pixels, to estimate a decent fit in the box, rather than calculating the height in the same way as width.)</p>
<pre>// finally, write the string:
imagestring($im, $font, $leftTextPos, $height-18, $text, $yellow);</pre>
<p>Lastly, we send the image to the browser then clear it from memory:</p>
<pre>// output the image
// tell the browser what we're sending it
Header('Content-type: image/png');
// output the image as a png
imagepng($im);</pre>
<p>That's it! the output image is:<br />
<a href="http://www.tiposaurus.co.uk/2012/04/write-text-on-to-an-existing-png-image-using-php/image-result/" rel="attachment wp-att-294"><img class="alignnone size-full wp-image-294" title="image result" src="http://www.tiposaurus.co.uk/wp-content/uploads/2005/10/image-result.png" alt="image with tiposaurus written on it" width="150" height="150" /></a><br />
This will be shown in the browser whenever anyone accesses the php page we've just created!</p>
<p>To finish off, here's the code in full:</p>
<pre>&lt;?
// load the image from the file specified:
$im = imagecreatefrompng("image.png");
// if there's an error, stop processing the page:
if(!$im)
{
die("");
}

// define some colours to use with the image
$yellow = imagecolorallocate($im, 255, 255, 0);
$black = imagecolorallocate($im, 0, 0, 0);

// get the width and the height of the image
$width = imagesx($im);
$height = imagesy($im);

// draw a black rectangle across the bottom, say, 20 pixels of the image:
imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);

// now we want to write in the centre of the rectangle:
$font = 4; // store the int ID of the system font we're using in $font
$text = "tiposaurus"; // store the text we're going to write in $text
// calculate the left position of the text:
$leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2;
// finally, write the string:
imagestring($im, $font, $leftTextPos, $height-18, $text, $yellow);

// output the image
// tell the browser what we're sending it
Header('Content-type: image/png');
// output the image as a png
imagepng($im);

// tidy up
imagedestroy($im);
?&gt;</pre>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.tiposaurus.co.uk/2012/04/18/write-text-on-to-an-existing-png-image-using-php/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Rotating Images with PHP</title>
		<link>http://www.tiposaurus.co.uk/2012/04/17/rotating-pictures-with-php/</link>
		<comments>http://www.tiposaurus.co.uk/2012/04/17/rotating-pictures-with-php/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 09:12:01 +0000</pubDate>
		<dc:creator>Tiposaurus</dc:creator>
				<category><![CDATA[Image Handling]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[image handling]]></category>
		<category><![CDATA[image manipulation]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[png]]></category>

		<guid isPermaLink="false">http://discomoose.org/2006/04/28/rotating-pictures-with-php/</guid>
		<description><![CDATA[This tutorial will show you how to rotate an image in your PHP scripts. You'll need to have the GD library installed to be able to use some of the functions we use in this guide, though this is included with most PHP installations so there's a chance you'll have it. Image rotation in PHP [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start -->
<p>This tutorial will show you how to rotate an image in your PHP scripts. You'll need to have the GD library installed to be able to use some of the functions we use in this guide, though this is included with most PHP installations so there's a chance you'll have it.</p>
<p>Image rotation in PHP is handled by a function called <code>imagerotate()</code>, which takes three parameters (and optionally a fourth, which deals with transparency and is beyond the scope of this guide). The first is the resource identifier which you obtain when you load the original image within your script, the second is the angle which you want to rotate the image and the third parameter is used when the image isn't going to be rectangular, and specifies the colour with which to fill the uncovered areas of the rotated image to make it into a rectangle (since all image formats require rectangular images).</p>
<p><span id="more-16"></span></p>
<p>The first thing we need to do is to load the original image into the script, we do this with a command such as imagecreatefromjpeg("file.jpg") for JPEG images, or imagecreatefrompng("file.png") for PNG images.</p>
<pre>// filename of the original image
$fileName = "cow.jpg";
// load the original image from the file
$original = imagecreatefromjpeg($fileName);</pre>
<p>In this example, we're using an image called cow.jpg. The method imagecreatefromjpeg() returns a resource identifier which represents that image within the script - we store this in the variable $original. We need to pass this as the first parameter of the imagerotate() method.</p>
<p>The original image we're using is this particularly nice cow:<br />
<a href="http://www.tiposaurus.co.uk/2012/04/rotating-pictures-with-php/cow/" rel="attachment wp-att-263"><img class="alignnone size-full wp-image-263" title="cow" src="http://www.tiposaurus.co.uk/wp-content/uploads/2012/04/cow.jpg" alt="picture of a cow" width="252" height="196" /></a></p>
<p>Now it's time to rotate the image! First we have to decide how many degrees to rotate the image <em>anti-clockwise</em>. Firstly, we'll just roate the image 90 degrees - this means that the resulting image will be rectangular and we won't have to worry about the third parameter.</p>
<pre>// degrees to rotate the image (counter clockwise)
$angle = 90.0;
// rotate the image by $angle degrees
$rotated = imagerotate($original, $angle, 0);</pre>
<p>We pass the original image identifer - $original - and the angle to rotate by - $angle - into the imagerotate() function. In this special case the third parameter isn't important so we'll just enter 0 (which represents the colour black). This returns an identifier for the new rotated image, which we store in the variable $rotated.</p>
<p>Now it's time to display the image. We first tell the browser the content type (IE what sort of image we're going to display) then use the appropriate command to output the data of the image. We're dealing with JPEG images in this example, so the Content-type is "image/jpeg" and the method to output the image is imagejpeg().</p>
<pre>// print the appropriate header type
// this tells the browser we're displaying a jpeg image
header('Content-type: image/jpeg');
// use the imagejpeg() method to display the rotated image
imagejpeg($rotated);</pre>
<p>That code tells the browser we're about to display a jpeg image, then we call imagejpeg($rotated) which turns the image identified by $rotated into the a JPEG image for the browser to display. The output of this code is a sideways cow:<br />
<a href="http://www.tiposaurus.co.uk/2012/04/rotating-pictures-with-php/cow-90/" rel="attachment wp-att-262"><img class="alignnone size-full wp-image-262" title="cow.90" src="http://www.tiposaurus.co.uk/wp-content/uploads/2012/04/cow.90.jpg" alt="Sideways picture of a cow" width="196" height="252" /></a></p>
<p>Now let's consider the case where you want to rotate an image by an angle such as 45 degrees which requires the imagerotate() method to fill around the rotated image to produce a rectangle. We need to pass an integer as the third parameter to tell the method which colour to fill around the image with. We will do this by specifying the hexidecimal representation of the colour, as is used in HTML. For example, FF0000 is red (each digit is between 0-F; the first two digits represent the red component, the next two are green and the last two are blue) - in HTML we would use #FF0000. To tell PHP that we're using hex to represent the numbers rather than decimal, we must prefix this number by <strong>0x</strong> (zero x).</p>
<pre>// degrees to rotate the image (counter clockwise)
$angle = 45.0;
// if the resulting image is not rectangular..
// .. what colour will the uncovered bits be?
$bgColour = 0xFF0000; // red
// rotate the image by $angle degrees
$rotated = imagerotate($original, $angle, $bgColour);</pre>
<p>The imagerotate() method works as with a 90 degree rotation except fills the outsides with the colour red. The resulting output is:<br />
<a href="http://www.tiposaurus.co.uk/2012/04/rotating-pictures-with-php/cow-45/" rel="attachment wp-att-261"><img class="alignnone size-full wp-image-261" title="cow.45" src="http://www.tiposaurus.co.uk/wp-content/uploads/2012/04/cow.45.jpg" alt="Picture of a cow rotated 45 degrees" width="317" height="317" /></a></p>
<p>Other hexidecimal colour codes are:</p>
<ul>
<li>0xFFFFFF - white</li>
<li>0x00FF00 - green</li>
<li>0x000000 - black</li>
</ul>
<p>That's it! This tutorial should have helped you to rotate images in PHP.</p>
<p>The full PHP code for the 45 degree example is below:</p>
<pre> // filename of the original image
$fileName = "cow.jpg";

// degrees to rotate the image (counter clockwise)
$angle = 45.0;

// if the resulting image is not rectangular..
// .. what colour will the uncovered bits be?
$bgColour = 0xFFFFFF; // red

// load the original image from the file
$original = imagecreatefromjpeg($fileName);

// rotate the image by $angle degrees
$rotated = imagerotate($original, $angle, $bgColour);

// print the appropriate header type
// this tells the browser we're displaying a jpeg image
header('Content-type: image/jpeg');
// use the imagejpeg() method to display the rotated image
imagejpeg($rotated);
?&gt;</pre>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.tiposaurus.co.uk/2012/04/17/rotating-pictures-with-php/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Write text on a dynamically generated image using PHP</title>
		<link>http://www.tiposaurus.co.uk/2012/04/17/write-text-on-a-dynamically-generated-image-using-php/</link>
		<comments>http://www.tiposaurus.co.uk/2012/04/17/write-text-on-a-dynamically-generated-image-using-php/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 07:29:11 +0000</pubDate>
		<dc:creator>Steven</dc:creator>
				<category><![CDATA[Image Handling]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[image handling]]></category>
		<category><![CDATA[image manipulation]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://discomoose.org/2005/10/19/write-text-on-a-dynamically-generated-image-using-php/</guid>
		<description><![CDATA[This tutorial will tell you how to write text to a blank dynamically generated PNG image in PHP. This is fairly simple, so it will just be the first step in a series of tutorials dealing with images in PHP. The image functions require the GD library to be installed. The code we're using and [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start -->
<p>This tutorial will tell you how to write text to a blank dynamically generated PNG image in PHP. This is fairly simple, so it will just be the first step in a series of tutorials dealing with images in PHP. The image functions require the GD library to be installed.</p>
<p><span id="more-24"></span></p>
<p>The code we're using and the output is below, and below that is an explanation of what the code does.</p>
<pre> // create an image with width 100px, height 20px
$image = imagecreate(100, 20);

// create a red colour for the background
// as it is the first call to imagecolorallocate(), this fills the background automatically
$red_background = imagecolorallocate($image, 255, 0, 0);
// create a black colour for writing on the image
$black = imagecolorallocate($image, 0, 0, 0);

// write the string "tiposaurus" on the image
// the top left of the text is at the position (10, 2)
imagestring($image, 4, 10, 2, 'tiposaurus', $black);

// output the image
// tell the browser what we’re sending it
Header('Content-type: image/png');
// output the image as a png
imagepng($image);

// tidy up
imagedestroy($image);
?&gt;</pre>
<p>Output:<br />
<a href="http://www.tiposaurus.co.uk/2012/04/write-text-on-a-dynamically-generated-image-using-php/text/" rel="attachment wp-att-275"><img class="alignnone size-full wp-image-275" title="text" src="http://www.tiposaurus.co.uk/wp-content/uploads/2005/10/text.png" alt="tiposaurus" width="100" height="20" /></a></p>
<p>The first step is to tell PHP to create the image, we do this with the <code>imagecreate()</code> function, which takes the desired width and height as parameters. This returns the image identifier, which we store in the variable $image.</p>
<p>Next, we use the <code>imagecolorallocate()</code> function to define some colours for use in our image. This takes as parameters, the image identifier we're using, then the red, green and blue values for the colour. The first time we call <code>imagecolorallocate()</code> it fills in the background of the image with that colour.</p>
<p>We then use the <code>imagestring()</code> function to write text on the image. This function takes six parameters: the image identifier, an integer 1-5 determining which built in font to use (we're choosing 4), x and y coordinates giving the top left position of the text, the text to write, and the colour to write it in.</p>
<p>It's then time to output the image.<br />
First, we need to tell the browser what type of image we're sending (this is normally text, which wouldn't work for images), we do this with the line:<br />
<code>Header('Content-type: image/png');</code></p>
<p>Next, we need to tell PHP to output the image as a PNG, this is done with the <code>imagepng()</code> function, which takes the image identifier as a parameter.</p>
<p>That's it! The last thing we do is to use the <code>imagedestroy()</code> function to delete the image from memory, it takes the image identifier as a parameter.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.tiposaurus.co.uk/2012/04/17/write-text-on-a-dynamically-generated-image-using-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Generate a Month Calendar In PHP</title>
		<link>http://www.tiposaurus.co.uk/2012/04/16/generate-a-month-calendar-in-php/</link>
		<comments>http://www.tiposaurus.co.uk/2012/04/16/generate-a-month-calendar-in-php/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 07:10:08 +0000</pubDate>
		<dc:creator>Steven</dc:creator>
				<category><![CDATA[Basics]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[calendar]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://discomoose.org/2005/10/19/generate-a-month-calendar-in-php/</guid>
		<description><![CDATA[This article shows you how to display a calendar for a given month, mainly using PHP's date() function. The date() function The date() function displays the date in your desired format, at a given time. It works by accepting one or two parameters, the first is a string with the format, the letters in the [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start -->
<p>This article shows you how to display a calendar for a given month, mainly using PHP's date() function.</p>
<h3>The date() function</h3>
<p>The date() function displays the date in your desired format, at a given time. It works by accepting one or two parameters, the first is a string with the format, the letters in the string tell the function how to format the date. You can see what formatting options are available by seeing the <a href="http://uk.php.net/date">PHP Manual entry for date()</a>. If you just pass it a string to format the date then it uses the current time, however if you pass a timestamp (a standard way of representing time: the number of seconds since a set time in the 1970s), then it will output that date with the given format.</p>
<p><span id="more-22"></span></p>
<p>What we're going to code is a function to which you can pass two integers, representing the year and the month, and have it display the calendar for that month. We'll call that function showMonth() and the parameters $month and $year.</p>
<p>To keep this tutorial clear we won't bother validating $month and $year, but it would be a good idea to check that they were valid if you were to use a calendar in your program.<br />
So, the first code we'll write is an empty function definition:</p>
<pre>function showMonth($month, $year)
{
}</pre>
<p>Now it's time that we actually do some work! We'll start by figuring out a timestamp that corresponds to sometime in that month. It's not too important that we're percise here, so we will use the mktime() function, and get the time at 12:00:00 on the 1st of the month. We can then use the date() function to tell us the number of days in that month by passing the format string "t" along with our timestamp $date. Next, we can use date() to tell us what day of the week the 1st of the month is (remembering that we've set $date to a time on the 1st of the month) by passing the "w" format string to it which returns an integer value of 0 to 6 (0 is Sunday, etc) - we'll store this in $offset - with the timestamp in $date. Lastly, we'll declare a variable $rows, which keeps count of the number of rows in the calendar, and initialise it to 1. The code for this is below:</p>
<pre>$date = mktime(12, 0, 0, $month, 1, $year);
$daysInMonth = date("t", $date);
// calculate the position of the first day in the calendar (sunday = 1st column, etc)
$offset = date("w", $date);
$rows = 1;</pre>
<p>Next, we'll display some HTML which will begin the display of the calendar. There's not much PHP in this: just something to display the month as text followed by the year (eg July 2005) in a heading. What the HTML does is to display a heading, then start the table code to display the calendar, here we display the first row which contains column headings for each week day, Sunday to Saturday.</p>
<pre>echo "&lt;h1&gt;Displaying calendar for " . date("F Y", $date) . "&lt;/h1&gt;\n";
echo "&lt;table border=\"1\"&gt;\n";
echo "\t&lt;tr&gt;&lt;th&gt;Su&lt;/th&gt;&lt;th&gt;M&lt;/th&gt;&lt;th&gt;Tu&lt;/th&gt;&lt;th&gt;W&lt;/th&gt;&lt;th&gt;Th&lt;/th&gt;&lt;th&gt;F&lt;/th&gt;&lt;th&gt;Sa&lt;/th&gt;&lt;/tr&gt;";
echo "\n\t&lt;tr&gt;";</pre>
<p>Note that the last line in the above code block starts the first row of date entries in the calendar, also \n and \t represent a new line and tab respectively - we're using these so that if you view the source code of the finished page, it is much more readable.</p>
<p>Note that the last line in the above code block starts the first row of date entries in the calendar, also \n and \t represent a new line and tab respectively - we're using these so that if you view the source code of the finished page, it is much more readable.</p>
<p>Now we can begin filling the cells in the table. There are going to be 3 stages to this: first is filling in blank cells before the start of the month, then filling in the numbers 1, 2.. until the end of the month and lastly we have to pad the from the end of the month until the end of the last row (if you don't understand then look at a calendar: there are probably blank spaces before the first day and after the last day of the month).</p>
<h3>Padding at the start</h3>
<p>Earlier, we found an integer representing what day of the week the start of the month was, and stored the result in the variable $offset. This value represents the number of columns into the table that we have to skip before entering the 1st of the month. For example, if the first is a Sunday then we don't need to skip any columns and $offset is 0. To skip these columns, we'll just add a blank cell in the table to this row using a for loop.</p>
<pre>for($i = 1; $i &lt;= $offset; $i++)
{
echo "&lt;td&gt;&lt;/td&gt;";
}</pre>
<h3>Days in the month</h3>
<p>Now we need to put the values 1, 2, 3,... $daysInMonth (which we found earlier) into the right places in the calendar. We do this with another for loop which starts at 1 and increments/loops until the end of the month. The only tricky bit here is knowing when to start a new row. We find that out by adding $offset to the current value of $day, subtracting one then taking the modulus with 7. This means that when we're on a value which will lie in the 8th row (eg when $offset = 0 and $day = 8, or, $offset = 4 and $day = 4) then we'll start a new row before displaying that value. The only thing we need to check is that it's not the first of the month, because we've already started that row of the table. Each time we start a new row, we increment the variable $rows to keep track of how many rows are in the table. Then for each day in the month, we write a cell in the table containing the value of $day.</p>
<pre>for($day = 1; $day &lt;= $daysInMonth; $day++)
{
if( ($day + $offset - 1) % 7 == 0 &amp;&amp; $day != 1)
{
echo "&lt;/tr&gt;\n\t&lt;tr&gt;";
$rows++;
}

echo "&lt;td&gt;" . $day . "&lt;/td&gt;";
}</pre>
<h3>Padding at the end</h3>
<p>Now we have to add empty cells at the end of the table. We have so far displayed ($day + $offset) cells, with ($rows * 7) to go, so we just use a while loop to add empty cells until we reach the end of the last row, where ($day + $offset) is equal to (7* $rows):</p>
<pre>while( ($day + $offset) &lt;= $rows * 7)
{
echo "&lt;td&gt;&lt;/td&gt;";
$day++;
}</pre>
<p>Now all that's left is to finish off the HTML code for the table, closing the last row and then the table tags:</p>
<pre>echo "&lt;/tr&gt;\n";
echo "&lt;/table&gt;\n";</pre>
<p>That's the function complete. The full source code for it is shown below:</p>
<pre>&lt;?
function showMonth($month, $year)
{
$date = mktime(12, 0, 0, $month, 1, $year);
$daysInMonth = date("t", $date);
// calculate the position of the first day in the calendar (sunday = 1st column, etc)
$offset = date("w", $date);
$rows = 1;

echo "&lt;h1&gt;Displaying calendar for " . date("F Y", $date) . "&lt;/h1&gt;\n";

echo "&lt;table border=\"1\"&gt;\n";
echo "\t&lt;tr&gt;&lt;th&gt;Su&lt;/th&gt;&lt;th&gt;M&lt;/th&gt;&lt;th&gt;Tu&lt;/th&gt;&lt;th&gt;W&lt;/th&gt;&lt;th&gt;Th&lt;/th&gt;&lt;th&gt;F&lt;/th&gt;&lt;th&gt;Sa&lt;/th&gt;&lt;/tr&gt;";
echo "\n\t&lt;tr&gt;";

for($i = 1; $i &lt;= $offset; $i++)
{
echo "&lt;td&gt;&lt;/td&gt;";
}
for($day = 1; $day &lt;= $daysInMonth; $day++)
{
if( ($day + $offset - 1) % 7 == 0 &amp;&amp; $day != 1)
{
echo "&lt;/tr&gt;\n\t&lt;tr&gt;";
$rows++;
}

echo "&lt;td&gt;" . $day . "&lt;/td&gt;";
}
while( ($day + $offset) &lt;= $rows * 7)
{
echo "&lt;td&gt;&lt;/td&gt;";
$day++;
}
echo "&lt;/tr&gt;\n";
echo "&lt;/table&gt;\n";
}
?&gt;</pre>
<h3>Displaying the calendar in a page</h3>
<p>To display it in a HTML page, you just need to include a call to the showMonth() function, passing the desired month and year as paramters.<br />
For example:</p>
<pre>&lt;html&gt;
&lt;head&gt;&lt;title&gt;Calendar&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;? showMonth(7, 2005); // July 2005
showMonth(1, 1980); // January 1980
showMonth(12, 2012); // December 2012 ?&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>The output of this code is shown below:<br />
<img src="http://img246.imageshack.us/img246/9369/caloutput5pl.png" alt="Output of the above code" /></p>
<p>That's it! Thanks for following this tutorial through, hopefully you'll have found it useful! <img src='http://www.tiposaurus.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><em>NB. This was originally published in October 2005 on another of my websites.</em></p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.tiposaurus.co.uk/2012/04/16/generate-a-month-calendar-in-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Generating Random Numbers in PHP</title>
		<link>http://www.tiposaurus.co.uk/2011/06/10/random-numbers-in-php/</link>
		<comments>http://www.tiposaurus.co.uk/2011/06/10/random-numbers-in-php/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 09:56:59 +0000</pubDate>
		<dc:creator>Steven</dc:creator>
				<category><![CDATA[Basics]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[random numbers]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://discomoose.org/2006/04/27/random-numbers-in-php/</guid>
		<description><![CDATA[To create random numbers in PHP, you can use the rand() function call, which takes either zero or, optionally, two parameters determining the minimum and maximum random numbers (inclusive) that you'd like. If you don't specify a maximum then it will default to a value, RAND_MAX, set in the PHP configuration. The function returns an [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start -->
<p>To create random numbers in PHP, you can use the <em>rand()</em> function call, which takes either zero or, optionally, two parameters determining the minimum and maximum random numbers (inclusive) that you'd like. If you don't specify a maximum then it will default to a value, <em>RAND_MAX</em>, set in the PHP configuration. The function returns an integer, generated "randomly." (Computers can't generate real random numbers, but this tries and is suitable for most purposes.)</p>
<p>You can get the value of RAND_MAX with the function getrandmax(). Then if you desire a random number larger than that, you have to specify the min and max bounds as parameters, eg <em>rand(0, 100000)</em> will return a random number between 0 and 100000 inclusive - so both 0 and 100000 can be returned.</p>
<p>For example:</p>
<pre>&lt;!--?php // random number between 0 and RAND_MAX:
echo rand(); // output: 28688 (for example)
// random number between 37 and 50:
echo rand(37, 50); // output: 44
// find out the value of RAND_MAX:
echo getrandmax(); // output: 32767
// get a larger random number by specifying min and max:
echo rand(0, 1000000); // output: 351990
?&gt;</pre>
<p>The output shown in the comments above is as an example. Since it is generating random numbers, the numbers generated are very likely to be be different every time.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.tiposaurus.co.uk/2011/06/10/random-numbers-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How To: Read a file&#039;s contents with PHP</title>
		<link>http://www.tiposaurus.co.uk/2011/04/05/reading-a-files-contents-with-php/</link>
		<comments>http://www.tiposaurus.co.uk/2011/04/05/reading-a-files-contents-with-php/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 09:04:06 +0000</pubDate>
		<dc:creator>Tiposaurus</dc:creator>
				<category><![CDATA[Files]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://discomoose.org/2006/04/28/reading-a-files-contents-with-php/</guid>
		<description><![CDATA[PHP provides three built-in functions which allow you to easily read the contents of a file on your webserver. This is useful when, for example, another program may write information to the file and you could access that information through your script. In this example, we're going to use the following example 5 line file, [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start -->
<p>PHP provides three built-in functions which allow you to easily read the contents of a file on your webserver. This is useful when, for example, another program may write information to the file and you could access that information through your script.</p>
<p>In this example, we're going to use the following example 5 line file, saved as file.txt:</p>
<pre>line 1
line 2
line 3
line 4
line 5</pre>
<p>In the same directory as file.txt, we're going to work with the PHP file test.php. We'll outline the file reading functions below.</p>
<h3>readfile()</h3>
<p>readfile() is the least useful of the file functions. As a parameter, it takes a string which specifies the location of the file to read - this can be relative or absolute; since our file is the same directory as the script, we just need to pass the string 'file.txt' as this paramter. We call the function using the code <code>readfile('file.txt')</code> and it prints out the contents of the file straight to the browser:</p>
<pre>
// readfile() writes the contents of the file straight to the browser
echo 'Contents of file.txt using readfile():&lt;br&gt;';
readfile('file.txt');
</pre>
<p>The disadvantage of readfile() is that it doesn't allow you to manipulate the file contents before displaying it - the next two functions we're going to look at will allow you to do that.</p>
<h3>file()</h3>
<p>The code <code>file('file.txt')</code> will read the contents of file.txt into an array, which you can then manipulate in your scripts and display yourself. Each line of the file is stored in a seperate element of the array, this means that we can access and manipulate each line of the file seperately using normal array notation.</p>
<pre>
// file() loads the contents of file.txt into an array, $lines
// each line in the file becomes a seperate element of the array.
$lines = file('file.txt');

// now loop through the array to print the contents of the file
echo 'Contents of file.txt using file():&lt;br&gt;';
foreach ($lines as $line)
{
echo htmlspecialchars($line) . '&lt;br&gt;';
}

// we can also access each line of the file seperately
echo '3rd line of the file: "' . htmlspecialchars($lines[2]) . '"&lt;br&gt;';
</pre>
<p>In this example, we've loaded the contents of file.txt into an array called <code>$lines</code> then used foreach to loop through the array and display each line on the user's browser. We then use <code>$lines[2]</code> to access the third line of the file (the element at array index 2, since line 1 is <code>$lines[0]</code>).</p>
<p>You may notice that we've used a function called htmlspecialchars() when displaying the file's contents - this enables that special characters used in HTML, such as &gt; and " are displayed correctly. This illustrates the power of file() over readfile() since readfile() was unable to perform this kind of processing.</p>
<h3>get_file_contents()</h3>
<p>The last method we will look at is get_file_contents('file.txt') which is similar to file() however rather than returning an array, it returns a string with all the lines of the file. We can manipulate and display this in a similar way as with file():</p>
<pre>
// file_get_contents() reads the file and places the contents in a string
$fileString = file_get_contents('file.txt');
echo 'Contents of file.txt using file_get_contents():&lt;br&gt;';
echo nl2br( htmlspecialchars($fileString) );
</pre>
<p>Since we have no way of seperating the lines with this method, we've used the nl2br() function which converts line breaks in the string (represented by the special character <code>n</code>) into the HTML line break &lt;br /&gt; so that the file will display correctly on the visitor's browser.</p>
<p>The full contents of test.php is below:</p>
<pre>
&lt;?php
// file() loads the contents of file.txt into an array, $lines
// each line in the file becomes a seperate element of the array.
$lines = file('file.txt');

// now loop through the array to print the contents of the file
echo 'Contents of file.txt using file():
';
foreach ($lines as $line)
{
echo htmlspecialchars($line) . '&lt;br&gt;';
}

// we can also access each line of the file seperately
echo '3rd line of the file: "' . htmlspecialchars($lines[2]) . '"&lt;br&gt;';
echo '&lt;br&gt;';

// file_get_contents() reads the file and places the contents in a string
$fileString = file_get_contents('file.txt');
echo 'Contents of file.txt using file_get_contents():
';
echo nl2br( htmlspecialchars($fileString) );
echo '&lt;br&gt;';

// readfile() writes the contents of the file straight to the browser
echo 'Contents of file.txt using readfile():&lt;br&gt;';
readfile('file.txt');
?&gt;
</pre>
<p>The output from test.php is:</p>
<pre>Contents of file.txt using file():
line 1
line 2
line 3
line 4
line 5
3rd line of the file: "line 3 "

Contents of file.txt using file_get_contents():
line 1
line 2
line 3
line 4
line 5

Contents of file.txt using readfile():
line 1 line 2 line 3 line 4 line 5</pre>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.tiposaurus.co.uk/2011/04/05/reading-a-files-contents-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How To: Find Items in PHP Arrays</title>
		<link>http://www.tiposaurus.co.uk/2011/04/04/finding-items-in-an-array-with-php/</link>
		<comments>http://www.tiposaurus.co.uk/2011/04/04/finding-items-in-an-array-with-php/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 23:17:57 +0000</pubDate>
		<dc:creator>Tiposaurus</dc:creator>
				<category><![CDATA[Basics]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://discomoose.org/2006/04/28/finding-items-in-an-array-with-php/</guid>
		<description><![CDATA[The problem: we have an array of items in PHP and we want to find out if a specific item is in the array. In code we can define the array as: IE we have an array called $fruitBasket which contains 5 strings, each of which is the name of a fruit. We want to [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start -->
<p>The problem: we have an array of items in PHP and we want to find out if a specific item is in the array. In code we can define the array as:</p>
<p><code><!--? // create an array of strings called $fruitBasket: $fruitBasket = array( "Apple", "Orange", "Mango", "Lemon", "Pear" ); ?--></code></p>
<p>IE we have an array called $fruitBasket which contains 5 strings, each of which is the name of a fruit. We want to find out if there is an Apple in the $fruitBasket - is there an element "Apple" in the array?</p>
<p>We do this with the following code:</p>
<pre>&lt;?
// create an array of strings called $fruitBasket:</pre>
<pre>$fruitBasket = array( "Apple", "Orange", "Mango", "Lemon", "Pear" );
// use the in_array() function to check if "Apple" is in the array:
if( in_array("Apple", $fruitBasket) )
{
  echo "Apple is in the array";
}
else
{
  echo "Apple is not in the array";
}
?&gt;</pre>
<p>This code uses the <em>in_array()</em> method to check if the element "Apple" exists in the array $fruitBasket:<br />
<code>in_array("Apple", $fruitBasket)</code></p>
<p>in_array() takes two parameters here: firstly the object we're looking for, in this case "Apple", and secondly the array which we're looking in, $fruitBasket. It then returns a boolean value: true if "Apple" is in the array, or false if it isn't.</p>
<p>References: <a href="http://www.php.net/manual/en/function.in-array.php" _mce_href="http://www.php.net/manual/en/function.in-array.php">php.net manual: in_array() function</a></p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.tiposaurus.co.uk/2011/04/04/finding-items-in-an-array-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

