PHP Functions Example Tutorials

By |2018-04-01T11:17:00+05:30January 21st, 2017|PHP|

PHP functions is one of the language’s most powerful concept. PHP is a programming language that is associated with dynamic web development.

PHP Functions :

A function is defined as a block of statements that performs a specified task and may be used repeatedly within a program.

At the moment, there exist over 2000 inbuilt PHP functions and the number keeps increasing with each update.

PHP User Defined Functions :

The power of a programming language is its ability to allow the developer to define functions. A program is a set of instructions for a computer system.

PHP allows the developer to define functions, thus being able to do anything, being limited only by his or her creativity.

User defined functions start with the keyword ‘function.’

Syntax :


function functionName() {
    Block of code comes here;
}

Function names can only begin with an underscore or letter. It is a good idea to use names that imply the application of that function.

PHP functions waits until the page is fully loaded before their execution. The time needed for this to happen has been reduced significantly with released updates, which are currently at PHP 7.

Example :

In this example, we intend to create function to display personal phone contacts. It could be for a conference sign up or any other function.


<?php
function contactlist() {
echo "<b><u>Name</u>  <u>Phone Number</u></b><br>";
echo "Mark: (+91) 8595 879545<br>";
echo "Mary: (+1) 8455 057854<br>";
echo "Lucy: (+263) 854 454555<br>";
}
contactlist();
?>

Output:

Name Phone Number
Mark: (+91) 8595 879545
Mary: (+1) 8455 057854
Lucy: (+263) 854 454555

We have created the function called contactlist(). Whenever it is called, the PHP parser executes it, and we are able to get the results shown above.

PHP functions with arguments :

PHP function Arguments are a standard way of passing information to functions. Arguments behave like variables.

PHP function arguments are specified within the parentheses after the function name. Whenever the arguments passed are two or more, they should be separated by a comma.

Syntax :


<?php

function functionName($argument) {

Block of code to be executed;

}

We could think of a hydrocarbons chains organic chemistry. There are three types of hydrocarbons.

Alkanes, alkenes, and alkynes, all named based on the number of carbons on the backbone chain. Let us use function arguments to name them.


<?php
function alkanes($prefix) {
    echo $prefix;
    echo "ane.<br>";
}

alkanes();
alkanes("1 Meth");
alkanes("2 Eth");
alkanes("3 Prop");
alkanes("4 But");
alkanes("5 Pent");
?>

Output:

ane.
1 Methane.
2 Ethane.
3 Propane.
4 Butane.
5 Pentane.

PHP Functions Default Arguments :

It is usually a good practice to initialize functions by setting a default parameter or argument.

We can give PHP arguments their default values by assigning values to the arguments within the parentheses.

Syntax :


function functionName($parameter = default_value) {
Block of code to be executed;
}

Example :


<?php

function alkanes($prefix = "Alk") {
echo $prefix;
echo "ane.<br>";
}

alkanes();
alkanes("1 Meth");
alkanes("2 Eth");
alkanes("3 Prop");
alkanes("4 But");
alkanes("5 Pent");
?>

Output:

Alkane.
1 Methane.
2 Ethane.
3 Propane.
4 Butane.
5 Pentane.

The first argument takes the default value because none is defined.

PHP returning Functions :

It lets the functions return a value to be used within a program.

Example :

In this example, we calculate the hypotenuse of a triangle.


<?php
function hypotenuse($a, $b) {
$c = ($a**2 + $b**2)**0.5;
return $c;
}

echo "A triangle with length=5 and width=10, has a hypotenuse of ". hypotenuse(5, 10) . "<br>";
echo "A triangle with length=3 and width=4, has a hypotenuse of ". hypotenuse(3, 4) . "<br>";
echo "A triangle with length=2 and width=4, has a hypotenuse of " . hypotenuse(2, 4);"<br>";
?>

Output:

A triangle with length=5 and width=10, has a hypotenuse of 11.180339887499
A triangle with length=3 and width=4, has a hypotenuse of 5
A triangle with length=2 and width=4, has a hypotenuse of 4.4721359549996

Dynamic Functions Call :

Dynamic functions are defined when a developer assigns a function name as a string to a variable, then treats it as one would, the function name itself.

Example :

<?php

function myName() {
    echo "My name is Mark</br>";
}

$function_name = "myName";
$function_name();
//calling the function directly
myName();
?>

The output will be the same as that obtained when one calls the function directly.

Output:

My name is Mark
My name is Mark

PHP Functions Call by Reference :

A function may be called or its values altered by using the referencing method. A variable is manipulated using a function and using it within a program, rather than creating a duplicate of the variable’s value.

In this case, controlling the variable is simpler because it can be altered simply by passing an argument to it by reference, using the ampersand(&) symbol. This symbol is added before a function definition or function call.

Example :


<?php

function sum($cats) {
$cats+=3;
}

function animals(&$cats) {
$cats+=7;
}

$pets = 11;
sum($pets);
echo "The total number of pets is $pets</br>";

animals($pets);
echo "The total number of animals is $pets</br>";
?>

Output:

The total number of pets is 11
The total number of animals is 18

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment