Functions
Functions
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.
<?php
function sendmsg() {
echo "Hey there";
}
sendmsg();
?>
OUTPUT:
Hey there
PHP User-Defined Functions
• PHP also allows you to define your own functions.
OUTPUT:
Fatal error
PHP Default Argument Value
• The following example shows how to use a default parameter.
• If we call the function setchildren() without arguments it takes the default value as argument:
<?php
function setchildren(int $children = 0) {
echo "Total number of children are : $children <br>";
}
setchildren(2);
setchildren(1);
setchildren();
setchildren(3);
?>
OUTPUT:
Total number of children are : 2
Total number of children are : 1
Total number of children are : 0
Total number of children are : 3
Returning values
• A function can return a value back to the script that called the function using the return
statement.
<?php
function add(int $a, int $b) {
return $a+$b;
}
OUTPUT:
5 + 10 = 15
7 + 13 = 20
2+4=6
Return Type Declarations
• PHP 7 also supports Type Declarations for the return
statement.
Return Type Declarations(contd.)
<?php
function add(float $a, float $b) {
return $a + $b;
}
echo add(1.2, 5.2);
?>
OUTPUT:
6.4
Return Type Declarations(contd.)
<?php
function add(float $a, float $b) {
return (int)($a + $b);
}
echo add(1.2, 5.2);
?>
OUTPUT:
6
Passing Arguments to a Function by Reference
• In PHP there are two ways you can pass arguments to a
function: by value and by reference.
$mynum = 5;
echo $mynum; // Outputs: 5
echo "<br>";
selfMultiply($mynum);
echo $mynum; // Outputs: 25
?>
OUTPUT:
525
Passing Arguments to a Function by
Reference(contd.)
<?php
function product(&$value) {
$value *= 5;
}
$num = 2;
product($num);
echo $num;
?>
OUTPUT:
10
Passing Arguments to a Function by
Reference(contd.)
<?php
function adder(&$str2)
{
$str2 .= 'Call By Reference';
}
$str = 'Hello ';
adder($str);
echo $str;
?>
OUTPUT:
Hello Call By Reference
Understanding the Variable Scope
• However, you can declare the variables anywhere in a
PHP script.
OUTPUT:
Web development language: PHP
Local variable(contd.)
<?php
function mytest()
{
$lang = "PHP";
echo "Web development language: " .$lang;
}
mytest();
//using $lang (local variable) outside the function will generate an error
echo $lang;
?>
OUTPUT:
Web development language: PHP
Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line
28
Global variable
• The global variables are the variables that are declared
outside the function.
• These variables can be accessed anywhere in the
program.
• To access the global variable within a function, use the
GLOBAL keyword before the variable.
• However, these variables can be directly accessed or
used outside the function without any keyword.
• Therefore there is no need to use any keyword to access
a global variable outside the function.
Global variable(contd.)
<?php
$lang = "PHP";
function mytest()
{
global $lang;
echo $lang;
echo "<br>";
}
mytest();
echo $lang;
?>
OUTPUT:
PHP
PHP
Global variable(contd.)
<?php
$lang = "PHP";
function mytest()
{
$lang = "Java";
echo $lang;
echo "<br>";
}
mytest();
echo $lang;
?>
OUTPUT:
Java
PHP
Global variable(contd.)
Note: Without using the global keyword, if you try to access
a global variable inside the function, it will generate an error
that the variable is undefined.
Global variable(contd.)
<?php
$lang = "PHP";
function mytest()
{
echo $lang;
echo "<br>";
}
mytest();
?>
OUTPUT:
Warning: Undefined variable $lang in C:\xampp\htdocs\a.php on
line 5
Global variable(contd.)
• Using $GLOBALS instead of global - Another way to use the global
variable inside the function is predefined $GLOBALS array.
<?php
$lang = "PHP";
function mytest()
{
$l = $GLOBALS['lang'];
echo $l;
echo "<br>";
}
mytest();
?>
OUTPUT:
PHP
Global variable(contd.)
<?php
$num1 = 5; //global variable
$num2 = 13; //global variable
function global_var()
{
$sum = $GLOBALS['num1'] + $GLOBALS['num2'];
echo "Sum of global variables is: " .$sum;
}
global_var();
?>
OUTPUT:
Sum of global variables is: 18
Global variable(contd.)
• If two variables, local and global, have the same name, then the local
variable has higher priority than the global variable inside the function.
<?php
$x = 5;
function mytest()
{
$x = 7;
echo "value of x: " .$x;
}
mytest();
?>
OUTPUT:
Value of x: 7
Static variable
• When a function is completed/executed, all of its variables
are deleted. However, sometimes we want a local variable
NOT to be deleted. We need it for a further job.
OUTPUT:
Static: 3
Non-static: 6
Static: 4
Non-static: 6
Static variable(contd.)
<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
static $x = 1;
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
</body>
</html>
OUTPUT:
1
2
3