Please wait...
Popular Resources
Recent Forum Posts

PHP Variables, Functions and Classes

Posted: 08-11-11 09:23     

PHP Variables, Functions and Classes

PHP holds many types of default variables functions and classes. Here we will look at some of the most commonly used ones.

Before you start looking at PHP features you should read the “PHP for Beginners Guide” to understand what PHP is and how to use it.

The most commonly used PHP statement is “echo”. This allows you to print content onto the page. Below is an example of how echo can be used.

Code
1
2
3
 
echo "This is some content"; 
 
 

The echo statement can accept strings, variables or functions to print content to the page. An example of using a variable is as follows:

Code
1
2
3
 
echo $content; 
 
 

That is the echo statement in its entirity.

Next let’s take a look at variables. Variables in PHP are used to store information in the form of strings or other types. Variables can be used on a PHP page once set. Setting a variable is simple:

Code
1
2
3
4
5
6
7
 
$content = "my content is a string";
 
# or

$content = 536; 
 
 

The second example is setting an integer (a number) and does not require double quotes.

 

The next thing to learn to use PHP more substantially are functions. Function are used to encase a peice of code which can be calld upon when needed. Functions allow you to structure and manage your code better. Firstly we need to define a function. We do so by doing the following:

Code
1
2
3
4
5
6
 
function myFunction() 
{
 
} 
 
 

Let’s run through what we just did. First we told PHP we are making a function. Next is the name of the function, in this case it is “myFunction”. Next you can see (). This holds any values we want to pass to the function, however we will leave this empty for now. Next we have a { and a }. Within these two brackets is where we put our code.
Example:

Code
1
2
3
4
5
6
7
8
 
function myFunction() 
{
 
echo "this is my function";
 
} 
 
 

Now we have made our functon we need to know how to use it. To do this we do the following:

Code
1
2
3
 
myFunction();
 
 

It’s that simple. Now when you put that anywhere in your PHP document it will run that code. Funtions can be used after they have been defined and however many times you wish.

PHP function can carry values between them. We can pass a value to a function and we can get a value out of a function. To pass a value to a function we put it in the () part of the function. For example:

Code
1
2
3
 
myFunction( $content );
 
 

However, before we can start passing values to our functions we need to tell our function what to expect. SO when we first make our function we tell it what values to look for.
Example:

Code
1
2
3
4
5
6
 
function myFunction( $value )
{
 
} 
 
 

In the code above, we have made our function like before, but told it that we will be giving it a value. So when we want to use this in our function we ask for the variable: $value.

Here is an example of creating and using a function in a PHP document.

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
<php
 
function myFunction( $value)
 
{
 
echo $value;
 
}
 
$content = "this is my content";
 
echo myFunction( $content );
?> 
 
 

So what will he expect from this bit of code. The output will simply say “this is my content”. But to display this we have done: Made our function which accepts a value, set a variable with some content in it then called our function passing our variable to it. Then our function produces the text by using the echo statement.

Now, we can also get values from functions. To do this we use the return statment. Here is an example:

Code
1
2
3
4
5
6
7
8
 
function myFunction() 
{
 
return "this is my content";
 
} 
 
 

This function will now give us a string when we ask for it which says “this is my content”. Here is how we may use it.

Code
1
2
3
 
$content = myFunction();
 
 

So by using that, our $content variable will now have the value that myFunction returns. e.g $content = “this is my content”.

That is how PHP functions work and how they can be used.

The next step in PHP are classes. Classes are like a library. They hold a collection of variables and functions which all relate to each other and have a specific purpose within the PHP project. PHP Classes are a good way of manging your code and keeping things tidy. Using classes is known as OOP (Object Orientated Programming).

Mostly, a single class will be kept in it’s own file. First we need to start our class.

Code
1
2
3
4
5
6
 
class myClass
{
 
} 
 
 

You can see its very similar to a function but we can’t pass values to it. Now within the two brackets is where we put all of the code for our class. This is just general PHP variables and functions. However we have to set them a bit differently. To set a variable we use:

Code
1
2
3
 
 var $myVariable; 
 
 

To set a function we use:

Code
1
2
3
4
5
6
 
public function myFunction() 
{
 
} 
 
 

With variables inside classes we always use the word “var” before our variable, and this defines it as a variable within the class. When setting variables in functions you can set them normally.

Functions in classes can now have a property such as public, private or protected. Public functions can be accessed outside the class, and private/protected functions can only be accessed by the current class they are made in.

When we have built up a class it will look something like this:

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
<?php
 
class myClass
 
{
 
var $myVariable;
 
public function myFunction()
 
{
 
echo "this came from a class";
 
}
 
}
 
?> 
 
 

Above we have defined one variable and one functions in out class called myClass.

Now, when we want to use te variables in our class we have to call them a different way, like below:

Code
1
2
3
 
$this->myVariable
 
 

Let’s try and explain what happened above.

When in a class there is a global variable called $this. This variable is a reference to anthing within the current class. To reference to anything in the class we use the $this variable followed by ->. This is known as an object identifier and lets us access things within our class.

Now we have a class, we want to use it. So to use it within our php document we use:

Code
1
2
3
4
5
 
$myClass = new myClass;
 
$myClass->myFunction(); 
 
 

Our first line of code is specifying our class into a variable. we do this by using new and then the class name.

The second line is using the function which is in our class by using our class variable ($myClass) and then the function using the object identifier. We can access variables in exactly the sam way.
Example:

Code
1
2
3
 
$myClass->myVariable; 
 
 

That’s the basics PHP variables functions and classes. From these you can create simple OOP (Object Orientated Programming) website. To understand classes better, take a look at the Advanced PHP Classes tutorial.