The power of any programming languages starts with variables and operators. Variables in PHP can be almost any word that starts with a “$”. To assign a value to a variable use the “=” operator. For example:
$Str=5; or $Str="5".
Please note that the value that $Str holds in the previous examples is different. One holds the numerical value of “5″, while the other holds the letter “5″. The difference may seem minor, but can really cause you problems if you’re not aware of these differences. In programming, these are called types.
Types
The PHP types we’ll be concered with in this tutorial are the following: Booleans, Integers, Floating point numbers, Strings, Arrays, and Objects.
Booleans
A boolean variable can hold the value of 1 or 0. Alternatively you can use the reserved words true and false. Most often these variables are used for flags and conditionals. For example, when you want to know when you are done you can assign the value true to $done.
Integers/Float (or double)
An integer is a whole number as contrasted to a Float which allows decimals. Integers can be specified in base 10, octal, or hex. To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x.
<?php
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
?>
An important point to remember about integers is that integer math produces different results than Floating point math. For example, consider the following code:
<?php
$a = 10;
$b = 3;
$c = (int) $a/$b;
?>
$c would hold the value of 3! This type of “error” isn’t as important in PHP because PHP allows automatic type conversion. This means that if I wrote $c=$a/$b in the previous example, PHP would detect that the results will not be an integer and it would have automatically converted $c into a real number. [The "(int)" declaration "cast" $c as an integer. This is called type casting and has nothing to do with acting!] $c would then have the expected value of 3.33333333333.
In languages like C and C++, if you declare a variable as an integer then it will stay an integer unless you execute a command to change it. Thus PHP allows the programmer to be more sloppy with their programming.
Strings
We’ve seen strings before. A string is a series of characters contained within single or double quotes, e.b. “Hello World” or ‘Hello World’.
Arrays and Objects
We’ll cover these complex data types later.
Operators
These are shown in the Glossary and won’t be re-covered here. Besides, you already know most from Excel and normal math. Some of the special operators we’ll explain later. OK? OK.
More on naming conventions
Though you can use anything for a variable name, maintaining code is simpler if you use a coding convention. There are three main conventions to be aware of: Hungarian notation, camel case, and Fortran-style.
Hungarian Notation
Named after the heritage of Charles Simonyi, a programmer who worked at Xerox PARC circa 1972-1981, and who later became Chief Architect at Microsoft, this notation include the type of the variable in the variable name. For example an integer hold the number of customer would be written as intCustomer and the flag done would be written as boolFlag. The advantage of this system is that the programmer always knows the data type held by the variable. As one might expect, this is the coding convention at Microsoft and in use by many VB and VBA developers.
Camel Case
This is the practice of using two or more words (without spaces) to describe the purpose of the variable and capitalizing some or all of the starting characters in each word. It also happens to be my variable. I find Hungarian notation interferes with my understanding of what the program is doing. The same variables used above in camel case might be written as follows: theCustomer and isDone. Java tends to use this style of coding.
Underscores
This is the practice of using two or more words to describe the purpose of the variable, but instead of using camel case, underscores are used where spaces would be. In my examples, we’d use the_customer and is_done.
i, j, k, l, m
Fortran had a concept where some variables were implicitly declared as Integers. These variables started with i, j, k, l, and m. Though you could use any word starting with those letters, it became commonplace to use those letters as counters in loops. If you needed to create a programming loop you would use i to hold the count. A subloop would use j, then k, etc. Though this is no longer the case in any modern language (Fortran included!) it is still commonplace to use i ,j, and k in this manner.
In the end, remember that variable names can be anything. Figure out a style you like and go with it. So long as you’re consistent it doesn’t really matter.
Putting it all to use!
Let’s start with a simple example of setting up a character for Harath that has 8 attributes and some secondary attributes. The program will store the basic attributes and calculate the secondary attributes. It will then print them all out. We’ll use Kentaro as our example. Kentaro has the following attributes:
| S | 10 | W | 5 |
|---|---|---|---|
| C | 5 | M | 4 |
| D | 4 | Q | 4 |
So let’s get started. Create a file called kentaro.php and include the following:
<?php
$S=10;
$C=5;
$D=4;
$W=5;
$M=4;
$Q=4;
$Hgt=24;
$Wgt=30;
//PF = 2 x Con
$pf=2*$C;
//MF = Mana
$mf=$M;
//SIZ = (Hgt+Wgt)/2
$size=($hgt+$wgt)/2;
//HP=(Con + Wgt) x 2
$hp=($C+$Wgt)*2;
//R = (Quick+Dex/2)
//We'll use a builtin function called ceil to return the next highest integer here
$r= ceil( ($Q+$D)/2 );
//MR=(Str+Dex+Hgt-Enc)/2
//Enc always equals 0 for base MR, so we'll ignore that
$mr=($S+$D+$Hgt)/2;
//SXD = Str/4 (round down)
//We'll use a built-in function, floor(), to return the next LOWEST integer here
$sxd=floor($S/4);
//TXD = Str/3 (round down)
//We'll use a built-in function, floor(), to return the next LOWEST integer here
$txd=floor($S/3);
echo "S: $S<br \>";
echo "C: $C<br \>";
echo "D: $D<br \>";
echo "W: $W<br \>";
echo "M: $M<br \>";
echo "Q: $Q<br \>";
?>
After you have that section working, add statements to print out all the secondary attributes that were calculated.