An overview of functions
Using functions in your code should be 2nd nature in medium to large scale projects. In basic terms, a function is a block of code with a name, and a majority will take parameters/arguments and return a value(s). If you’ve done PHP before (if you’re reading this I’m guessing you have) you no doubt will have encountered functions without knowing it - here’s some you’ve probably ran into; echo(), require_once(), include_once(), die(). All of these functions are already built in to PHP and unlike other languages you don’t need to do anything before you can use them.
Basic syntax
There’s two pieces of code you need to know when using and creating functions, the code to create them and the code to call them. Let’s take a look at both.
function myFunction(/* parameters here*/)
{
// code goes here
// return a value (optional)
return true;
}
This code would create a (useless) function called myFunction(), to call it in our code we’d do something like this;
myFunction(/*parameters here*/);
Obviously without the commented /*parameters here*/. As you can see, by storing a chunk of code in one location and referencing it rather than rewriting it over and over again, if we have a problem with the code we only have to edit one location rather than several. Also if we want to update the code or anything in between, we can again do it from one location. Of course, nothing makes sense without a real world example, so lets say that we have an application that needs to check if a number is even, here’s a simple way of how we’d go about that;
function is_even($number)
{
if ($number % 2 == 0 )
{
return true;
} else {
return false;
}
}
$myNumber = 1491823;
if ( is_even($myNumer) )
{
echo $myNumber . ' is even!';
} else {
echo $myNumber . ' is odd!';
}
Granted the above function won’t be the most useful one you’ll keep under your belt but the theory is there. Let’s go through it bit by bit and see what’s happening. First of all we’re creating a function called is even which takes one parameter called $number. A parameter is something you can pass into a function, it can be a number, string, variable and so on. Next up for those of you who don’t know what the % is, it shows the remainder, obviously dividing an even number in 2 will have no remainder which is how we find out what the number is. The rest of the code is self explanatory, we can use it just like any other function.
But hang on, let’s roll back a bit and take another look at the variables we’re using. Now something to keep in mind is that outside of the function {} we can not edit any of the variables inside, vice versa - we can not edit variables not inside the function {} from inside the function {} - not the best way to word it, so let’s move on and look at an important factor of functions…
Variable scope
Variable scope is quite a large subject and we only want to know what we need, so we won’t go too far in depth but I heavily recommend looking it up and doing some further reading, it can cause a lot of problems if you’re not knowledged in the area.
Every variable is born with its own scope, a scope is the area in which it can be used in any way. Look at this example;
function set_something()
{
$something = 'hello world';
}
echo $something;
This would echo nothing as $something is local to the function. Variables defined inside functions are local to that function and can not be used by the main body of code as standard. However, the scope of variables can be expanded. The following two examples will work.
$myVar = 1;
function example()
{
global $myVar;
$myVar = $myVar + 10;
return $myVar;
}
echo example(); // would echo 11
$myVar = 1;
function example()
{
$GLOBALS['myVar'] = $GLOBALS['myVar'] + 10;
}
echo $myVar; // would echo 11
That’s right kids, the way functions can access variables outside their local scope is to either declare the variable as global as in example #1 or to use the superglobal - $GLOBALS. So long as the function knows to look for a variable in the global scope, we’re in business.
Whilst we’re on the subject of scope let’s quickly brush on to static scope variables and their use within functions. We know that normal variables in a function have local scope, to use variables outside the function we use the global scope, but when a function ends and returns the control to the main code again it will forget any local variables it used, what if we wanted to remember them?
Introducing the static varaible. Static variables are identical to local scope variables with the exception they will only be initialized once and will be remembered in the function. Let’s say you want a function to keep count of something, for our example let’s say we want to keep count of how many times we query a database, it’d need to look a little something like this;
$myVar = 1;
function query_count()
{
// only initalized once
static $count = 0;
$count++;
echo $count;
}
// some query here
query_count();
// some query here
query_count();
// some query here
query_count();
The above would echo 123. The static $count = 0; is the code that initializes the variable and is only executed once and ignored then on. Also for those who are wondering what the ++ means, it incremements the variable calling it by 1.
The one-line return
The return command doesn’t just return true, false or a variable, it can be used for pretty much anything.
function some_function()
{
$some_var = $var_a + $var_b;
return $some_var
}
Can be expressed as
function some_function()
{
return $var_a + $var_b;
}
Just a small tip that saves time, lines of code and variables. The return function can do anything, even call other functions!
Conclusion
Well I hope you’ve got to grips with basic functionality of functions (pun?), be sure to keep a look out for part 2 where we’ll look at more in depth and advanced functions and varaibles. Any questions or suggestions let me know just below (poet?!)



06:36 On December 5th, 2008
pretty basic concepts here. Very nice to newbies.
20:26 On December 5th, 2008
Good Tutorial! It was chosen for the home page of http://www.tutorialsroom.com
Please submit all of your future quality tutorials in there.
19:03 On December 10th, 2008
hi there … nice tutorial by the way i am new to php starting from today i want to know which compiler is normally used for writing php
thanx and i really appreciate ur tutorial !
10:54 On December 11th, 2008
Hey jerichoxd, you don’t need a compiler for PHP as it’s a run-time language meaning it compiles on the fly. You should look in to finding some cheap hosting with PHP support, be sure to checkout HostGator!
22:42 On January 15th, 2009
very good!
11:20 On March 3rd, 2009
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru