Home | Store | osCommerce Tutorials

PHP Custom Functions

osCommerce site has a lot of custom functions, therefore this tutorial will discuss PHP custom functions in more details with a lot of examples.

PHP has a lot of built-in functions. The print(), define() and require() are just some of the built-in functions we discussed in previous PHP tutorials.

Actually you can write or create your own or custom PHP functions. The main purpose of writting custom functions is that they can be reused again and again whenever you need it, without writting the same piece of codes again.

Basic Concept of PHP Function

Basically, functions are simply a block of codes that will be executed when run or called. This can easily be explained with the following diagram:

PHP custom function basic concept

Basic Syntax of PHP Function

The following diagram shows a basic syntax of PHP function. We will discuss the structure in more details in this functiontutorial.

PHP custom function sybtax

Most Basic Syntax of PHP Function

The most basic syntax of PHP function is shown in the following diagram. No value is sent to the function to process. The function simply process something, for example, print out something on the screen, and no values are stored in variables and return back to the codes.

PHP custom function basic syntax

The best way to understand PHP function is to see how it works.

PHP Custom Function Example 1:

<?php

function callYou()
{
echo "Who is calling me?";
}

// PHP codes here

// I need to call the callYou() function
echo '<p><b>1st time to call the callYou() function</b></p>';
echo callYou();

// More PHP codes here

// I need to call the callYou() function again here
echo '<p><b>2nd time to call the callYou() function</b></p>';
echo callYou();

?>

The function callYou() will be executed whenever it is being called. In the above example. the function callYou() is being called twice.

The output of the above PHP function example should look like:

1st time to call the callYou() function

Who is calling me?

2nd time to call the callYou() function

Who is calling me?

PHP Function with Values Send Into

This PHP function is bit more complicated than the previous example. However the function syntax is almost the same. In this PHP function tutorial, two values are sent to the function to process. The whole process is better to illustrate with the following diagram.

PHP custom function with value send into

PHP Custom Function Example 2:

<?php

function callYou($myName, $myAge)
{
echo "My name is " . $myName . " . I am " . $myAge . " old.";
}

// PHP codes here

// I need to call the callYou() function
echo '<p><b>1st time to call the callYou() function</b></p>';
echo callYou("Alex", 25);

// More PHP codes here

// I need to call the callYou() function again here
echo '<p><b>2nd time to call the callYou() function</b></p>';
echo callYou("Kathy", 22);

?>

The output of the above PHP function example 2 should look like:

1st time to call the callYou() function

My name is Alex . I am 25 old.

2nd time to call the callYou() function

My name is Kathy . I am 22 old.

PHP Function with Return Value

Sometimes PHP function required to return value back to the main code. The following diagram illustrate how value is returned from the function to the main code.

PHP custom function with return value

PHP Custom Function Example 3:

<?php

function numTotal()
{
$total = 100;
return $total;
}

echo "The value return from function is " . numTotal();

?>

The output of the above PHP function example 3 should look like:

The value return from function is 100

PHP Function with Argument and Return Value

In previous PHP function example with return value, there is no value send to the function to process. In this examplel, a value will send into the function for processing when called. And the argument parameter in the function will act as a variable for what value that has sent into the function. The scenerio is illustrated in the following diagram:

PHP custom function with return value

PHP Custom Example 4:

<?php

function numTotal($num1)
{
return $total = $num1;
}

$numT = numTotal(300);
echo "The value return is " . $numT;

?>

The output of the above PHP function example 4 should look like:

The value return is 300

PHP Function with Multiple Arguments and Return Value

In previous PHP function example, a single value was sent to the function when called. Actually multiple values can be sent to PHP functions.

In this example, two values will send into the function for processing when called. And the two arguments or parameters in the function will act as variables for what corresponding values that sent into the function. The scenerio is illustrated in the following diagram:

PHP custom function with return value

PHP Custom Function Example 5:

<?php

function numTotal($num1, $num2)
{
$total = $num1 + $num2;
return $total;
}

$var1 = 3;
$var2 = 5;
$numT = numTotal($var1, $var2);
echo "The total of " . $var1 . " and " . $var2 . " is " . $numT;

?>

The output of the above PHP function example 5 should look like:

The total of 3 and 5 is 8

Get Variables Inside PHP Function

Up to now, you may ask if the variables inside a function can be passed to the main program and use.

Let's try with an example:

PHP Custom Function Example 6:

<?php

function callYou()
{
$myName = "Alex";
}

// Call the function
callYou();

// Let's see if we can get the variable $myName inside the function.
echo "My name is " . $myName;

?>

The output of the above PHP function example 6 should look like:

My name is

Hm... The variable inside the function cannot be passed to the main program. Why? Because the variables inside the function is local (Local Function Scope) and cannot be passed outside for use. In other words, the variables inside the function is said to have Local Function Scope, and thus cannot be used by the main program (Global Scope).

This limitation will hinder the flexibility and power of PHP function. Therefore a global keyword is introduced.

PHP Custom Function Example 7:

<?php

function callYou()
{
// Declare variable $myName inside the function with global keyword
global $myName;

$myName = "Alex";
}

// Call the function
callYou();

// Let's see if we can get the variable $myName inside the function.
echo "My name is " . $myName;

?>

The output of the above PHP function example 7 should look like:

My name is Alex

Get Variables from Outside the Function

You may have another question. How can I use the variables outside the function?

Take a look at the following PHP example.

PHP Custom Function Example 8:

<?php

// Declare a variable outside the function
$myName = "Alex";

function callYou()
{
echo "My name is " . $myName;
}

// Call the function
callYou();

?>

The output of the above PHP function example 8 should look like:

My name is

As you can seen that the variable $myName is declared outside the function. Therefore the function can only be used outside the function.

In this situation. If you wish to use variables outside the function, simply declare the variables inside the function with global keyword. The following example show how to do that.

PHP Custom Function Example 9:

<?php

// Declare a variable outside the function
$myName = "Alex";

function callYou()
{
// Declare variable $myName inside the function with global keyword
global $myName;

echo "My name is " . $myName;
}

// Call the function
callYou();

?>

The output of the above PHP function example 9 should look like:

My name is Alex

Now you should understand how PHP custom function works.