<?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"
	>

<channel>
	<title>PHP Tutorials</title>
	<atom:link href="http://www.roughguidetophp.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.roughguidetophp.com</link>
	<description>An introductory tutorial to the world of PHP. Some more advanced subjects too</description>
	<pubDate>Wed, 08 Oct 2008 20:29:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Reporting and handling errors in PHP</title>
		<link>http://www.roughguidetophp.com/reporting-and-handling-errors-in-php/</link>
		<comments>http://www.roughguidetophp.com/reporting-and-handling-errors-in-php/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 10:13:57 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[Advanced PHP]]></category>

		<category><![CDATA[Add new tag]]></category>

		<category><![CDATA[debugging]]></category>

		<category><![CDATA[exceptions]]></category>

		<category><![CDATA[faults]]></category>

		<category><![CDATA[handling]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=62</guid>
		<description><![CDATA[
When developing a PHP project, whether it&#8217;s a simple website or a much larger web application, it&#8217;s always a good idea to take into account the potential appearance of errors in your PHP code.  An error occurs whenever PHP tries to execute an instruction which either results in an impossible outcome, or otherwise prevents [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:Types of Errors in PHP--></p>
<p>When developing a PHP project, whether it&#8217;s a simple website or a much larger web application, it&#8217;s always a good idea to take into account the potential appearance of <dfn>errors</dfn> in your PHP code.  An error occurs whenever PHP tries to execute an instruction which either results in an impossible outcome, or otherwise prevents the script from executing in the manner intended by the programmer.</p>
<p>Errors range from simple <dfn>fatal errors</dfn>, where program execution is halted at the point of the fault - usually a <dfn>syntax error</dfn> or a call to a function that doesn&#8217;t exist - to more complex errors where the cause of the problem isn&#8217;t immediately clear, and an indepth review of the code must be carried out to pinpoint the flaw.</p>
<h3>Undefined Variables (and other little glitches)</h3>
<p>Dealing with errors in a meaningful and graceful manner is a major consideration when developing PHP code. The default behaviour of PHP when encountering an error is to output a very simple line of text, with very basic formatting. It&#8217;s really quite ugly, and not the sort of thing you want your visitors to be seeing:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #990000;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$hello_world</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here, we are trying to access a variable $hello_world which has not been defined. The resulting output will be a screen with the following text</p>

<div class="wp_syntax"><div class="code"><pre class="text">Notice: Undefined variable: hello_world in C:\local\myphp.php on line 5</pre></div></div>

<p><b>Note: </b> You might not see this appear on screen right away. PHP sometimes is not fully set up to display all errors as they happen. To change PHP&#8217;s default behaviour, add the following lines before the echo statement above</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php"><span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">E_ALL</span><span style="color: #339933;">|</span>E_STRICT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display_errors&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Line 1 tells PHP to report all errors (including &#8220;strict&#8221; errors, which aren&#8217;t included as part of the E_ALL argument). Line 2 sets a PHP configuration variable which instructs PHP to always display errors. This value is also hidden away in the php.ini configuration file, but that&#8217;s not always accessible, so this way of setting it is perfectly valid.</p>
<p>One way to get around this error message cropping up in the first place is to make sure you&#8217;ve properly defined the variable before you use it. And if you&#8217;re uncertain whether or not the variable is defined, you can always place checks before calling it, like so</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">//the isset function checks if a given variable is defined or not</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$hello_world</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #000033;">$hello_world</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;The variable 'hello world' couldn't be located&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But that&#8217;s not terribly practical when you have to do the same checks for every variable, and the error message displayed still makes it all too clear to the visiting public that something&#8217;s gone wrong.</p>
<p>In the next section, we will look at ways of handling these error messages in a much more efficient manner</p>
]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/reporting-and-handling-errors-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Classes and Object-Oriented Programming in PHP</title>
		<link>http://www.roughguidetophp.com/classes-and-object-oriented-programming-in-php/</link>
		<comments>http://www.roughguidetophp.com/classes-and-object-oriented-programming-in-php/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 10:13:27 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[Advanced PHP]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=64</guid>
		<description><![CDATA[
So far throughout this tutorial, we&#8217;ve been looking at a method of programming called &#8220;procedural programming&#8221; in which program execution proceeds in a linear fashion, from start to finish and makes use of procedure calls. Procedures, which we&#8217;ve come to know as functions, allow us to execute arbitrary code segments at any point throughout the [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:Introduction to Classes--><br />
So far throughout this tutorial, we&#8217;ve been looking at a method of programming called &#8220;procedural programming&#8221; in which program execution proceeds in a linear fashion, from start to finish and makes use of <em>procedure calls</em>. Procedures, which we&#8217;ve come to know as <dfn>functions</dfn>, allow us to execute arbitrary code segments at any point throughout the script.</p>
<p>Procedural programming has many advantages, mainly the ability to store frequently used code within functions, and to encompass complex algorithms within easy-to-remember function names.</p>
<p>In this tutorial, I&#8217;ll take things a step further, and introduce you to the concept of Classes and Objects in PHP. To put it simply, an <dfn>Object</dfn> in the context of programming languages is a data structure which can contain any number of variables and functions all held together under a <dfn>Class name</dfn>. This <dfn>Class</dfn> can then be created in the normal scope of the code and stored inside a new variable, and multiple copies of this same class can be created. This allows for an even greater level of code encapsulation, allowing more complexity to be accessed from simple commands.</p>
<p>I mentioned that procedural programming allows us to encapsulate code within functions to be re-used as required. Classes take that a step further, allowing us to encapsulate many functions within a class and to create multiple copies of each class.</p>
<p>Note that, when inside a class definition, functions are referred to as <strong>methods</strong>, and variables are referred to as <strong>members</strong> - even though we still use the keyword &#8220;function&#8221; to define methods.</p>
<h3>Methods : &#8220;Functions defined inside Classes&#8221;</h3>
<p>When defining a function within a class, we refer to it as a <dfn>method</dfn>, and this method then becomes available to all <dfn>instances</dfn> of a class. The following example shows the syntax used to define a class, and also create an instance of the class for use. We will then call the method defined within the class, using this newly created instance</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">Class</span> MyClass <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Hello world!&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000033;">$myObject</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//create an instance of MyClass called $myObject</span>
<span style="color: #000033;">$myObject</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">myFunction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//call the method, myFunction() using the instance</span></pre></div></div>

<p>Lets take a step by step look at what&#8217;s happening here</p>
<ul>
<li><strong>Line 1</strong> : New classes in PHP are defined by the use of the Class keyword, followed by a user defined name to label the class. Here, we&#8217;re calling our class MyClass. The body of the class is placed between a pair of braces. Class naming convention is the same as for standard variables and function names, and traditionally class names start with an uppercase letter</li>
<li><strong>Lines 2-4</strong> : Within the class, we can define a range of variables and functions. Here, we&#8217;re defining just one function, in this case, the function myFunction, which serves a single purpose - to print &#8220;Hello world!&#8221;</li>
<li><strong>Line 6</strong> : Here&#8217;s where the fun begins! After a class has been created, we can then assign <em>instances</em> of the class to variables. Here, we make use of the &#8220;new&#8221; keyword to create a new copy of the MyClass object. Note the parentheses after MyClass. These aren&#8217;t explicitly required here, but later on in the tutorial, I&#8217;ll show you how you can place values within these to add extra functionality to your class.</li>
<li><strong>Line 7</strong> : by the use of the -> operator we can call functions and access variables within the class object. Here, we are calling the myFunction function. This will produce the now-familiar output

<div class="wp_syntax"><div class="code"><pre class="text">Hello world!</pre></div></div>

</li>
</ul>
<p>Multiple instances of the same class can be created. This way, we can reuse the functionality of the class without needing to worry about redeclaring variables, or functions</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$firstObject</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$secondObject</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$thirdObject</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$firstObject</span> <span style="color: #339933;">-&gt;</span><span style="color: #004000;">myFunction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$secondObject</span> <span style="color: #339933;">-&gt;</span><span style="color: #004000;">myFunction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$thirdObject</span> <span style="color: #339933;">-&gt;</span><span style="color: #004000;">myFunction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s take a step forwards now and add in some variables, or <em>members</em> into our functions. When defining variables within the body of a class, we refer to these variables as <dfn>members</dfn>. Here is an example of another class definition with a couple of members, $a and $b defined within its body.</p>
<h3>Members : &#8220;Variables defined inside Classes&#8221;</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">Class</span> MyClass <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000033;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000033;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> showValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;a equals $this-&gt;a&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot; and b equals $this-&gt;b&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000033;">$class</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
<span style="color: #000033;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">showValues</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;a plus b = &quot;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">a</span> <span style="color: #339933;">+</span> <span style="color: #000033;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><div class="code"><pre class="text">a equals 5 and b equals 10
a plus b = 15</pre></div></div>

<p>Here, you&#8217;ll see much that is familiar from the previous example. We define a class using the &#8220;Class&#8221; keyword, and inside the class is a function, or method showValues(). But, we&#8217;ve got a couple of new things here that need some explaining!</p>
<ul>
<li><strong>Lines 2 and 3</strong> : Here, you can see a couple of <em>members</em> $a and $b being defined, in much the same way as we&#8217;d define variables within the normal scope of a script, except when defining members inside a class, we must use the <strong>public</strong> keyword. There are exceptions to this rule though, and they will be explained shortly. Note that it is also acceptable to define members by using the keyword <strong>var</strong> e.g. <em>var $a = 5;</em> although this usage is a remnant from earlier versions of PHP and is only supported for compatibility reasons. It is better to use <strong>public</strong></li>
<li><strong>Lines 6 and 7</strong> : Now that we have the variables $a and $b defined within the body of the class, we can use these values from within functions. Here, you&#8217;ll see another mysterious variable - <strong>$this</strong>. $this is a very special variable in PHP. When inside a class, or object, $this allows you to refer to the class from <em>within itself</em>. Notice how, when calling a class method from outwith the class definition we use the notation <em>$class-&gt;functionName()</em>? When we&#8217;re running code from inside the class itself, we can do the same thing, but instead of using the name of the class, we simply use $this. This way we can refer to the class object without needing to know the specific variable name the class has been assigned to.In this case, we&#8217;re working with the values of $a and $b from inside the method by referring to them with $this-&gt;a and $this-&gt;b</li>
<li><strong>Line 12</strong> : Here we are accessing the values of $a and $b directly. You can see from this example how it&#8217;s much cleaner, and compact to use methods from within the class to access variables in this way, since it allows for greater code portability. This code can be improved like so,<br/><br/>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">Class</span> MyClass <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000033;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000033;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> addValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;a plus b = &quot;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">a</span> <span style="color: #339933;">+</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000033;">$class</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #000033;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValues</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ul>
<p>Confused? Yes, don&#8217;t worry, it can be a little overwhelming at first, but the thing to remember about Object Oriented Programming is that it uses lots of the same principles you&#8217;ve been using already, but with a little twist. It&#8217;s really about <em>changing the way you think about coding</em>, into a more packaged, compartmentalised view of things.</p>
<p>In the next section, we&#8217;re going to take a look at how we can control access to the members and methods contained within a class</p>
]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/classes-and-object-oriented-programming-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP Image manipulation with GD2</title>
		<link>http://www.roughguidetophp.com/php-image-manipulation-with-gd2/</link>
		<comments>http://www.roughguidetophp.com/php-image-manipulation-with-gd2/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 10:08:45 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[Advanced PHP]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=51</guid>
		<description><![CDATA[
One of the more surprising things you can learn about PHP is that it&#8217;s actually very useful when it comes to manipulating images of varying filetypes and formats. And a good thing too, since a lot of the internet is made up of pictures!
The functionality of PHP in terms of image manipulation ranges from very [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:PHP and images, an introduction--><br />
One of the more surprising things you can learn about PHP is that it&#8217;s actually very useful when it comes to manipulating images of varying filetypes and formats. And a good thing too, since a lot of the internet is made up of pictures!</p>
<p>The functionality of PHP in terms of image manipulation ranges from very simple resizing of images through to more complex effects and functions using image libraries such as ImageMagick</p>
<p>In this tutorial, I&#8217;ll take you through some of the more common uses for PHP when manipulating images, and then introduce you to some of the cooler aspects of this powerful little tool, which could help you to really add a bit of jazz and sparkle to your website!</p>
<p>Let&#8217;s start with something simple. We&#8217;ll take a particular image, how about, let&#8217;s say, an <a href="http://www.roughguidetophp.com/wp-content/uploads/2008/08/puppy.jpg">image of a cute puppy</a> and then run some functions on it to determine a few facts about it.</p>
<h3>getimagesize()</h3>
<p>We&#8217;ll start here by determining the image type (is it a jpeg, gif, png?), dimensions (height and width) and a few other things by using the getimagesize() function.  Note that for this example, the image we&#8217;re using - puppy.jpg - must be in the same directory as the PHP script we&#8217;re calling this function from</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$info</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;puppy.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$info</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The function returns an array containing all of the relevant information, so the above code will produce</p>

<div class="wp_syntax"><div class="code"><pre class="text">Array (
  [0] =&gt; 491 //width
  [1] =&gt; 367 //height
  [2] =&gt; 2 //this number is a constant representing the image type
  [3] =&gt; width=&quot;491&quot; height=&quot;367&quot;
  [bits] =&gt; 8  //number of bits-per-pixel in the image
  [channels] =&gt; 3 // 3 = RGB, 4 = CMYK
  [mime] =&gt; image/jpeg //determines the filetype
)</pre></div></div>

<p>From this we can tell the image is 491 by 367 pixels in size, with an 8-bit colour depth, and 3 colour channels. We also have the filetype, stored as the &#8220;mime&#8221; value of the array - &#8220;image/jpeg&#8221;.</p>
<p>Another way we can retrieve the type of image used is to make use of PHP&#8217;s image_type_to_extension() function which will take, as its argument, the value of the array in index &#8220;2&#8243; - in this case a value of 2, which represents the filetype of the image. So, for this value we get</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #990000;">echo</span> image_type_to_extension<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>which outputs</p>

<div class="wp_syntax"><div class="code"><pre class="text">.jpeg</pre></div></div>

<p>So, now we have this information, what else can we do with the file? We could try resizing the image, and saving a new copy of it. But first, let&#8217;s just try reading in the file data and see what happens</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: image/jpeg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$image</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span>imagecreatefromjpeg<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;puppy.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
imagejpeg<span style="color: #009900;">&#40;</span><span style="color: #000033;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
imagedestroy<span style="color: #009900;">&#40;</span><span style="color: #000033;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<div id="attachment_244" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.roughguidetophp.com/wp-content/uploads/2008/08/puppy.jpg"><img class="size-medium wp-image-244" title="puppy" src="http://www.roughguidetophp.com/wp-content/uploads/2008/08/puppy-300x224.jpg" alt="And behold, a picture of a cute puppy!" width="300" height="224" /></a><p class="wp-caption-text">And behold, a picture of a cute puppy!</p></div>
<ul>
<li><strong>Line 1 </strong> : This header value tells the browser to interpret the following data as Jpeg image data</li>
<li><strong>Line 2 </strong> : Read the image data from the file, puppy.jpg using imagecreatefromjpeg(). The @ symbol simply supresses any errors that might occur. This creates a <dfn>GD Resource</dfn> $image, which can then be manipulated as required, or sent as output to the browser. Function like this one are part of PHP&#8217;s <strong><dfn>GD2 Library</dfn></strong> of image manipulation functions. Each function works on, or loads/saves an object representing image data called a <strong><dfn>GD Resource</dfn></strong>.</li>
<li><strong>Line 3 </strong> : imagejpeg() is the function that outputs the image data to the browser</li>
<li><strong>Line 4 </strong> : After we have output the image data, destroy the image resource with imagedestroy()</li>
</ul>
<p>So that&#8217;s how we read in and print out simple jpeg data. Similar functions also exist for PNG and GIF filetypes - namely imagecreatefromgif(), imagegif() and imagecreatefrompng(), imagepng()</p>
<h3>Resizing and Rotation</h3>
<p>What else can we do with the image once we&#8217;ve loaded it into memory with imagecreatefromjpeg()? Let&#8217;s try resizing the image. Here we will make use of the imagecreatetruecolor() function to create a blank GD resource to copy the resized image into, and the imagecopyresampled() function to perform the copying.</p>
<p>The arguments for imagecopyresampled() are as follows</p>

<div class="wp_syntax"><div class="code"><pre class="text">bool imagecopyresampled  ( resource $dst_image, resource $src_image, int $dst_x, int $dst_y, int $src_x, int $src_y, int $dst_w, int $dst_h, int $src_w, int $src_h )</pre></div></div>

<p>Which might need a little explanation: The first two are relatively straightforward, $dst_image is the GD resource to copy <em>to</em>, and $src_image is the GD resource to copy <em>from</em>. After these two arguments are 8 parameters that specify (x, y) co-ordinates on both the source and destination images, and width and height values for the source and destination images also.</p>
<p>These points and widths are used to calculate which area of the source image is to be copied based on the top-left (x, y) co-ordinates and the source width and height, and the new width and height of the destination image, as well as the top-left co-ordinate values for the new image to start at</p>
<p>When we call imagecreatetruecolor() we must also specify a width and height for this destination image.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: image/jpeg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$image</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span>imagecreatefromjpeg<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;puppy.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;puppy.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$percent</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">150</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$image_dest</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span>imagecreatetruecolor<span style="color: #009900;">&#40;</span><span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000033;">$percent</span><span style="color: #339933;">,</span> <span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000033;">$percent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
imagecopyresampled<span style="color: #009900;">&#40;</span><span style="color: #000033;">$image_dest</span><span style="color: #339933;">,</span> <span style="color: #000033;">$image</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000033;">$percent</span><span style="color: #339933;">,</span> <span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000033;">$percent</span><span style="color: #339933;">,</span> <span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
imagejpeg<span style="color: #009900;">&#40;</span><span style="color: #000033;">$image_dest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will output an image 150% the width and height of the original. You can control the size of the image by changing the value of $percent. With some experimentation you can also try changing the ratio of the width and height to distort the image, and change the values within imagecopyresampled() to change the portion of the image that is resized.</p>
<p>Why not try <strong>image rotation</strong> by replacing the imagejpeg($image_dest); function call to the following</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #666666; font-style: italic;">// 0x000000 is the background colour to use for the blank</span>
<span style="color: #666666; font-style: italic;">// space left after the rotation calculation</span>
<span style="color: #000033;">$rotate</span> <span style="color: #339933;">=</span> imagerotate<span style="color: #009900;">&#40;</span><span style="color: #000033;">$image_dest</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">45.0</span><span style="color: #339933;">,</span> 0x000000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
imagejpeg<span style="color: #009900;">&#40;</span><span style="color: #000033;">$rotate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<div id="attachment_245" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.roughguidetophp.com/wp-content/uploads/2008/08/rotate.jpg"><img class="size-medium wp-image-245" title="rotate" src="http://www.roughguidetophp.com/wp-content/uploads/2008/08/rotate-300x300.jpg" alt="Cute puppy has been rotated 45 degrees. Poor thing" width="300" height="300" /></a><p class="wp-caption-text">Cute puppy has been rotated 45 degrees. Poor thing</p></div>
<h3>Saving the Image To Disk</h3>
<p>Once you&#8217;ve manipulated the image enough, you may then want to save it to disk. A common usage for such a function is the use of <dfn>generated thumbnail images</dfn> for image upload sites. The idea being that a user would upload an image to a forum, or an image hosting site, and they would be given the option of either linking to the full-size image, or to a small thumbnail of the image.</p>
<p>Such functionality is easy to achieve with the GD2 library, we simply modiy to the code from earlier by adding a new parameter to imagejpeg. This simply saves the GD resource to a file on the disk, as specified by the name given.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$image</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span>imagecreatefromjpeg<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;puppy.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;puppy.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// let's save a 33% thumbnail</span>
<span style="color: #000033;">$percent</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">33</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$image_dest</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span>imagecreatetruecolor<span style="color: #009900;">&#40;</span><span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000033;">$percent</span><span style="color: #339933;">,</span> <span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000033;">$percent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
imagecopyresampled<span style="color: #009900;">&#40;</span><span style="color: #000033;">$image_dest</span><span style="color: #339933;">,</span> <span style="color: #000033;">$image</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000033;">$percent</span><span style="color: #339933;">,</span> <span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000033;">$percent</span><span style="color: #339933;">,</span> <span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// modify this final line to add an extra parameter to imagejpeg</span>
imagejpeg<span style="color: #009900;">&#40;</span><span style="color: #000033;">$image_dest</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;puppy_thumb.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<div id="attachment_248" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.roughguidetophp.com/wp-content/uploads/2008/08/thumb1.jpg"><img class="size-medium wp-image-248" title="thumb1" src="http://www.roughguidetophp.com/wp-content/uploads/2008/08/thumb1-300x162.jpg" alt="New, smaller thumbnail puppy" width="300" height="162" /></a><p class="wp-caption-text">New, smaller thumbnail puppy</p></div>
<p>This then results in a new file on the disk, smaller in size and dimensions than the original called &#8220;puppy_thumb.jpg&#8221;. The original file is not overwritten or modified during this action. We have also removed the header() function call from the first line, since we will no longer be writing the image data to the browser window.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/php-image-manipulation-with-gd2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>An Introduction to PHP</title>
		<link>http://www.roughguidetophp.com/an-introduction-to-php/</link>
		<comments>http://www.roughguidetophp.com/an-introduction-to-php/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 10:00:47 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[html]]></category>

		<category><![CDATA[hypertext preprocessor]]></category>

		<category><![CDATA[internet]]></category>

		<category><![CDATA[introduction]]></category>

		<category><![CDATA[php coding]]></category>

		<category><![CDATA[php development]]></category>

		<category><![CDATA[phpsite]]></category>

		<category><![CDATA[rasmus lerdorf]]></category>

		<category><![CDATA[web programming]]></category>

		<category><![CDATA[websites]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=35</guid>
		<description><![CDATA[
Website design with PHP
It&#8217;s quite likely you&#8217;ve reached this page because you&#8217;re interested in learning how to build your first website with PHP, or modify your existing website to make use of PHP. It&#8217;s also quite likely, if you&#8217;re just starting out with PHP, or programming in general, that you won&#8217;t really know what PHP [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:Introduction--></p>
<h3>Website design with PHP</h3>
<p>It&#8217;s quite likely you&#8217;ve reached this page because you&#8217;re interested in learning how to build your first website with PHP, or modify your existing website to make use of PHP. It&#8217;s also quite likely, if you&#8217;re just starting out with PHP, or programming in general, that you won&#8217;t really know what PHP <i>is</i> exactly, or what it does.</p>
<p>PHP is one of the most widely used technologies for building websites. Alongside the database management system MySQL, it powers some of the most popular and heavily visited sites. The role of PHP in these websites is to construct the pages of text and images you see when you navigate these sites, and it can also be used for even more complex operations behind the scenes. PHP can dynamically construct pages based on user input, so that you don&#8217;t need to offer visitors static pages of text, but can instead provide them with information based on their specific requirements.</p>
<p>A user might, for example, select from a drop-down list of items in a shop, and be presented with a detailed description of the selected product, or fill in an email form to contact you directly about a service, bypassing the need to provide your email address on a public website.</p>
<p>If you&#8217;re already familiar with PHP, you may already have covered most of the topics in the first part of this tutorial. We will cover some of the fundamentals of programming, including for loops, if&#8230;else statements and more. Although some people may be well versed in the use of these programming methods, there may still be information of value. Near the end of each section I will look at each subject in more detail, and there may even be one or two nuggets of information which even the more experienced PHP developer will find useful!</p>
<p>This tutorial won&#8217;t aim to cover everything though, and doesn&#8217;t aim to replace the more comprehensive PHP references out there - including PHP&#8217;s own, extremely useful, online library available through <a href="http://www.php.net/manual/en/index.php">php.net</a></p>
<h3>What is PHP?</h3>
<p>It is said that the acronym PHP is recursive, and stands for &#8220;PHP: Hypertext Preprocessor&#8221;, which would no doubt cause some headaches if you attempted to write it out in full. A more sensible explanation is that it stands for &#8220;Personal Home Page&#8221;, originally from a piece of software developed by Danish programmer Rasmus Lerdorf to maintain his own personal homepage.</p>
<p>These days, PHP is still used to maintain personal homepages. Also business pages, online directories, online shops&#8230; the list goes on. It is an extremely versatile language and, while its critics may say it lacks the langauge processing capabilities of Perl, say, or the complexities of C/C++, there&#8217;s no arguing that it is a very capable language for use in creating the underlying code and structure of a great number of websites</p>
<div id="attachment_156" class="wp-caption aligncenter" style="width: 450px"><img class="size-full wp-image-156" title="whatisphp" src="/wp-content/uploads/2008/08/whatisphp.jpg" alt="" width="440" height="182" /><p class="wp-caption-text">How PHP Works. Roughly</p></div>
<p>The basic idea behind PHP is outlined in the image above. PHP code is stored within <b>PHP Files [1]</b>, which are then passed through a <b>PHP Parser [2]</b> that then generates the code necessary to display the <b>webpage [3]</b> you see when you visit a specific URL, like http://www.mywebsite.com/index.php</p>
]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/an-introduction-to-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Working with dates and times in PHP</title>
		<link>http://www.roughguidetophp.com/working-with-dates-and-times-in-php/</link>
		<comments>http://www.roughguidetophp.com/working-with-dates-and-times-in-php/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 07:27:21 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[Advanced PHP]]></category>

		<category><![CDATA[date()]]></category>

		<category><![CDATA[dates]]></category>

		<category><![CDATA[microtime()]]></category>

		<category><![CDATA[mktime()]]></category>

		<category><![CDATA[time()]]></category>

		<category><![CDATA[times]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=45</guid>
		<description><![CDATA[
When developing more complex PHP sites, especially forums, discussion boards and blogs where date and time functions are important, you&#8217;ll regularly encounter situations where you need to display and manipulate values based around the current date and time. PHP includes a range of functions which can help you to easily format and adjust your date [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:time(), date() and mktime()--><br />
When developing more complex PHP sites, especially forums, discussion boards and blogs where date and time functions are important, you&#8217;ll regularly encounter situations where you need to display and manipulate values based around the current date and time. PHP includes a range of functions which can help you to easily format and adjust your date and time displays to suit your needs.  In this tutorial, we&#8217;ll look at some easy ways to display calendar information for your visitors.</p>
<h3>time() or, the Unix Timestamp</h3>
<p>One of the simplest date/time functions in PHP is time(). This simple function takes no arguments, and simply returns a numeric value equal to the number of seconds since the &#8220;Unix Epoch&#8221;, declared as January 1st 1970 at midnight (GMT) - just an arbitrary date chosen to base timing calculations on when working with dates and times. This gives a start time upon which we can base our time calculations</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #000033;">$time</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//gives us something like 1219075172</span></pre></div></div>

<h3>PHP&#8217;s date() function</h3>
<p>This timestamp can then be used as input to the date() function to format it for improved readability. The date() function takes, as its arguments, a string to define how we wish the formatted time to appear and secondly, a Unix timestamp just like the one returned by time(). It is also possible to use date() without specifying a timestamp and it will use the current time as its source.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y, g:i a&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1219075172</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//produces &quot;August 18, 2008, 4:59 pm&quot;</span></pre></div></div>

<p>The string used in the date() function can contain a number of single letters which are then substituted for their human readable counterparts according to the value of the timestamp. In our case, the following substitutions are made</p>
<ul>
<li>F: The full textual representation of the month, August in this case</li>
<li>j: The day of the month, without leading zeros, e.g. 4, 18, 22</li>
<li>Y: The full year, e.g. 2008</li>
<li>g: The current hour, in 12-hour format</li>
<li>i: The current number of minutes</li>
<li>a: Either am, or pm, depending on the time</li>
</ul>
<p>A full list of all the codes, and their textual counterparts can be seen on PHP.net&#8217;s date() page <a href="http://uk2.php.net/manual/en/function.date.php">here</a>. Extra text can be added to the formatted string as required, but remember to escape any characters which date() will interpret as formatting text.</p>
<h3>Make time with mktime()</h3>
<p>An alternative to the time() function is mktime(). This function is useful when we wish to use a date/time which is not the current time. mktime() takes as its arguments numerical values representing, in this order - <i>hours, minutes, seconds, months, days, years</i> - and a final value to declare whether daylight savings time is to be taken into account.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mktime</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1980</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//produces 345464100</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y, g:i a&quot;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//produces &quot;December 12, 1980, 10:15 am&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/working-with-dates-and-times-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP Syntax</title>
		<link>http://www.roughguidetophp.com/php-syntax/</link>
		<comments>http://www.roughguidetophp.com/php-syntax/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 07:25:34 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[basic]]></category>

		<category><![CDATA[editing php]]></category>

		<category><![CDATA[functions]]></category>

		<category><![CDATA[local server]]></category>

		<category><![CDATA[php hosting]]></category>

		<category><![CDATA[php syntax]]></category>

		<category><![CDATA[remote host]]></category>

		<category><![CDATA[starting php]]></category>

		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=36</guid>
		<description><![CDATA[

Saving a PHP file
A PHP file is basically a text file containing all the required PHP code to run a specific program.  PHP code can be written with the use of a simple text editor, like Windows Notepad, or the more useful Crimson Editor. All you need to do is type in the required [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:Structure of a basic PHP file--><br />
<!--Now that we've successfully set up our PHP development environment (and if not, skip to the previous tutorial for instructions on how to set up a system for creating a testing your own PHP files) it's time for you to create your very first PHP file and execute it--></p>
<h3>Saving a PHP file</h3>
<p>A PHP file is basically a text file containing all the required PHP code to run a specific program.  PHP code can be written with the use of a simple text editor, like Windows Notepad, or the more useful <a href="http://www.crimsoneditor.com/">Crimson Editor</a>. All you need to do is type in the required PHP code and save the file with the extension .php</p>
<p>You can create a PHP file yourself by creating a blank text file and simply renaming it with the extension .php. You need absolutely nothing else for this to work as a valid PHP file. Although, as I&#8217;m sure you&#8217;ll realise, a blank file won&#8217;t do anything much of use!  First of all, we need to enter some valid PHP code into this file</p>
<p>(<b>Important:</b> <b>Don&#8217;t use</b> a program such as Microsoft Word, Frontpage or other more complex word processors to create your PHP file. Although the page might look free of formatting when you save the file, there will be other hidden formatting elements within the file itself that will prevent any PHP code from running.)</p>
<p>This is an example of some PHP code</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//this code is saved in a file named e.g. myphp.php and then</span>
<span style="color: #666666; font-style: italic;">//saved in e.g. c:\local\mywebsite\myphp.php or http://www.mywebsite.com/myphp.php</span>
  <span style="color: #000033;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
  <span style="color: #000033;">$y</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;b&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Hello world&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This code is the exact same code that will work as a PHP file on a server, and we&#8217;ll take a look shortly at how you can create your very own simple PHP file to run as a basic web-page sample. First though, we need to know where to locate our PHP files.</p>
<h3>The location of the PHP file on disk</h3>
<p>An important thing for us at this point is to know <b>where to save this .php file</b> for it to be recognised as a valid web-page file. Any PHP documents must be placed within the <dfn>document root</dfn> of a website server. In simple terms, this means that on your computer, or on the remote computer that you&#8217;ve got set up as a website host, are a number of folders and directories. One of these directories will be set up as the <b>document root</b>, and is usually named <b>c:\local</b> if you&#8217;re using your own Windows machine to run the code, or something like <strong>/htdocs</strong> or <strong>/html_root</strong> if you&#8217;re running on a remote server.</p>
<p>The <b>document root</b> is the base directory from which all the website data is served from. Any sub-directories within this main root directory will show up as sub-folders when you type in the URL</p>
<p>e.g. if you have a folder &#8220;mysite&#8221; within the c:\local\ folder, you&#8217;ll be able to access the URL http://localhost/mysite to see the contents of this folder. Any PHP files within this folder can be accessed by adding the PHP filename to the end. e.g. http://localhost/mysite/myphpfile.php</p>
<p>Similarly with a remote host, a folder like htdocs/mysite will be accessible from http://www.mywebsite.com/mysite and any relevant PHP files in the <i>mysite</i> folder will be at http://www.mywebsite.com/mysite/myphpfile.php and so on</p>
<h3>Index.php</h3>
<p>PHP files can be named anything you want, according to the filename rules of the operating system you&#8217;re using (For example, Windows filenames can&#8217;t contain certain characters) - you can have &#8220;myphpfile.php&#8221;, &#8220;biscuits.php&#8221;, &#8220;cheese_on_toast.php&#8221;. But a useful name for a PHP file is <dfn>index.php</dfn></p>
<p>The reason behind naming a php file <b>index.php</b> is that the file &#8220;index.php&#8221; will always be loaded first when visiting a URL that doesn&#8217;t specify a filename e.g. http://www.mywebsite.com - in this case, that URL will automatically look for a file named http://www.mywebsite.com/index.php. This is an extremely useful way of defining a main page, or <b>home page</b> for your site.</p>
<p>In the next section, we&#8217;ll make a start on generating our first PHP file, and seeing the results in a browser window.</p>
<h3>Adding your first few lines of code</h3>
<p>After saving a blank PHP file in your chosen location, point your browser to the URL where your file is located, according to the directory structure and the location you saved the file in. E.g. using the address http://localhost/myphp/myphp.php if you saved the file in c:\local\myphp\myphp.php or http://www.mywebsite.com/mysite/file.php if you saved the file <i>file.php</i> on a remote server in the folder <em>mysite</em>).</p>
<p>After entering this URL, you should see a blank screen appear. This is because we&#8217;ve not added anything to the file yet!</p>
<p>Return to your text editor, and add the following lines to the file</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Hello world&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Type it out <i>exactly</i> as it&#8217;s written above. I&#8217;ll explain the symbols used here in just a moment, but return to the web browser now, and reload the page. You&#8217;ll see the following output</p>

<div class="wp_syntax"><div class="code"><pre class="php">Hello world</pre></div></div>

<p>If you see the above text, then everything is going well! You should now have your first working PHP application. If you don&#8217;t see the text above appear on the screen, go back and check the file again. You must enter the code <i>exactly</i> as it appears above. Now, I shall explain what&#8217;s going on here</p>
<ul>
<li>The <b>&lt;?php</b> and <b>?&gt;</b> tags are the PHP <i>delimeters</i>. They are used to declare where the PHP code starts, and where it ends. This will be of more importance in the next tutorial which deals with the combination of HTML code and PHP. For now, all you need to know is that these symbols are vital to the execution of the code. It is also acceptable to start and stop these PHP delimiters as often as you like. So, for example, the following code is perfectly valid<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Hello world&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Hello world again!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Note that you can also use the symbolds <b>&lt;?</b> and <b>?&gt;</b> to express the beginning and end of the code, but the use of <b>&lt;?php</b> and <b>?&gt;</b> is much more widely accepted as the standard, due to the way most servers interpret the code.
</li>
<li>
On the second line, you will see a commonly used PHP command, &#8220;echo&#8221;, which we&#8217;ll look at again later. For now, the important thing to note is the semicolon used at the end of the line. It is absolutely vital, in PHP, to terminate any expression or command with this semicolon, otherwise the code will fail.
</li>
</ul>
<p><i>If you&#8217;re coming from an ASP background (another language similar to PHP, not as widely used but still very popular) you can enable PHP to recognise ASP style tags, like so</i></p>

<div class="wp_syntax"><div class="code"><pre class="asp"><span style="color: #0000ff; font-weight: bold;">&lt;%</span> echo <span style="color: #cc0000;">&quot;These tags look a bit different&quot;</span><span style="color: #006600; font-weight: bold">;</span> <span style="color: #0000ff; font-weight: bold;">%&gt;</span></pre></div></div>

<h3>Using Comments in PHP</h3>
<p>Comments are an excellent way to document your code. They allow for a way to place handy notes and thoughts for yourself alongside your code so that you can pick up where you left off when you return to either add new features or debug the script. Comments also provide an effective way for other developers to understand your thoughts and motivations when working on your code.</p>
<p>Comments can be written in one of three ways as the following examples demonstrate</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* this is a comment
   it can cover multiple lines */</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Yes, it is&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//this is also a comment, but only for a single line</span>
<span style="color: #666666; font-style: italic;">#this is also a comment, but not used as much</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I never use it anyway&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;You can also..&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//..add comments after statements</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;As well as..&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/*multiline
  comments */</span> <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;..and another statement after&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Comments may seem a bit counterintuitive at first. If you&#8217;re not used to programming, you might wonder what the point is of adding all that extra work if it does nothing useful. In time though, and with experience of larger projects, you&#8217;ll find that all those little thoughts and statements become invaluable when you return to debug code days, weeks, months or years later.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/php-syntax/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Combining PHP with HTML</title>
		<link>http://www.roughguidetophp.com/adding-php-to-html-files/</link>
		<comments>http://www.roughguidetophp.com/adding-php-to-html-files/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 07:17:00 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[adding php into html]]></category>

		<category><![CDATA[development php]]></category>

		<category><![CDATA[php and html]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[website code]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=37</guid>
		<description><![CDATA[
Now it&#8217;s time to get down to the real task at hand - actually creating a working web-page using PHP. In this tutorial, I&#8217;ll show you how to create a simple web-page to demonstrate how to add a little bit of dynamic jazz to an otherwise static site.
Mixing PHP with HTML
I&#8217;m assuming, for this tutorial, [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:Mixing PHP With HTML--></p>
<p>Now it&#8217;s time to get down to the real task at hand - actually creating a working web-page using PHP. In this tutorial, I&#8217;ll show you how to create a simple web-page to demonstrate how to add a little bit of dynamic jazz to an otherwise static site.</p>
<h3>Mixing PHP with HTML</h3>
<p>I&#8217;m assuming, for this tutorial, and throughout the rest of the tutorials, that you have at least a basic knowledge of HTML - enough to be able to use simple tags such as &lt;br/&gt; (newline),  &lt;a&gt;, &lt;form&gt; and so on. If not, why not check out any of the decent HTML tutorials around the web - Google for &#8220;HTML Tutorial&#8221;</p>
<h3>HTML PHP is HTML</h3>
<p>One thing to realise here is that an HTML page can function as both an HTML page <em>and as a PHP page</em>. Let me explain:</p>
<p>In the previous tutorial, we saw how a PHP file could be created by using the start and end tags, &lt;?php and ?&gt;. We also saw how these tags could start and stop as often as you wished. Within the context of a standard PHP file, this doesn&#8217;t make much sense. After all, what&#8217;s the point of stopping and starting the PHP code if we&#8217;re just going to be executing more of the same PHP?</p>
<p>The answer lies in the fact that, between those PHP tags, we can output <strong>HTML content</strong>. Take the following code snippet as an example. This is the full text from a PHP file</p>

<div class="wp_syntax"><div class="code"><pre class="php">A sample PHP file<span style="color: #339933;">&lt;</span>br<span style="color: #339933;">/&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;And here is the second line&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Run this, and we see the output</p>

<div class="wp_syntax"><div class="code"><pre class="php">A sample PHP <span style="color: #990000;">file</span>
And here is the second line</pre></div></div>

<p>Now, what&#8217;s going on here? You might well have guessed by now, the &#8220;echo&#8221; command is used to output data to the screen. But there&#8217;s no echo command on the first line, so how is this text appearing? And more importantly, you might ask, <em>where&#8217;s the semicolon?</em> I did say in an earlier tutorial that <em>any PHP code without a semicolon at the end of each statement will fail</em>. But the important thing to note here is that on line 1, before the &lt;?php tag, <strong>that&#8217;s not PHP code</strong></p>
<p>Huh? What?</p>
<p>PHP files can contain both <b>static text</b> and <b>PHP code</b>, the important thing to realise is that, whenever the code is between the <b>&lt;?php</b> and <b>?&gt;</b> tags, it will be <i>executed as PHP code</i></p>
<p>The next page will go into a little more detail</p>
]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/adding-php-to-html-files/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Functions in PHP</title>
		<link>http://www.roughguidetophp.com/functions-in-php/</link>
		<comments>http://www.roughguidetophp.com/functions-in-php/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 07:09:46 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=34</guid>
		<description><![CDATA[
Functions are arguably one of the most useful features of PHP. They allow a programmer to encapsulate sections of code within a structure which can then be executed from anywhere else in the program flow. Functions are instantiated by use of the function keyword, followed by the user-defined function name including a list of parameters [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:An Introduction to Functions--><br />
Functions are arguably one of the most useful features of PHP. They allow a programmer to encapsulate sections of code within a structure which can then be executed from anywhere else in the program flow. Functions are instantiated by use of the function keyword, followed by the user-defined function name including a list of parameters between the paretheses (but we&#8217;ll worry about that in a moment) and the body of the function is contained between braces.</p>
<p>Just as when defining variables, specific naming conventions must be adhered to. A function name can contain letters, numbers and underscores, and must not start with a number. They are also, like variables, case-sensitive. A (very simple) example:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">function</span> my_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Hello world&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000033;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;The value of <span style="color: #000099; font-weight: bold;">\$</span>a is $a&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
my_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php">Hello world
The value of <span style="color: #000033;">$a</span> is <span style="color: #cc66cc;">5</span></pre></div></div>

<ul>
<li>Line 1 : The definition of the function. This declares that you, the programmer, have decided to create a function for use called &#8220;my_function&#8221;. The empty brackets () will be filled in later, but they are important and you cannot leave them out.</li>
<li>Lines 2-4 : Inside the braces you can place whatever code you desire</li>
<li>Line 6 : This is a <dfn>function call</dfn> and lets PHP know that we want to execute the function at this point. This differs from the function declaration in that we are not using the <dfn>function</dfn> keyword to create a function. This function call can occur anywhere within the code, as long as it occurs <em>after the function has been declared</em></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/functions-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Arrays in PHP</title>
		<link>http://www.roughguidetophp.com/arrays-in-php/</link>
		<comments>http://www.roughguidetophp.com/arrays-in-php/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 07:09:44 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[arrays]]></category>

		<category><![CDATA[associative array]]></category>

		<category><![CDATA[keys]]></category>

		<category><![CDATA[multidimensional array]]></category>

		<category><![CDATA[non-assoc]]></category>

		<category><![CDATA[non-associative array]]></category>

		<category><![CDATA[nonassociative]]></category>

		<category><![CDATA[php array]]></category>

		<category><![CDATA[values]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=32</guid>
		<description><![CDATA[
Arrays
There are times in programming where you need to keep track of many variables at once. Maybe the current time of day, the number of items in a package or the name of a certain product. These variables may all be very similar too though, you might, for example, need to keep track of the [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:Introduction to Arrays--></p>
<h3>Arrays</h3>
<p>There are times in programming where you need to keep track of many variables at once. Maybe the current time of day, the number of items in a package or the name of a certain product. These variables may all be very similar too though, you might, for example, need to keep track of the names of all employees in a company, or the number of items inside each of a large number of packages.  Arrays can be used to keep track of these multiple values from within one variable.</p>
<p>Arrays are a special type of data structure in PHP. They are used to hold multiple values, in a way which allows the values held within to be re-arranged, searched or sorted. Values can also be removed or added easily. Arrays are especially useful when you&#8217;re dealing with lists of items, or collections of values that need to be sorted into ascending or descending order.</p>
<h3>Creating and manipulating arrays</h3>
<p>An array is initialised using the language construct array(), and is assigned to a PHP variable name, in exactly the same way as other scalar variables [reference scalar in previous tut]. Arrays can contain any number of values, each of which can be any of the PHP data types. Here is an example of a simple array</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;two&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3.0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;four&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will initialise the variable $array as an array containing the values 1, &#8220;two&#8221;, 3.0 and &#8220;four&#8221;.</p>
<p>An array like this is known as a <b><dfn>Non-associative array</dfn></b> because the values are not associated with any specific <dfn>keys</dfn> but are instead just numbered numerically, like so&#8230;</p>
<div id="attachment_192" class="wp-caption aligncenter" style="width: 440px"><img src="/wp-content/uploads/2008/08/array1.jpg" alt="An example of a simple non-associative array" title="array1" width="430" height="187" class="size-full wp-image-192" /><p class="wp-caption-text">An example of a simple non-associative array</p></div>
<h3>Displaying Arrays</h3>
<p>Once this array has been initialised, we can check the contents of the array in certain ways</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php"><span style="color: #990000;">echo</span> <span style="color: #000033;">$array</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This produces</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php"><span style="color: #990000;">Array</span>
<span style="color: #990000;">Array</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> two <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> four <span style="color: #009900;">&#41;</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> int<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;two&quot;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> float<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;four&quot;</span> <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Echo is not built to handle complex data structures like arrays. Instead we need to turn to our old friend print_r() - but even then, we&#8217;re not getting the full picture. Var_dump() produces the most information about our array, giving us the datatype held in each position of the array.</p>
<h3>Associative arrays</h3>
<p>Note the numbers in square brackets on lines 2 and 3 in the above example. These numbers correspond to the &#8220;keys&#8221; of each item in the array. Arrays work by assigning each item a &#8220;key-value pair&#8221; by which to reference each position in the array. In the above example, the keys are generated automatically, in sequence according to the order in which the values were entered. We can over-ride this behaviour though, and specify our own keys for each value</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;two&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3.0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;four&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here, we&#8217;ve assigned the entries in the array to keys in descending numerical order.</p>
<p>Another example is an array containing a list of employee names, and their associated job titles</p>
<p><img src="/wp-content/uploads/2008/08/array2.jpg" alt="" title="array2" width="500" height="217" class="aligncenter size-full wp-image-194" /></p>
<p>Now, you might be asking, what&#8217;s the use of this feature in real code? Well, this gives me a good opportunity to introduce a powerful feature of arrays - sorting.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/arrays-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Variables in PHP</title>
		<link>http://www.roughguidetophp.com/variables-in-php/</link>
		<comments>http://www.roughguidetophp.com/variables-in-php/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 07:09:42 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[address]]></category>

		<category><![CDATA[assignment]]></category>

		<category><![CDATA[echo]]></category>

		<category><![CDATA[float]]></category>

		<category><![CDATA[floating point]]></category>

		<category><![CDATA[function]]></category>

		<category><![CDATA[integer]]></category>

		<category><![CDATA[php variables]]></category>

		<category><![CDATA[printing variables]]></category>

		<category><![CDATA[print_r]]></category>

		<category><![CDATA[reference]]></category>

		<category><![CDATA[string]]></category>

		<category><![CDATA[strings]]></category>

		<category><![CDATA[value]]></category>

		<category><![CDATA[var_dump]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=31</guid>
		<description><![CDATA[
Variables are probably one of the most commonly used code constructs you&#8217;ll come across while programming. If you have any experience of mathematics, especially algebra, you&#8217;ll be aware of the concept of using symbols to represent unknown quantities in equations, like in the following example

x + 5 = 7;
from this we can deduce that x [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:Introduction to variables in PHP--><br />
Variables are probably one of the most commonly used code constructs you&#8217;ll come across while programming. If you have any experience of mathematics, especially algebra, you&#8217;ll be aware of the concept of using symbols to represent unknown quantities in equations, like in the following example</p>

<div class="wp_syntax"><div class="code"><pre class="text">x + 5 = 7;
from this we can deduce that x = 2</pre></div></div>

<p>In PHP it works in a similar fashion, except we usually know the value of x, and simply assign it a specific value, such as x = 5. We can then use that value to perform calculations using other symbols and numbers. For example</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span>  <span style="color: #666666; font-style: italic;">//x now equals 5</span>
<span style="color: #000033;">$y</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$x</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">10</span>  <span style="color: #666666; font-style: italic;">/* y now equals 5 + 10 */</span></pre></div></div>

<p>These symbols in PHP are known as <dfn>variables</dfn> and require you to follow a specific syntax to use them properly. Once you understand the principles behind them, you&#8217;ll begin to see how they form a major component of any PHP application, and can be used to store many things other than just numbers.  Let us begin though, by guiding you through a few simple examples of the use of variables.</p>
<h3>A simple introduction</h3>
<p>A very simple example of the use of a variable in PHP is to hold just one number, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span></pre></div></div>

<p>All variables in PHP start with a dollar sign, followed by a string of numbers, letters (both lower-case and capitals) and underscores. Variables are also case sensitive, meaning that $Variable is <em>not equal to</em> $variable. The only other requirement of variable naming is that the first digit cannot be a number. These are examples of valid variable names</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$variable_name</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//valid - integer</span>
<span style="color: #000033;">$VariableName</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">25.5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//valid - floating point</span>
<span style="color: #000033;">$variable3</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">155</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//valid</span>
<span style="color: #000033;">$_variable_number_four</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//valid - string</span>
<span style="color: #000033;">$_x</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;five&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//valid</span></pre></div></div>

<p>These are not valid variable names</p>

<div class="wp_syntax"><div class="code"><pre class="php">$1st_variable <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//invalid</span>
<span style="color: #000033;">$Variable</span><span style="color: #339933;">.</span>name <span style="color: #339933;">=</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//invalid, cannot have dots in names</span></pre></div></div>

<p>Variables are assigned values through the use of the equals sign (=) operator. Once a variable has been assigned to a value, that variable symbol can be used in place of the value anywhere in the code. The value of the variable can also be changed at any point during the execution of the code. The same value can also be assigned to multiple variables at the same time, like so</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;$a, $b, $c&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This produces the output</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span></pre></div></div>

<h3>Flexible Types</h3>
<p>The &#8220;type&#8221; of a variable is defined by the value you&#8217;ve assigned to it. PHP supports a wide range of types, including strings, integers and floats.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$integer</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//an integer value;</span>
<span style="color: #000033;">$float</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4.55</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//a &quot;floating point&quot; value</span></pre></div></div>

<p>PHP doesn&#8217;t explicitly require that you maintain the same type for a certain variable though. If you tried to assign a string value to a variable which previously contained an integer, you&#8217;ll encounter no problems. Similarly with any other types, floating point variables can hold integers or strings and so on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/variables-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Conditional Statements in PHP</title>
		<link>http://www.roughguidetophp.com/conditional-statements-in-php/</link>
		<comments>http://www.roughguidetophp.com/conditional-statements-in-php/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 19:52:13 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=17</guid>
		<description><![CDATA[
Conditional statements are used in PHP (and in any other programming language) when the programmer wishes to place a branch in the code, where a specific result will occur if, and only if, a set of conditions is matched. A real world example might be
I will go to the cinema if I have $15 or [...]]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:An introduction to If, Else, Switch and Elseif--><br />
Conditional statements are used in PHP (and in any other programming language) when the programmer wishes to place a branch in the code, where a specific result will occur if, and only if, a set of conditions is matched. A real world example might be</p>
<p><b>I will go to the cinema if I have $15 or more to spend tonight</b></p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span>  <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I'm going to the cinema&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-17"></span><br />
But, what would you do if you didn&#8217;t go to the cinema? There&#8217;s usually an alternative&#8230;</p>
<p><b>I will go to the cinema if I have $15 or more. Otherwise, I&#8217;ll just stay in and watch TV</b></p>
<p>This would be represented in PHP as</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span>  <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I'm going to the cinema&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I'm a bit skint. I think I'll just stay in and watch Friends re-runs again&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first line sets up the proposition, &#8220;What&#8217;s the value of $amount?&#8221; or, &#8220;How much money have I got?&#8221;. The curly brackets (or braces) are the containers for each of the outcomes (this is an important syntactical feature of PHP).</p>
<p>What we&#8217;re saying here is that the echo statement contained within the first pair of braces (line 2) will be executed if the value of $amount is <i>greater than or equal to (>=)</i> 15</p>
<p>The statement within the second set of braces (line 4) is executed when $amount is <i>less than</i> 15. The &#8220;else&#8221; operator tells PHP that we want one or the other - not both - dependant on the outcome of the &#8220;if&#8221; statement</p>
<h3>True &#038; False</h3>
<p>PHP, much like any other language, uses the two boolean values TRUE and FALSE to represent the outcome of logical expressions. The result of a statement like &#8220;$amount >= 15&#8243; will equal TRUE if the variable $amount holds a numerical value which is greater than, or equal to 15 and FALSE otherwise. Here are some other examples of boolean expressions</p>

<div class="wp_syntax"><div class="code"><pre class="php">  <span style="color: #cc66cc;">5</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;">//TRUE, 5 is greater than 3</span>
  <span style="color: #cc66cc;">3</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span> <span style="color: #666666; font-style: italic;">//TRUE, 3 is less than 5</span>
  <span style="color: #cc66cc;">4</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">4</span> <span style="color: #666666; font-style: italic;">//TRUE, 4 is indeed greater than or &lt;i&gt;equal to&lt;/i&gt; 4</span>
  <span style="color: #cc66cc;">5</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;">//TRUE, 5 is &lt;i&gt;not equal to (!=)&lt;/i&gt; 3</span>
  <span style="color: #cc66cc;">3</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;">//FALSE, 3 is equal to 3 - the operand &quot;!=&quot; fails here</span>
  <span style="color: #cc66cc;">5</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">5</span> <span style="color: #666666; font-style: italic;">//TRUE, 5 does indeed equal 5</span></pre></div></div>

<p>These are fairly banal examples. I can&#8217;t imagine anyone would get excited about writing a program to prove that 5 equals 5!</p>
<h3>Equality operator (==) &#038; Assignment operator (=)</h3>
<p><b>An important thing to note</b> about the last example is the use of equality operator (==) This is <i>not the same as</i> the assignment operator (=) <b>==</b> is used to check if two values are equal, whereas <b>=</b> is used to assign a specific value to a variable. i.e.</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// $x now equals 5</span>
<span style="color: #000033;">$x</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">/* 7 doesn't get assigned to $x
 instead, the whole expression equates to false
 since $x doesn't equal 7 */</span></pre></div></div>

<p>We can see this more clearly by looking at the <i>value of each expression as a whole</i> using var_dump()</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #990000;">var_dump</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* int(5) */</span>
<span style="color: #990000;">var_dump</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$x</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">/*  bool(false)  */</span></pre></div></div>

<h3>AND &#038; OR</h3>
<p>Sometimes you want to base the outcome of a situation on more than one variable, you wish to know, for example, that two certain conditions will be fulfilled before deciding on a choice of action. Expanding on our cinema example from earlier -</p>
<p><b>If I have $15 or more <i>AND</i> it is not raining, I&#8217;ll go to the cinema. Otherwise I&#8217;ll stay in and watch TV</b></p>
<p>becomes&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">15.00</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000033;">$raining</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I really want to see the new Indiana Jones film&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;It's too wet out, and the episode of Friends where they play 'Fireball' is on&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><i>Here, we are using the operator &#8220;&#038;&#038;&#8221; to represent the expression &#8220;and&#8221; - similarly, we can use the operator &#8220;||&#8221; to represent &#8220;or&#8221;.</i></p>
<p>This statement is based on two things - firstly, the same condition as before - how much money have I got?  Secondly, the condition &#8220;!$raining&#8221;. The exclamation mark before $raining is means that !$raining will equate to TRUE only if $raining equals FALSE. Confusing? Yes, it is a little bit to begin with, but dealing with logical expressions usually can be!</p>
<p>Think of it this way - it can be rewritten as</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">15.00</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000033;">$raining</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></pre></div></div>

<p>Which makes more sense. You may encounter quite a few situations like this in PHP, where things are written in a specific shorthand that somewhat obscures the meaning, but once you get the hang of it you find it quicker and simpler to use the shorthand version. Take the following code snippet&#8230;</p>
<h3>Elseif</h3>
<p>Sometimes it might be the case that there aren&#8217;t just two potential outcomes to a situation. Indeed, as with most scenarios in life, there are many more. Consider yet another modifcation to the above scenario.</p>
<p><b>I&#8217;m going to the cinema if it&#8217;s not raining, and I have $15 or more. If it is raining, I&#8217;ll stay in and watch TV. Otherwise, if it isn&#8217;t raining, but I have less than $15, I&#8217;ll go and visit my friends</b></p>
<p>Here, we have three unique outcomes - going to the cinema, staying in, or visiting friends. We can work this out with logic by introducing another choice in our if statement, as follows</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">15.00</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000033;">$raining</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Let's go to the cinema&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">15.00</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000033;">$raining</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I'm going to visit friends&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">15.00</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000033;">$raining</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I think I'll just stay in tonight&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Notice though, in the final case, we&#8217;re doing checks for logical conditions that aren&#8217;t actually required, given that we can reason, through logical deduction, that if it&#8217;s raining, neither of the first cases will match, so the check for $raining == TRUE is redundant. Likewise, because we know that the value of $amount isn&#8217;t relevant either, we can safely ignore that too. A more compact way of writing the above code would be</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">15.00</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000033;">$raining</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Let's go to the cinema&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">15.00</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000033;">$raining</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I'm going to visit friends&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I think I'll just stay in tonight&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Usually, for the final condition in an if&#8230;elseif&#8230;else statement, we have already checked for all the relevant conditions we&#8217;re interested in, and this final &#8220;else&#8221; is simply the &#8220;default case&#8221;</p>
<h3>Conditional Shorthand using ? and :</h3>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #990000;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span> <span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">15</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000033;">$raining</span><span style="color: #009900;">&#41;</span>?<span style="color: #0000ff;">&quot;I'm going to the cinema&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;I'm going to stay in&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now what on earth is all <i>that</i> about?  Well, it&#8217;s exactly the same expression as before - only written in an even more shorthand way.  This time, we&#8217;re using the conditional operators ? and : to express our situation.</p>
<p>An interesting way to view this is to think of the ? in the statement above in <i>exactly the same way</i> as you would view it in a normal sentence.  Let&#8217;s reword our thoughts about going to the movies&#8230;</p>
<p><b>Is it dry outside, and do I have more than $15? If so, then I&#8217;ll head out. If not, then I&#8217;ll stay in</b></p>
<p>We&#8217;re asking a question here - the same question in PHP form reads..</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$amount</span> <span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">15</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000033;">$raining</span>?<span style="color: #666666; font-style: italic;">/* if so */</span><span style="color: #0000ff;">&quot;I'm not going to the cinema&quot;</span><span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">/* otherwise */</span><span style="color: #0000ff;">&quot;I am going to the cinema&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p><i>(I added in some extra wording in comments to make it clearer - that might not be fully accepted as valid PHP code though)</i></p>
<p>Here, we&#8217;re using the colon (:) to mean &#8220;Otherwise&#8221;, so the use of the ? and : operators basically amounts to</p>

<div class="wp_syntax"><div class="code"><pre class="text">  (statement which equals either TRUE or FALSE)?Do this if it's true:Do this if it's false;</pre></div></div>

<p>If you think about it, you can also imagine how you could place other conditional statements within the expressions to be evaluated in either case, to produce even more complex scenarios</p>
<p>Let&#8217;s expand again on our cinema example</p>
<p><b>I&#8217;m going to the cinema if it&#8217;s not raining, and I have $15 or more. Otherwise I&#8217;ll stay in and watch TV. But, if my friends call me up to go along, I&#8217;ll head anyway</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">15.00</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000033;">$raining</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000033;">$friends_have_called</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Let's go to the cinema&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Nah, let's not bother&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here, the conditional statement in line 1 has expanded to include an &#8220;or&#8221; (||) condition - either, it&#8217;s not raining and I have enough money, or, my friends have called. If my friends have called, it doesn&#8217;t matter about the first expression.</p>
<p>Note as well, that the first condition has been placed in brackets, instead of writing</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$amount</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">15.00</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000033;">$raining</span> <span style="color: #339933;">||</span> <span style="color: #000033;">$friends_have_called</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></pre></td></tr></table></div>

<p>Which is prone to misinterpretation (That could be saying &#8220;If I have more than $15, <b>and</b> it&#8217;s either not raining or my friends have called&#8230;&#8221;) There is a feature of programming called <i>operator precedence</i> which defines which operator is considered first, but when in doubt, always use brackets to make your statements clearer - it helps when other people look at your code too</p>
<h3>Switch Statements</h3>
<p>One other way of providing a conditional response to an expression is to use the switch() function. This function is especially useful when we expect one of a fixed number of outcomes to occur, and we wish to compare a specific variable to each, to find out which value has occured.</p>
<p>The switch() function also uses the keywords case, default and break, and the exact purpose of these can be seen in the example below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php"><span style="color: #000033;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;i is equal to 2&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;i is three&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;i is only one here&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;i is neither 1, 2 or 3&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The above code will output</p>

<div class="wp_syntax"><div class="code"><pre class="text">i is equal to 2</pre></div></div>

<p>Here, we are using the case keyword to declare which of the outcomes 1, 2 or 3 we wish to deal with in each case. The switch statement itself is encased within a pair of braces (lines 2 &#038; 15) and each case statement is simply defined by whatever code appears between the colon (:) and the break; statement. </p>
<p>The final case is named &#8220;default&#8221;, and this is the case which occurs when none of the previous cases is matched. The usage here is similar to the last sentence I wrote in the section above about &#8220;elseif&#8221; - the last default case is usually the one that requires no extra conditions to be matched.</p>
<p>This switch statement above is identical in functionality to the series of if statements:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php"><span style="color: #000033;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$i</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;i is equal to 2&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$i</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;i is three&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$i</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;i is only one here&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;i is neither 1, 2 or 3&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>But the switch statement is arguably more compact, and a bit simpler to code than the equivalent code using &#8220;if&#8221;</p>
<h3>Footnote : An alternate syntax for If&#8230;else statements</h3>
<p>if&#8230;else statements can also be written using the following syntax</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
   <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$a</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000033;">$b</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Statement 1&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$a</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Statement 2&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Statement 3&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//can be rewritten as...</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$a</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000033;">$b</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Statement 1&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$a</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Statement 2&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Statement 3&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Note that, in the second code example, there is no need for any braces round each of the results. All the code to be executed based on the logic expression is contained between each of the if:, elseif: and else: statements</p>
<p>Note as well, it&#8217;s vitally important to include the &#8220;endif;&#8221; statement, to replace the final closing brace.</p>
<p><b>Switch</b> statements can also be written as</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
  <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;one&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;not one&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endswitch</span><span style="color: #339933;">;</span></pre></div></div>

<p>And similar syntactical differences can be applied to <dfn>while</dfn> loops, <dfn>for</dfn> loops and <dfn>foreach</dfn> loops</p>
<p>This way of writing if statements can be useful, and slightly tidier, but is slightly more prone to errors, especially when nesting if statements, and can sometimes produce erratic results</p>
]]></content:encoded>
			<wfw:commentRss>http://www.roughguidetophp.com/conditional-statements-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Loops in PHP : While and For</title>
		<link>http://www.roughguidetophp.com/loops-in-php-while-and-for/</link>
		<comments>http://www.roughguidetophp.com/loops-in-php-while-and-for/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 07:03:31 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://localhost/devblog/?p=30</guid>
		<description><![CDATA[Blah blah blah etc.

This is a test]]></description>
			<content:encoded><![CDATA[<p><!--pagetitle:An introduction to For and Foreach Loops in PHP--></p>
<h3>Introduction to Loops</h3>
<p>Loops in PHP are very useful when you need to perform repetitions of the same code over and over again, or when you need to ensure that you iterate through all possible values of an array, for example. They come in a few different flavours, the most common being &#8220;for&#8221; loops and &#8220;while&#8221; loops. There are a few other more obscure ways to create loops, and I&#8217;ll cover them at the end, in the extended section.</p>
<p>The three types of loop I will cover in this section are <b>For loops, While loops and Foreach loops</b></p>
<h3>While loops</h3>
<p>While loops are the simplest kind of loop in PHP. They&#8217;re pretty much just saying &#8220;Do something for a while until a certain condition is satisfied&#8221;. You could be saying <strong>Stay at work until this project is finished</strong> or <strong>Keep on running this marathon until you get to the end</strong></p>
<p>The general syntax of a while loop is as follows</p>

<div class="wp_syntax"><div class="code"><pre class="text">while(condition = true) {
  do something
}</pre></div></div>

<p>A real-world code example of this might be</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php"><span style="color: #000033;">$miles_completed</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$race_finished</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$race_finished</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000033;">$miles_completed</span><span style="color: #339933;">++;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$miles_completed</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">26</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000033;">$race_finished</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;ll explain some of the symbols and operations going on here&#8230;</p>
<ul>
<li><b>Lines 1 and 2</b> : we initialise the values of $miles_completed and $race_finished. The code wouldn&#8217;t work properly otherwise, as $race_completed has to be equal to FALSE initially to satisfy the while loop in line 3</li>
<li><b>Line 3</b> : The while loop will repeat as long as $race_finished is equal to false. A drawback of while loops is that they can loop indefinitely if you are not careful, and this will cause your application to crash - you can see how this would occur if we didn&#8217;t increment the value of $miles_completed or set $race_completed to be true. You&#8217;d be running that marathon for ever!</li>
<li><b>Line 4 </b>: the expression $miles_completed++; is a way of saying $miles_completed = $miles_completed+1; This can also be rewritten as $miles_completed += 1;</li>
<li><b>Line 5 </b>: we check the value of $miles_completed on every repetition of the loop. Once $miles_completed becomes greater than 10 (since we are incrementing the value every time round) then $rage_finished is set to TRUE, therefore breaking the condition that keeps the loop going</li>
</ul>
<p>Another way of writing while loops is to invoke the &#8220;do&#8221; statement</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #000033;">$miles_completed</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$race_finished</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">do</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000033;">$miles_completed</span><span style="color: #339933;">++;</span>
  <span style="color: #b1b100;">if