If you are writing a piece of code no matter if it is large or small, it would be a very good idea to manage it in a way that each part of the code will only do one thing at a time, this will help us in debugging and also will isolate parts of the code so it would be a very useful thing to learn, Yes I am talking about writing your own PHP functions.

But before we start this I would like to point out that there are two different kind of functions available in PHP

  1. Internal functions that come with PHP and there are a lots of them(good thing for us!) and you can find them at online PHP manual
  2. Custom functions that we will write and in this part of our PHP Training we will look at how to make a PHP function.

here is how it works(And it has began!):

you will need to first define a function. For this you can use this syntax:

function Function_name(variable_1[optional],…,variable_n [optional]){         //note there is no semicolon “;”

Some code that it has to run;

}

and then to run it you will need to use:

Function_name(variable_1[optional],…,variable_n [optional]);              //note the semicolon “;”

so If you would like to create  a function to count down from 4 to 1 here how you can do this:

function countdown(){

$counter=4;

do{

echo $counter.”<br />”;

$counter–;

}while($counter!=1);

}

countdown();

now if you want to make this more generalised so you can tell it what number to start then you can try this:

function countdown($counter=10){

for($i=$counter ; $i >0 ; $i–){

echo $i.”<br />”;

}

}

countdown();// this will show numbers from 10 to 1, one on each line

countdown(5);/ this will sow numbers 5 to 1, one in each line

You can have multiple inputs like :

function countdown($start = 10,$finish = 0){

/* it will get two variables and it set the default value for them

(you don’t have to set the default but if you don’t and you don’t put the value in when calling the function you will have some trouble!)*/

for($i = $counter ; $i >=$finish ; $i–){

echo $i.”<br />”;

}

}

countdown();// will display 10 to 0, one per line

countdown(5);// will display 5 to 0, one per line

countdown(5,2);// will display 5 to 2, one per line

countdown(5,8);//won’t show anything

Now what if you wanted a value or multiple values returned? well they are different ways of doing this but using the function here is how you can do this:

  • Use “&”

if the variable that you like to return can be returned in the same variable they entered into the function you can use “&” before the variable name like this:

function what_car(&$car){

if ($car==”b” or $car==”B”){

$car=”BMW“;

}else{

$car=”My Car”;

}

}

$c=”b”;

what_car($c);

echo “The car is: “.$c; // will show The car is BMW

$c=”c”;

what_car($c);

echo “The car is: “.$c; // will show The Car is My Car

  • Use return

You can return as many as you like with this method and here is how:

function foo($something){

$result=$something*12;

return $result;

}

$c=foo(4);

echo $c; // this will return 48

Now if you want to send back multiple values just add them to an array and return the array like:

function numbers_thing($num){

$a=$num%2;

if ($a==1){$res1=”It can not be divided by 2″;}else{$res1=”It can be divided by 2″;}// can it be divided by 2?

$b=$num%3;

if($b==0){$res2=”It can be divided by 3″;}else{$res2=”It can not be divided by 3″;}// can it be divided by 3

return array($res1,$res2);

}

$j=numbers_thing(9);

echo $j[0].”<br />”.$j[1];// will display It can not be divided by 2 in the first line and then It can be divided by 3 at the second line.

  • Use Global variables

I will be discussing this in the Scope of variables