First steps

Getting to the real stuff..

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 >?php 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
3
4
<?php 
 // How to define a variable in PhP
 $MyFirstVariable = 16;
 ?>
<?php 
 // How to define a variable in PhP
 $MyFirstVariable = 16;
 ?>

OK, that is all nice. But now what can we do with this? Of course: We can just display it on screen:

1
2
3
4
<?php 
 $MyFirstVariable = 16;
 echo $ MyFirstVariable ; // will show: 16
 ?>
<?php 
 $MyFirstVariable = 16;
 echo $ MyFirstVariable ; // will show: 16
 ?>

Or we can use it in a comparison. On of the most common constructor for this is an if – else statement. In PHP the different steps in the comparison are deliniated by normal brackets ( ) and curly brackts { }. An example will explain this better:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php 
 $MyFirstVariable = 16;
 $MySecondVariable = 20;
 
 if($MyFirstVariable < $MySecondVariable)
   {
   $compared = "less";
   }
 else
   {
   $compared = "more";
   }
 
 echo "$MyFirstVariable is $compared than $MySecondVariable";
 ?>
<?php 
 $MyFirstVariable = 16;
 $MySecondVariable = 20;
 
 if($MyFirstVariable < $MySecondVariable)
   {
   $compared = "less";
   }
 else
   {
   $compared = "more";
   }
 
 echo "$MyFirstVariable is $compared than $MySecondVariable";
 ?>

The output of this little script is of course “16 is less than 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 than an other value. Which other operators can we distinguish?

== Equals (please note: the single = is reserved for assigning a value)
< Less then
> More then
<= Less or the same
>= More or the same

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
3
4
5
6
7
<?php 
 $MyVariable = 3;
 $MyVariable = $MyVariable +2;
 echo $MyVariable; // Will give 5
 $MyVariable = "three";
 $MyVariable = $MyVariable + 2; // WIll result in an error message
 ?>
<?php 
 $MyVariable = 3;
 $MyVariable = $MyVariable +2;
 echo $MyVariable; // Will give 5
 $MyVariable = "three";
 $MyVariable = $MyVariable + 2; // WIll result in an error message
 ?>

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.

Leave a Reply