Functions
in PHP
PHP functions are similar to other programming languages. A
function is a piece of code which takes one more input in the form of parameter
and does some processing and returns a value.
Why use
Functions?
·
Better code organization – functions allow us to group
blocks of related code that perform a specific task together.
·
Reusability – once defined, a function can be called
by a number of scripts in our PHP files.
·
Easy maintenance- updates to the system only need to
be made in one place.
PHP
Built-in Functions
A function is a self-contained block of code that performs a
specific task.
PHP has a huge collection of internal or built-in functions
that you can call directly within your PHP scripts to perform a specific task,
like print_r(), var_dump, etc.
For a complete list of useful PHP built-in functions, please
check out PHP reference.
PHP
User-Defined Functions
PHP also allows you to define your own functions. It is a way
to create reusable code packages that perform specific tasks and can be kept
and maintained separately form main program.
There are two parts which should be clear to you −
·
Creating a PHP Function
·
Calling a PHP Function
Creating a
PHP Function
The basic syntax of creating a custom function can be give
with:
Syntax –
function
functionName()
{
//Code to be executed
}
|
The declaration of a user-defined function start with the
word function, followed by the name of the function you want to create followed
by parentheses i.e. () and finally place your function's code between curly
brackets {}.
A function name must start with a letter or underscore
character not with a number, optionally followed by the more letters, numbers,
or underscore characters. Function names are case-insensitive.
Example –
In this example of an user define function, that display an
message:
<?php
//Define
function
function
msg()
{
echo “Hello World!”;
}
//Calling
Function
msg();
?>
|
PHP
function with parameters (arguments)
PHP gives you option to pass your parameters inside a
function. You can pass as many as parameters your like. These parameters work
like variables inside your function. Following example:
Example – 1
<?php
//Define
function
function
name($name)
{
echo “Welcome ”.$name;
}
//Calling
function
name(“Cyberteak”);
?>
|
Example – 2
<?php
//Define
function
function
data($name, $birthofyear)
{
echo $name.” is born in”. $birthofyear;
}
//Calling
function
data(“CyberTeak”,”2018”);
?>
|
PHP Default
Argument Value
You can also create functions with optional parameters — just
insert the parameter name, followed by an equals (=) sign, followed by a
default value, like this.
Example – 3
<?php
function setWeight($weight = 50)
{
echo “The height is ”.
$weight.”<br>”;
}
setWeight(100);
setWeight(80);
setWeight(); //Default argument
setWeight(100);
?>
|
PHP
Function - Returning Value
A function can return a value back to the script that called
the function using the return statement. The value may be of any type,
including arrays and objects.
Example – 4
<?php
//Function Definition
function add($a,$b)
{
$c = $a+$b;
return $c;
}
//Calling Function
echo add(10,20); //30
?>
|
0 Comments