<?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>Ruelicke.net &#187; PHP</title>
	<atom:link href="http://www.ruelicke.net/category/tutorial/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ruelicke.net</link>
	<description>...when coding passion, gaming addiction, Internet surfing syndrome and the real life collide...</description>
	<lastBuildDate>Mon, 05 Apr 2010 17:11:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP Tutorial 102: Manipulating Variables and doing Mathematics</title>
		<link>http://www.ruelicke.net/2007/04/28/php-tutorial-102-variables-mathematics/</link>
		<comments>http://www.ruelicke.net/2007/04/28/php-tutorial-102-variables-mathematics/#comments</comments>
		<pubDate>Sat, 28 Apr 2007 18:27:21 +0000</pubDate>
		<dc:creator>Marco</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Websites]]></category>

		<guid isPermaLink="false">http://blog.ruelicke.net/2007/04/28/php-tutorial-102-variables-mathematics/</guid>
		<description><![CDATA[Now, after we learned how to show content and also had a first glance at variables, we will now go a step further. To define a variable in PHP you just need to use $ and a variable name: $variable. You can use as variable name whatever you like, however you are not allowed to [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.ruelicke.net/wp-content/themes/ruelicke/images/icon-php.png" alt="PHP - Hypertext PreProcessor" title="PHP - Hypertext PreProcessor" class="alignleft" />Now, after we learned <a href="http://www.ruelicke.net/2007/04/php-tutorial-101-hello-world/">how to show content</a> and also had a first glance at variables, we will now go a step further.</p>
<p>To define a variable in PHP you just need to use $ and a variable name: <code>$variable</code>.<br />
You can use as variable name whatever you like, however you are not allowed to let it start with a number, like <code>$1variable</code> and there are also some predefined variables, about which I will talk later.</p>
<p>There are many types of variables, but for now we concentrate on 4 types (actually 5, but the fifth will follow in another tutorial).<span id="more-13"></span></p>
<p>So, which 4 types do we have?</p>
<p><strong>string</strong><br />
e.g. <code>$string = "This is a string";</code></p>
<p><strong>boolean</strong><br />
e.g. <code>$boolean = True;</code></p>
<p><strong>integer</strong><br />
e.g. <code>$integer = 123456;</code></p>
<p><strong>float</strong><br />
e.g. <code>$float = 1.234;</code></p>
<p>so, let&#8217;s make a script where can play a bit with those variables:</p>
<p><code>&lt;?PHP<br />
$string= "This is a string"; // String<br />
$string_alt= 123456; // String<br />
$boolean= True; // Boolean<br />
$integer= 123456; // Integer<br />
$float= 1.234; // Float</code></p>
<p><code>print 'The variable "$string" has the type of: '.gettype($string).'&lt;br /&gt;';<br />
print 'The variable "$string_alt" has the type of: '.gettype($string_alt).'&lt;br /&gt;';<br />
print 'The variable "$boolean" has the type of: '.gettype($boolean).'&lt;br /&gt;';<br />
print 'The variable "$integer" has the type of: '.gettype($integer).'&lt;br /&gt;';<br />
print 'The variable "$float" has the type of: '.gettype($float);<br />
?&gt;</code></p>
<p>Run this script and the gettype() function will tell you which type a variable has.<br />
If you have a look at the output, you will see a mistake. Isn&#8217;t &#8220;$string_alt&#8221; supposed to have the type &#8220;string&#8221;?<br />
Yes, it is supposed to be a string, however, PHP does not know that your number shall be a string. So how can fix this problem?<br />
It is quite easy, just add <code>(string)</code> infront of it, so it looks like <code>$string_alt= (string) 123456; // String</code>.</p>
<p>Everything should be fine now, you could even turn the string back to integer, just add (integer) again:<br />
<code>&lt;?PHP<br />
$string_alt= (string) 123456; // String</code></p>
<p><code>print 'The variable "$string_alt" has the type of: '.gettype($string_alt).'&lt;br /&gt;';</code></p>
<p><code>$string_alt = (integer )$string_alt; // convert to Integer<br />
print 'The variable "$string_alt" has the type of: '.gettype($string_alt);<br />
?&gt;</code></p>
<p>What we did here is called &#8220;type casting&#8221; and it is always useful if you have script where you need to change the variable type to prepare it for a special function or whatever you plan to do with it. You can find a complete list of the type casting in the <a href="http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting">PHP documentation at the official website.</a></p>
<p>Type casting is not the only thing you can do variables. You can also add $variable1 at the end (or beginning) of $variable2:</p>
<p><code>&lt;?PHP<br />
$start = "The little brown fox ";<br />
$end = "jumped over the green fence.";</code></p>
<p><code>$start_merge = $start;<br />
$start_merge .= $end; // adds behind the existing content</code></p>
<p><code>print 'Start sentence: '.$start.'&lt;br /&gt;';<br />
print 'End sentence: '.$end.'&lt;br /&gt;&lt;br /&gt;';<br />
print 'Correct sentence: '.$start_merge.'&lt;br /&gt;';<br />
print 'Correct sentence: '.$start.$end.'&lt;br /&gt;';<br />
?&gt;</code></p>
<p>As you will see our sentence will be shown in different ways. So let us have a closer look at the code itself:<br />
The &#8220;.=&#8221; means, that the variable which shall be added to another one will be added at the end of the other variable<br />
The &#8220;$start.$end&#8221; looks, familiar, doesn&#8217;t it? I mentioned it in <a href="http://www.ruelicke.net/2007/04/php-tutorial-101-hello-world/">PHP Tutorial 101: Hello World!</a> and the same behavior you see there also happens here.</p>
<p>Before I go on with some mathematical things we can do, I will tell you about the &#8220;//&#8221; I added to the code because I assume you are wondering what it means <img src='http://www.ruelicke.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
The double-slashes only exclude a comment of the code. Beside the &#8220;//&#8221; you can also use &#8220;/* comment */&#8221; and &#8220;#&#8221;<br />
The &#8220;//&#8221; comment will end either at the end of the line of code where it is placed; Everything on the same line and behind of the slashes belong to the comment.<br />
The &#8220;/* comment */&#8221; starts at &#8220;/*&#8221; and ends at &#8220;*/&#8221; no matter if there are multiple lines of code or not.<br />
The &#8220;#&#8221; comment behaves like the &#8220;//&#8221; comment.</p>
<p>So, as we have this explained, I&#8217;m able to use more comments in the code and avoid longer &#8220;outside of code&#8221; explanations <img src='http://www.ruelicke.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Now let&#8217;s do some mathematics!</p>
<p>With PHP (as with any other programming language) you are able to make all kind mathematic calculations. For this tutorial, I only stick to the most basic mathematic functions and refer you to the <a href="http://php.net/manual/en/ref.math.php">PHP Manual: Mathematical Functions</a></p>
<p>We will use the following script as a start for some calculations:<br />
<code>&lt;?PHP<br />
$a = 5;<br />
$b = 2.3;<br />
$c = -6;<br />
$d = 100;</code></p>
<p><code>$ad = $a + $d; // Adds $d to $a<br />
$da = $a - $d; // Subtracts $d from $a<br />
$bc = $b / $c; // Divides $b through $c<br />
$dc = $d * $c; // Multiplies $b with $c</code></p>
<p><code>print $a.' + '.$d.' = '.$ad.'&lt;br /&gt;';<br />
print $a.' - '.$d.' = '.$da.'&lt;br /&gt;';<br />
print $b.' / '.$c.' = '.$bc.'&lt;br /&gt;';<br />
print $d.' x '.$c.' = '.$dc.'&lt;br /&gt;';<br />
?&gt;</code></p>
<p>We just did the basic mathematic calculations you and I think it is self-explanatory, so let&#8217;s have a look how we can get a random number, divide it by another random number and make sure the result is rounded to 2 decimal places, if it is a floating number:<br />
<code>&lt;?PHP<br />
$random_a = rand(1,1000); // get a random number between 1 and 1000<br />
$random_b = rand(500,1500); // get a random number between 500 and 1500</code></p>
<p><code>$random = $random_a / $random_b;<br />
$random_round = round($random,2); // rounds result to 2 decimal places</code></p>
<p><code>print 'First random number is: '.$random_a.'&lt;br /&gt;<br />
Second random number is: '.$random_b.'&lt;br /&gt;<br />
Result is: '.$random.'&lt;br /&gt;<br />
Rounded result is: '.$random_round;<br />
?&gt;</code></p>
<p>Of course there are way more things you can do, but I think this is enough for mathematics. I recommend you have a look at the <a href="http://php.net/manual/en/ref.math.php">PHP Manual: Mathematical Functions</a> and play around because it is proved that you learn much more by the &#8220;Trial and Error&#8221; concept that any other Tutorial.<br />
A tutorial is always a start, but you really learn something by doing it on your own. So use the time to the next tutorial by training what you have learned. <img src='http://www.ruelicke.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><ins datetime="2007-07-29T20:49:05+00:00">Update:</ins> <a href="http://www.ruelicke.net/example/php/tutorial102.php">Here you see a page with the results of the code used in this tutorial</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ruelicke.net/2007/04/28/php-tutorial-102-variables-mathematics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Tutorial 101: Hello World!</title>
		<link>http://www.ruelicke.net/2007/04/23/php-tutorial-101-hello-world/</link>
		<comments>http://www.ruelicke.net/2007/04/23/php-tutorial-101-hello-world/#comments</comments>
		<pubDate>Mon, 23 Apr 2007 12:21:12 +0000</pubDate>
		<dc:creator>Marco</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Websites]]></category>

		<guid isPermaLink="false">http://blog.ruelicke.net/2007/04/23/tutorial-hello-world/</guid>
		<description><![CDATA[So, here it is, my first tutorial ever! I will try to explain how to create your first PHP &#8220;script&#8221;. Requirements for the PHP Tutorials: - at least good knowledge about HTML - Webspace or local server with PHP Support - Additional: knowledge about CSS &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; Well, let&#8217;s start with the script itself, I will [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.ruelicke.net/wp-content/themes/ruelicke/images/icon-php.png" alt="PHP - Hypertext PreProcessor" title="PHP - Hypertext PreProcessor" class="alignleft" />So, here it is, my first tutorial ever!<br />
I will try to explain how to create your first PHP &#8220;script&#8221;.</p>
<p>Requirements for the PHP Tutorials:<br />
- at least good knowledge about HTML<br />
- Webspace or local server with PHP Support<br />
- Additional: knowledge about CSS<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>Well, let&#8217;s start with the script itself, I will explain everything later:<br />
<code><br />
&lt;?PHP<br />
echo "Hello World!&lt;br /&gt;";<br />
print 'Hello World!&lt;br /&gt;';</code></p>
<p><code>echo "&lt;a href=\"http://www.ruelicke.net\"&gt;Hello World link&lt;/a&gt;&lt;br /&gt;";<br />
print '&lt;a href="http://www.ruelicke.net"&gt;Hello World link&lt;/a&gt;';<br />
?&gt;</code></p>
<p>Create a HTML file, insert the code above into the &lt;body&gt; and save it. Then rename the HTML file from foo.html to foo.php<br />
Upload the file and access it.</p>
<p>If everything is working, then you should see &#8220;Hello World&#8221; (without the quotes) 4 times. Congratulations, looks like you just wrote your first PHP script.<br />
<span id="more-5"></span>But what happens there?<br />
Well, I don&#8217;t know if you remember <a href="http://en.wikipedia.org/wiki/MS-DOS">MS-DOS</a> or if you ever used it <img src='http://www.ruelicke.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
Anyway, in DOS there were two commands, named <a href="http://www.computerhope.com/echohlp.htm">ECHO</a> and <a href="http://www.computerhope.com/printhlp.htm">PRINT</a>. Basically you can say that using either of the commands could return a string input to the screen or to a peripheral (e.g. a printer).<br />
In PHP it means almost the same, except both commands return a string to the screen/website.</p>
<p>Now let&#8217;s have a closer look at the script itself:</p>
<p><code>&lt;?PHP<br />
[...]<br />
?&gt;</code></p>
<p>Every PHP-Script has to start and end with these two lines.<br />
It tells the server that this script has to be processed before it can be viewed by the visitor.<br />
You could use &#8220;&lt;?&#8221; and &#8220;?&gt;&#8221; only, but to avoid future compatibility problems, we use &#8220;&lt;?PHP&#8221; as starting and &#8220;?&gt;&#8221; as closing tag.</p>
<p>Hm&#8230;didn&#8217;t I say something about it has to be processed by the server before you can see the &#8220;Hello World&#8221;? Well, PHP actually means &#8220;Hypertext Preprocessor&#8221;, you could say that the server reads the PHP code and turns it into something you can see. To avoid making this tutorial too boring I just refer to <a href="http://en.wikipedia.org/wiki/PHP">Wikipedia&#8217;s PHP entry</a>. Maybe I will write a short article about the history of PHP at a later date, but we will see <img src='http://www.ruelicke.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Now let&#8217;s go on&#8230;</p>
<p><code>echo "Hello World!&lt;br /&gt;";<br />
print 'Hello World!&lt;br /&gt;';</code></p>
<p>I assume you see that I used double-quotes for the echo and single-quotes for the print function. Actually it does not really matter, which kind of quote you use for both, as long as the starting and the ending quote are the same AND the function is closed with a semicolon.<br />
Maybe you wonder why I used the different quotes, of course I have a reason&#8230;</p>
<p>Let&#8217;s assume you want to have &#8220;Hello World&#8221; as a link to your own website:<br />
Would you use the echo with the double-quotes you would either get into problems or your HTML would not validate.<br />
Neither is something we want to happen, so we have to do something special, if not annoying if you have a lot of code where you need to do it:</p>
<p><code>echo "&lt;a href=\"http://www.ruelicke.net\"&gt;Hello World link&lt;/a&gt;&lt;br /&gt;";</code></p>
<p>As you see I &#8220;commented&#8221; the double-quote which is used after &#8220;href=&#8221; with a backslash. Remove those slashes and run your script, I&#8217;m pretty sure you will run into a PHP bug. Of course, you could replace the double-quotes you use for the echo by single-quotes and everything is fine, but since I prefer using the print function (as it actually prints the string to the screen/website) I use it with single-quotes and have no problems at all:</p>
<p><code>print '&lt;a href="http://www.ruelicke.net"&gt;Hello World link&lt;/a&gt;';</code></p>
<p>Well, I assume you still see no difference between &#8220;echo&#8221; and &#8220;print&#8221;, so let&#8217;s go on with your &#8220;Hello World&#8221; script and use some variables:</p>
<p><code>&lt;?PHP<br />
$message = "Hello World!";</code></p>
<p><code>echo "Message of the day: $message &lt;br /&gt;";<br />
print 'Message of the day: '.$message.'&lt;br /&gt;';<br />
?&gt;</code></p>
<p>This script returns the variable &#8220;$message&#8221;, which we gave the content &#8220;Hello World!&#8221;.<br />
Note that all variables in PHP have to start with the dollar symbol: $ and the line also has to be ended with the semicolon.<br />
I assume you looked carefully at the &#8220;echo&#8221; and the &#8220;print&#8221; code, and you spotted the difference.<br />
As you see I just wrote the &#8220;$message&#8221; right into the echo quotes. When using &#8220;echo&#8221;, it will return variables right away, so you don&#8217;t need to write some extra code or so&#8230;<br />
Well, since a friend told me it would be better (and easier) to use &#8220;print&#8221;, I prefer the &#8220;print&#8221; function. I don&#8217;t know if you listen to me, but I recommend you use &#8220;print&#8221;. Anyway, it is your decision which function you use.<br />
If you use &#8220;print&#8221;, please note that you need to insert the variable by inserting it with &#8220;quote dot variable dot quote&#8221;: &#8216;.$variable.&#8217; except the variable is at the beginning or the end of the print function:</p>
<p><code>print 'some string '.$variable.' some more string';<br />
print $variable.' some string';<br />
print 'some string'.$variable;</code></p>
<p><ins datetime="2007-07-29T20:40:44+00:00">Update:</ins> <a href="http://www.ruelicke.net/example/php/tutorial101.php">Final example of this tutorial</a></p>
<p>I hope you understood everything so far, in <a href="http://www.ruelicke.net/2007/04/php-tutorial-102-variables-mathematics/">the next Tutorial 102</a> we will play with some variables<del datetime="2007-04-28T18:27:51+00:00">, so stay tuned for updates</del>.<br />
If you have any questions, feel free to comment, I will try to answer as fast as possible <img src='http://www.ruelicke.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.ruelicke.net/2007/04/23/php-tutorial-101-hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
