|
|
| BASICS OF PHP |
|
In this tutorial I will cover the basics of programming PHP. First I will
show how to declare and assign variables, then the use of functions and at
the end how to use classes. The understanding of this tutorial is essential
of the comming tutorials.
|
Variables
|
|
When you program one have to store values, we do this in variables.
In order to use a varibale we have to declare it. The declaration has
to take place in a class or a method. But this will we cover later.
When we declare a variable, we give it a datatype. This datatype
allows the variable to use different kind of numbers and strings.
Here is an example on how to make a declaration of variables and an assignment.
( comments in asp is made behind the // tags. )
|
//declaration of a vaiable
$myVariable;
//assigning it a value
$myVariable = "Hello Aprnon Tutorials";
|
|
This is a very wide declaration, because you can use it for number as well as
strings. I will not go into how you declare these different types, but rather make
a wide declaration in order to avoid parsing of variables.
|
Function
PHP is not object oriented in the same degree as C# and java. There
is however the possibility to make classes and use those. I will
come back to this later. Here I will show how to make functions and
later I will move them in to classes so we get a neat code :) But
first is first, here is an example on how to add two numbers with a function.
|
function Add ($number1,$number2)
{
return( $number1 + $number2 );
}
|
Here the Add function return the sum of number1 and number2.
Here is an example on how to use this function.
|
Class
A class contains many variables and functions.
And it is used to split the program into smaller peaces, in order
to get a well structured program.
Here is an example on a class:
|
class Math
{
function Math()
{
echo("object made");
}
function Add($number1,$number2)
{
return( $number1 + $number2 );
}
function Substract($number1,$number2)
{
return( $number1 - $number2 );
}
}
|
In order to get to the methods inside the class, we have to make an
object of the class we want access to.
We can than use the object to call the methods in the class.
But the object can just access the methods which are public.
There are some exceptions
here, but I will not cover this in this toturial.
Here is an example on how we make an object and use methods.
|
First of all the constructor will write that the class is made. Then
we will write to sum of 4 and 3, and the substraction between 4 and 3.
Here we use the Add method to add 4 and 3 and the Substract method to
substract 4 with 3.
This is basic programming, and in the comming tutorials I will use external
files for such classes and link to the project files.
|
|
Category
|
Programming with PHP
|
|
Author
|
Tommy Johannessen
|
|
|
|