<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dan.forys.co.uk &#187; variables</title>
	<atom:link href="http://dan.forys.co.uk/tag/variables/feed/" rel="self" type="application/rss+xml" />
	<link>http://dan.forys.co.uk</link>
	<description>Dan is a web developer in London. He is interested in all things Internet, Linux and Mac.</description>
	<lastBuildDate>Fri, 22 Jan 2010 14:25:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Learning PHP â€“ Part 2: Variable Basics</title>
		<link>http://dan.forys.co.uk/learning-php-part-2-variable-basics/</link>
		<comments>http://dan.forys.co.uk/learning-php-part-2-variable-basics/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 19:53:24 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://www.danforys.com/?p=72</guid>
		<description><![CDATA[After part 1, you should be familiar with the echo statement, which outputs a string to the browser: echo 'This is a string'; (Note that from now on the opening (&#60;?php) and closing (?&#62;) PHP tags are omitted from examples, but should still be included in any source code you create) On its own, it [...]]]></description>
			<content:encoded><![CDATA[<p>After <a href="http://www.danforys.com/2009/10/24/learning-php-part-1-introduction/">part 1</a>, you should be familiar with the echo statement, which outputs a string to the browser:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span></pre></div></div>

<p>(Note that from now on the opening (&lt;?php) and closing (?&gt;) PHP tags are omitted from examples, but should still be included in any source code you create)</p>
<p>On its own, it does not appear to be useful; after all we could have just written &#8216;This is a string&#8217; in HTML for the same effect. The real power comes from using PHP variables.<br />
<span id="more-72"></span></p>
<blockquote><p>
Think of a variable as a named box, in which you can place something. Later, you can retrieve the thing you put in the box, you just look it up by its name.</p></blockquote>
<h3>Assigning values to variables</h3>
<p>Creating a variable is as easy as giving it a name and telling it what value you want it to store. In PHP, variables always begin with a dollar sign ($)</p>
<blockquote><p>
A variable name must begin with a letter, or an underscore. The rest of the name can be any combination of letters, numbers and/or underscores.
</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span></pre></div></div>

<p>This creates a new variable with the name of &#8216;$string&#8217; and stores the value &#8216;This is a string&#8217; inside it. You can combine the echo statement with a variable to output the contents of a variable to the browser:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will output &#8216;This is a string&#8217; as with the first example.</p>
<p>Any subsequent values you set will overwrite the previously stored value:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This overwrites the previous value'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 'This overwrites the previous value'</span></pre></div></div>

<p>Here we&#8217;ve introduced a style of PHP comment. Comments are totally ignored by PHP making them useful for documenting your code. I&#8217;ve used the &#8216;//&#8217; comment, where PHP will ignore everything up to the end of the line.</p>
<h3>Variable types</h3>
<p>In PHP, variables can be one of several types. These types affect what operations you can perform on the variable and how the values behave. Three basic variable types are listed below.</p>
<h4>Strings</h4>
<p>Strings are collections of characters. You denote strings by enclosing them in single or double quotes:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a single quoted string'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a double quoted string&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are some differences with how single and double quoted strings are interpreted &#8211; we&#8217;ll talk about that later.</p>
<p>You can add (concatenate in PHP-speak) two strings together using a period &#8216;.&#8217;:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This string '</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'is concatenated from two parts'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 'This string is concatenated from two parts';</span></pre></div></div>

<p>You can also do this with two variables containing strings:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This string '</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'is concatenated from two parts'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$string1</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$string2</span> <span style="color: #666666; font-style: italic;">// same result as above</span></pre></div></div>

<h4>Integers</h4>
<p>Integers are whole numbers. A whole number is one without a decimal point.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$integer</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">42</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$integer</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 42</span></pre></div></div>

<p>You can perform mathematical operations on integers:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$integer1</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$integer2</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$integer1</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$integer2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 30</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$integer1</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$integer2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 70</span></pre></div></div>

<h4>Floating point numbers</h4>
<p>Floating point numbers are numbers with a decimal point. In practice, they work similarly to integers:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$float1</span> <span style="color: #339933;">=</span> <span style="color:#800080;">10.5</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$float2</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.2</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$float1</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$float2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 10.3</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$float1</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$float2</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs 10.7</span></pre></div></div>

<p>Note that floating point numbers have a very important gotcha:</p>
<blockquote><p>
Inside the computer memory, floating point numbers are not stored as exact representations. This means rounding errors can occur. For this reason, never use floating point numbers where accuracy is important; such as in financial transactions for example.
</p></blockquote>
<h3>Other types</h3>
<p>There are several other types of variable in PHP. The other types are slightly more advanced and will be introduced later as we need them.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.forys.co.uk/learning-php-part-2-variable-basics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 2.282 seconds -->
