Functions in PHP allow programmers to organize code into reusable blocks. There are built-in and user-defined functions. User-defined functions use the function keyword and can accept arguments, return values, and be called elsewhere in the code. Functions can pass arguments by value, where changes inside the function don't affect the original variable, or by reference, where changes are reflected outside the function. Functions can also have default argument values and static variables that retain their value between calls.
2. A function is a named block of code that performs
a specific task, possibly acting upon a set of
values given to it, or parameters, and possibly
returning a single value.
Functions in a PHP program can be either built-in
or user-defined.
3. PHP User-defined Functions
A function may be defined using syntax such as
the following:
function functionname()
{
//code to be executed
}
5. PHP Function Arguments or
Parameterized function
Information can be passed to functions through arguments.
An argument is just like a variable.
Arguments are specified after the function name, inside the
parentheses. You can add as many arguments as you
want, just separate them with a comma.
<?php
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello(“Amit");
sayHello("Vimal");
sayHello("John");
?>
6. PHP Function: Returning Value
PHP functions can return only a single value with
the return keyword:
<?php
function cube($n){
return $n*$n*$n;
}
echo "Cube of 3 is: ".cube(3);
?>
Output: Cube of 3 is: 27
7. Function Parameters
Functions can expect, by declaring them in the
function definition, an arbitrary number of
arguments.
There are two different ways of passing
parameters to a function. The first, and more
common, is by value. The other is by reference.
8. Call By Value
The call by value method of passing arguments
to a function copies the actual value of an
argument into the formal parameter of the
function. In this case, changes made to the
parameter inside the function have no effect on
the argument.
9. Call By Value
<?php
$a=10;
$b=20;
function swap($a,$b)
{
echo "before swapping value of a & b are $a,$b"."<br>";
$temp=$a;
$a=$b;
$b=$temp;
echo "after swapping value of a & b are $a,$b"."<br>";
}
swap($a,$b);
echo "after function call value of a & b are $a,$b";
?>
10. Call by Reference
In case of PHP call by reference, actual value is
modified if it is modified inside the function. In
such case, you need to use & (ampersand)
symbol with formal arguments. The & represents
reference of the variable.
11. Call by Reference
<?php
$a=10;
$b=20;
function swap(&$a,&$b)
{
echo "before swapping value of a & b are $a,$b"."<br>";
$temp=$a;
$a=$b;
$b=$temp;
echo "after swapping value of a & b are $a,$b"."<br>";
}
swap($a,$b);
echo "after function call value of a & b are $a,$b";
?>
12. Difference between call by value and
call by reference
No
.
Call by value Call by reference
1 A copy of value is passed to the
function
An address of value is passed to
the function
2 Changes made inside the function
is not reflected on other functions
Changes made inside the function
is reflected outside the function
also
3 Actual and formal arguments will be
created in different memory
location
Actual and formal arguments will be
created in same memory location
13. PHP Default Argument Values
Function
PHP allows you to define C++ style default argument
values. In such case, if you don't pass any value to
the function, it will use default argument value.
<?php
function greeting($first=“Sunil",$last=“Kumar"){
echo "Greeting: $first $last<br/>";
}
greeting();
greeting("Rahul");
greeting("Michael","Clark");
?>
14. PHP Function: Default Argument
Value
We can specify a default argument value in function.
While calling PHP function if you don't specify any
argument, it will take the default argument.
<?php
function sayHello($name=“Anil"){
echo "Hello $name";
}
sayHello("Rajesh");
sayHello();//passing no value
sayHello("John");
?>
Output: Hello Rajesh Hello Anil Hello John
17. Variable Scope
The variables defined in a function, including its
parameters, are not accessible outside the function,
and, by default, variables defined outside a function
are not accessible inside the function.
<?php
$a = 3;
function fun( )
{
$a += 2;
}
fun( );
echo $a;?>
18. Global Variables
If you want a variable in the global scope to be accessible
from within a function, you can use the global keyword. Its
syntax is:
global var1, var2, ...
Changing the previous example to include a global
keyword, we get:
<?php
$a = 3;
function fun( ) {
global $a;
$a += 2;
}
fun( );
echo $a;
?>
19. Static Variables
Like C, PHP supports declaring function variables
static. A static variable is shared between all calls
to the function and is initialized during a script’s
execution only the first time the function is called.
To declare a function variable static, use the static
keyword at the variable’s first use. Typically, the
first use of a static variable is to assign an initial
value.
static $count = 0;
21. When the function is called for the first time, the
static variable $count is assigned a value of 0.
The value is returned and $count is incremented.
When the function ends, $count is not destroyed
like a non-static variable, and its value remains
the same until the next time counter( ) is called.
The for loop displays the numbers from 0 to 4.