So.. Now you are ready to start programming. Let’s get started!
First.. You need to know how to create a PhP file. As with general HTML files, this is very easy. All you need to do is create a normal text file, save it with the extention PHP and you ae ready to go. In the file itself, delineate the areas of PhP code with ? > and < ?. This will tell the compiler that a section with PHP code is coming up.
So now you know how to define a section with PHP. Lets get started with a variable. A variable can be used to store values and be used at a later instance. The way to identify a variable in PHP is with a dollar sign. A value is assigned by placing an = sign, and the value afterwards. You end the whole series with a semicolon, so the compiler knows the command has ended. When we combine this, we get:
1: <?
2: $MyFirstVariable = 16;
3: ?>
OK, that is all nice. But now what can we do with this? Of course: We can just display it on screen:
1: <?
2: $MyFirstVariable = 16;
3: echo $ MyFirstVariable ; // will show: 16
4: ?>
Or we can use it in a comparison. On of the most common constructor for this is and if - else statement. In PHP the different steps in teh comparison are deliniated by normal brackets ( ) and curly brackts { }. An example will explain this better:
1: <?
2: $MyFirstVariable = 16;
3: $MySecondVariable = 20;
4: if($MyFirstVariable < $MySecondVariable)
5: {
6: $compared = "less";
7: }
8: else
9: {
10: $compared = "more";
11: }
12: echo "$MyFirstVariable is $compared then $MySecondVariable";
13: ?>
The output of this little script is of course "16 is less then 20". Now, let us break is apart, and see each step. Line 1 is just a marker for the PhP compiler to start interpretation of the code. Line 2 and 3 define 2 variables, and give them the values 16 and 20. Line 4 is the exiting line, which tests whether the value stored in $MyFirstVariable is less then the value of $MySecondVariable. Please note the brackets around the subject of the if-statement. In the next paragraph we will look more closely at the comparison operator <. Line 5 shows the start of what happenes if the IF statements results in a true value, namely, we set $compared to the text (Or string variable) less. The curly bracket in line 6 ends this statement. Otherwise, (else) $compared is set to hold the string more.
So, in the if statement, we just saw that we can use the operator < to test whether a value is less then an other value. Which other operators can we distinguish?
An other thing which showed up in the code, was the use of a string (text), instead of a number in a variable. So, variables are not just reserved for numbers, but you also store numbers in them. However, if you store a string of a number (e.g., $MyVariable = "Three";) this will not be the same as storing a number (e.g., $MyVariable = 3;). The second you can use in a calculation, whereas the second you cannot.
1: <?
2: $MyVariable = 3;
3: $MyVariable = $MyVariable +2;
4: echo $MyVariable; // Will give 5
5: $MyVariable = "three";
6: $MyVariable = $MyVariable + 2; // WIll result in an error message
7: ?>
This should get you started. Have a play with the variables, and try out what you can do with the multiplication operator *, the substraction operator: - and the summation operator + in combination with variables.