Access_Denied's Forums

Site Resources => Tutorials => Topic started by: Access_Denied on August 16, 2007, 09:13:34 AM



Title: Tutorial Two: Variables
Post by: Access_Denied on August 16, 2007, 09:13:34 AM
Just like in C, or in any other language, a variable is a word that takes place of another word or number. So, if I wrote 'x = 3', instead of writing 3, I could just write 'x'. Anyway, making a variable in PHP is extremely simple. So, here's our sample code for the lesson.

<?php
$StartingMoney = 184;
$PriceOfItem = 135;
$MoneyLeft = $StartingMoney - $PriceOfItem;
$Currency = " Dollars";
echo "I have ".$MoneyLeft.$Currency;
?>

Now, our first four lines were just declaring variables. A variable has to start with a dollar sign, then the variable name must start with a letter or underscore. Also, strings (or words) must be surrounded by quotes, like in our $Currency variable. Now, in our $MoneyLeft line, we subtracted a value from another one, just like in math. Simple, right? And, in the echo line, we printed it all out. First we printed the words, "I have", then we printed our $MoneyLeft variable, then we printed our $Currency variable. Note that the period is used to connect two strings, or print two strings next to each other. Now, that wasn't hard, was it. Now, save that code into a PHP file, and test it. This should be your output:

I have 49 Dollars

See you in the next tutorial.


Hosted by www.Geocities.ws

1