Broad Network


PHP Function Basics

Basics of PHP – Part 9

Forward: In this article I explain the basics of PHP functions.

By: Chrysanthus Date Published: 28 Jul 2012

Introduction

This is part 9 of my series, Basics of PHP. A Function is a set of statements that perform a specific task. When you will get to writing programs, you will realize that programs are very long. You will realize that there are groups of statements that will have to be doing the same task in different parts of the code (program). You do not need to type this group of statements in different parts of the code. You can type it once, and then call it wherever it is needed in the code. In this article I explain the basics of PHP functions.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Defining Functions
The group of statements forms the function, but you need to group them in a particular way. By doing this, we say you are defining a function in PHP. In some languages, this process is split into two. One phase is called, declaring the function and another phase is called, defining the function. For this article and for basic PHP programming, we shall use a single process, which is defining the function.

A function definition consists of the following in the order given

- The reserved word, function.
- The name of the function.
- A list of parameters to the function, enclosed in parentheses and separated by commas (see below).
- The statements that define the function, enclosed in curly braces. The statements in a function can have among them calls to other functions defined in the current program (application).

Note: another name for reserved word is keyword.

Example
In the following example, we define a function that will add two numbers, find the square of the sum and then return the result.

    <?php

        function myFn ()
            {
                $num1 = 2;
                $num2 = 3;

                $sum = $num1 + $num2;
                $square = $sum * $sum;

                return $square;
        }

    ?>

The function begins with the reserved word, function. The name of the function is myFn. This is followed by parentheses. Then you have the block. In the block, you have the declaration and assignment of the two numbers. The third statement in the block sums the two numbers. The fourth statement squares the sum. The last statement returns the square to the statement that would call the function, outside the function. The reserved word, return, is used for this. It is followed by a variable or a literal. Not all functions end with the return instruction. Some functions just perform a task and do not return anything.

Calling a Function
You call a function by just typing the name of the function, followed by parentheses, in a statement. The following code illustrates this. Try it:

    <?php

        function myFn ()
            {
                $num1 = 2;
                $num2 = 3;
                    
                $sum = $num1 + $num2;
                $square = $sum * $sum;
            
                return $square;
            }

    $result = myFn();
    echo $result;

    ?>

This code is similar to the previous with the addition of two last statements. The last-but-one statement calls the function. This statement call is outside the function. The right operand of the statement is “myFn()”. It is this expression that calls the function. When it calls the function, it receives the value returned by the return statement in the function. This value is now assigned to the variable, $result. The last statement displays the result.

A function call does not always have to assign a return value to a variable. Functions that do not have return values are called by just typing the name, followed by parentheses (then semicolon, to form a statement).

Parameters and Arguments
Now, in the above function we can only deal with two particular numbers, which are 2 and 3. This is a disadvantage. If we declare (create) and assign the variables outside the function, then we can always change the values of the variables, then send the variables to the function before the function is executed. In this way, we shall be able to deal with many other pairs of numbers. The following example illustrates this:

    <?php

        $num1 = 4;
        $num2 = 5;

        function myFn($no1, $no2)
            {
                $sum = $no1 + $no2;
                $square = $sum * $sum;

                return $square;
            }

        $result = myFn($num1, $num2);
        echo $result;

    ?>

This time the variables have been declared and assigned outside the function. Some other function elsewhere in the code can actually change these values. However, a function cannot change the value of a variable inside some other function (everything being equal). In the definition of the function, the parentheses now have two variables. These variables in this position are called Parameters. These parameters of the function are used within the function.

In the last-but-one statement, where the function is called; the parentheses have two variables. These variables in this position are called Arguments. These arguments of the function are the variables declared outside the function. The arguments to a function call, can be literals, something like:

    $result = myFn(4, 5);

Read the above code and try it.

It is advisable to always make the variables for the parameters different from the corresponding variables for the arguments. If you do not do this, then while manipulating the parameters within the function, you might change the values of the variables outside the function.

Default Argument Value
Imagine that in the above example, one number is fixed, and the other can be changing. For example, assume that $num2 will always be 5 and $num1 can be any other number such as 2, 4, 6, 15, etc. In this case, the only variable declared (created) would be $num1. The fixed number will be one of the function parameters as the following example shows:

    <?php

        $num1 = 2;

        function myFn($no1, $no2=5)
            {
                $sum = $no1 + $no2;
                $square = $sum * $sum;

                return $square;
            }

        $result = myFn($num1);
        echo $result;

    ?>

When something is kept fixed like this, we say it is the Default Argument Value. You just have a parameter name and assign a value to it in the parentheses of the definition of the function. Note that in the function call, only one variable ($num1) has been used. You do not need to include the default argument value in the function call.

The echo Construct
The echo construct we have been using is not really a function. That is why it does not need parentheses when it is used. Just know that you can send more than one argument with the echo construct. Separate the arguments with comma as in the following example:

    <?php

        echo "one", "&nbsp;", "two";

    ?>

You can use variables in place of the literals.

Variables in Strings
If a string is in double quotes and it has as part of its content, a variable, the value of that variable will be displayed. However, if the string is in single quotes, then the name of the variable will be displayed and not the value of the variable. Read and try the following code:

    <?php

        $var = "good";

        echo "This is a $var man."; echo "<br />";
        echo 'This is a $var man.';

    ?>

Let us stop here and continue in the next part.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message