12

Mar

by steve

We’ve gone through the basics for variables and assignments. With this section we’ll add some more operators and focus on control structures. Control structures alter or affect the programming flow. There are four main control structures we’ll cover: Conditionals, switches, loops, and functions. Loops and functions we’ll cover with the next session.

Conditionals

The most basic form of structure is the conditional or branching statement. This allows us to execute different statements depending on some condition. This type of statement also introduces a two new set of operators: conditional and logical operators. If you think of a logic structure there are a few ways to compare two values, as follows:

equal to $a == $b true if $a equal to $b; “0″ is equal to 0 not equal to $a != $b true if $a is not equal to $b
exactly equal to $a === $b true if $a equal to $b and the type is the same; “0″ is not equal to 0 Not identical to $a !== $b true if $a is not equal to $b; “0″ is not equal to 0
greater than $a > $b true if $a is greater then $b greater than or equal $a >= $b true if $a is greater than or equal to $b
less than $a < $b true if $a is less than $b less than or equal $a <= $b true if $a is less than or equal to $b
exclusive or $a  xor  $b true only if one of $a and $b are true; but not both not ! $b true if $b is false
and $a && $b true if $a and $b are both true. or $a || $b true if $a or $b are true.

Basic Syntax

The most basic syntax is one that executes code only if something is true and is as follows:
if ( condition ) { statements; }
There are several things to note here. First, the statement starts with an “if”. Second, the condition must be contained within parenthesis. Third the code to be executed is contained within a “block”. Lastly, use white space and new lines liberally to assist the execution of coding. Blocks of code in PHP are always surrounded in braces, { … }. Below is a full sample:


if ($Wgt < $minWgt)  { $Wgt = $minWgt; }

If…then…else

Often it’s not sufficient to simply execute code if something is true. Sometimes you need to execute code is something is true and different code if it’s not. In PHP, this is done with an “else” block. The basic syntax is as follows:


if ( condition ) { statements; }
else { statements; }

Both of these structure are typically executed with more than one statement and can often be found nested within each other, therefore balancing the curly braces can become a challenge if indenting and copious use of new lines is not followed. There are two main methods of handling this. In one, a newline is always entered before an open brace. All statements are then entered on their own lines and tabbed in. The closing brace is then outdented. In this manner, you can easily see which braces matchup. See the code below:
 

if ($theSpell->name==$spellName)
{
echo "Updating spell!";</code>

$theSpell->runes=$runes;
$theSpell->skillName=$skillName;
$theSpell->specName=$specName;
$theSpell->intensity=$int;
$theSpell->range=$range;
$theSpell->radius=$radius;
$theSpell->comments=$comments;
$theSpell->tn=$tn;
$theSpell->power=$power;
$theSpell->explain=$explain;
$unfound=false;

}
if ($unfound)
{
$theSpell=new spell($spellName, $runes, $skillName, $specName, $int, $range, $radius, '', $tn, $power, $explain);
$spellList[$skillName][$specName][]=$theSpell;
}
}
else
{
$theSpell=new spell($spellName, $runes, $skillName, $specName, $int, $range, $radius, '', $tn, $power, $explain);
$spellList[$skillName][$specName][]=$theSpell;
}

An alternate method keeps the opening brace on the same line as the if statement. The only real advantage of this conservation of one line. It’s the method I prefer, because text editors I use have braces and parenthesis balancing assistance and I don’t like to see several lines where the only thing on it is an opening brace. Consider the previous code with this style:

if ($theSpell->name==$spellName) {
echo "Updating spell!";</code>

$theSpell->runes=$runes;
$theSpell->skillName=$skillName;
$theSpell->specName=$specName;
$theSpell->intensity=$int;
$theSpell->range=$range;&aring;
$theSpell->radius=$radius;
$theSpell->comments=$comments;
$theSpell->tn=$tn;
$theSpell->power=$power;
$theSpell->explain=$explain;
$unfound=false;

}
if ($unfound) {
$theSpell=new spell($spellName, $runes, $skillName, $specName, $int, $range, $radius, '', $tn, $power, $explain);
$spellList[$skillName][$specName][]=$theSpell;
}
}
else {
$theSpell=new spell($spellName, $runes, $skillName, $specName, $int, $range, $radius, '', $tn, $power, $explain);
$spellList[$skillName][$specName][]=$theSpell;
}

Assignment

For this assignment, I won’t give an example, but simply give you directions:

  1. Create a file called dieRoller.php
  2. Create three variables representing dice and give them the values, 1, 4, and 6
  3. Each die that has a value of 4 or greater should be counted as a success.
  4. Each 6 should have a new re-rolled die. This re-roll will be 3.
  5. Have the program calculate the number of successes of this roll (hint to add 1 to a variable use the following: $a = $a + 1;
  6. Output to the user the values of all dice, identifying the re-roll, and the number of successes

10

Mar

by steve

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.

28

Feb

by steve

PHP is a recursive acronym standing for PHP: Hypertext Processor. PHP executes on the server prior to the web server returning HTML code back to the client server. This guide will not go into HTML in detail, but because of its usage within an HTML environment, HTML will be used. Some specifics will be covered and some minimal explanation will be given.

Specifics of Processing

A typical scenario  starts with the user executing a URL in their browser. The web browser connects to the web server and requests the file specified. If no file is specified, the web server looks for a file called index.html or index.php. PHP code can used within either type of file, but convention dictates to name the files with the php extension. When the web server reads the file it identifies the PHP code and passes the code to the interpreter. The results are then returned into the file. Once the file has been completely read into the server’s memory is send the file back to the user. Please note that the user never sees PHP code nor is there anyway for the user to know what the PHP actually is. All the user sees is the resultant HTML file. PHP code in a code block must be placed within the html tags of
<?php and ?>. Later we’ll see how PHP code can be mixed with HTML freely so the two can interact somewhat.

Hello World

The simplest type of program (born with Kernigham and Richie's seminal work The C Programming Language,introduced the world to the "Hello World" program. Basically, all this program does is return the words, "Hello World" to the user. To start, create a file called world.php. In this file type the following lines exactly as they are here:


<?php
echo "Hello World";
?>

Now, upload the file to the web server to the /scratch directory and go to the URL: http://www.schraderenterprises.com/scratch/hello.php.

There, that was simple (I hope!). echo is statement in PHP that returns anything in quotes to the user. All statements in PHP must end with a semi-colon. One frustrating aspect of PHP/Web development is the handling of errors. With compiled code, the compiler will give you a list of errors to give you some idea of where you have made a mistake. With PHP on the web, you often see a blank white screen or the White Screen of Death (WSOD). If you see this, go back and look at your code carefully for things like missing semi-colons or parenthesis. A common way of debugging scripts is to use comments. A comment allows you to right helpful information to the reader of a script without the interpreter (or compiler) executing that line. PHP supports two types of comments:

  1. A single line common line comment is created with //
  2. A multi-line common line comment is created with /* and */
  3. . Anything between those two is commented.

Comments can be used to comment out suspect or buggy code or two add english descriptions to explain the code. Make it a practice to use comments frequently.


<?php
// This is a Hello World Program
echo "Hello World";
/* The lines below are commented out:
foo
bar
hey-didly-do!
*/
?>

Filed Under Programming 101 |  

28

Feb

ZEBRA

by steve

Environment:
PRAIRIE

Advantages:

* +2d PRAIRIE spirits
* +2d ILLUSION and DETECTION spells

Character:
Zebra is somewhat uncategorizable. Zebra shamans tend to have a techno-fetish (they love hardware and techie toys) but they are rabid defenders of the environment. They are usually skilled at social dealings but usually prefer solitary life for the most part. They tend to think in lofty abstracts and then deal in concrete realities. Zebra often contradicts himself, changes his mind without warning, and practices something other than what he preaches.

Disadvantages:
Zebra is actually fairly reliable and trustworthy, but he is really hard to predict. Zebra shamans go through mood swings that are severe, to say the least. They seem to often enjoy the problems they create for others, as they feed differing stories to different people. They sometimes have a problem keeping track of what they told who, though, and this often leads to trouble.

Filed Under Totems |  

28

Feb

WOLVERINE

by steve

Environment:
FOREST

Advantages:

* +3d FOREST spirits
* +3d COMBAT spells

Character:
Wolverine is fairly well-known for his temper and his fierce fighting ability. Wolverine is more territorial and dangerous than Badger, and is (perhaps unfortunately) a fairly common totem among shadow-running shamans.

Disadvantages:
Wolverine shamans are violent and tempermental, often for no real reason. They just like to rip things up and throw homicidal fits of rage. Whenever someone or something happens to piss off a Wolverine shaman (someone shoots at him, the car won’t start, the bank teller says he has exhausted the funds in that account), he must make a Willpower (4) test to avoid instantly laying into the source of his irritation with a physical attack– either with spell or weapon. For this reason, Wolverine shamans are often having to buy new possessions to replace old ones broken in a fit.

Filed Under Totems |  

next page »