0% found this document useful (0 votes)
41 views

Web Based Application Development With PHP: WBP (22619) Lect 9

This document discusses functions in PHP, including defining functions, passing parameters, variable scope, static and anonymous functions. Key points: - Functions are named blocks of code that perform specific tasks and can take parameters and return values. They improve reliability, readability and reuse of code. - Functions are defined using the function keyword followed by the name and parameters. Parameters can be passed by value or reference. - Variables inside functions are not accessible outside by default. The global keyword makes global variables accessible inside functions. - Static variables retain their value between function calls. Anonymous functions are defined without a name and can access outer scope variables using the use keyword.

Uploaded by

Govind Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Web Based Application Development With PHP: WBP (22619) Lect 9

This document discusses functions in PHP, including defining functions, passing parameters, variable scope, static and anonymous functions. Key points: - Functions are named blocks of code that perform specific tasks and can take parameters and return values. They improve reliability, readability and reuse of code. - Functions are defined using the function keyword followed by the name and parameters. Parameters can be passed by value or reference. - Variables inside functions are not accessible outside by default. The global keyword makes global variables accessible inside functions. - Static variables retain their value between function calls. Anonymous functions are defined without a name and can access outer scope variables using the use keyword.

Uploaded by

Govind Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Web Based Application

development with PHP


WBP (22619)
Lect 9

G. K. YADAV
Lect, Computer Engineering
Government Polytechnic Yavatmal
COMPETENCY
Develop simple web-based application
using PHP language
Unit-II
Arrays, Functions and Graphics
CO
b) Perform operations based on arrays and
graphics.
UO
2 a) Manipulate the given type of arrays to get the desired result
2 b) Apply implode, explode functions on the given array
Functions
• 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 save on compile time—no matter how many
times you call them, functions are compiled only once for
the page.
• They also improve reliability by allowing you to fix any bugs
in one place, rather than everywhere you perform a task,
and they improve readability by isolating code that
performs specific tasks.
• Functions in a PHP program can be built-in or user-defined.
Functions
• To define a function, use the following syntax:
function [&] function_name([parameter[, ...]])
{
statement list
}
• The function name can be any string that starts with a
letter or underscore followed by zero or more letters,
underscores, and digits. Function names are case-
insensitive.
• By convention, built-in PHP functions are called with
all lowercase.
Functions Example:
function strcat($left, $right)
{
$combinedString = $left . $right;
return $combinedString;
}

function doubler($value)
{
return $value << 1;
}
Functions
• You can nest function declarations, but with
limited effect.
• The inner function cannot be called until the outer
function has been called.
Functions
$a = 3;
function sum()
{
$a += 2;
}
sum();
echo $a;
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.
• 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, ...
Static Variables
• A static variable retains its value between all calls to the function and is
initialized during a script’s execution only the first time the function is
called.
• Use the static keyword at the variable’s first use to declare a function
variable static.
function counter()
{
static $count = 0;
return $count++;
}
for ($i = 1; $i <= 5; $i++) {
print counter();
}
Function Parameters
• There are two different ways to pass parameters to a
function.
• Passing Parameters by Value: In most cases, you pass
parameters by value. The argument is any valid
expression. That expression is evaluated, and the
resulting value is assigned to the appropriate variable
in the function.
• Passing Parameters by Reference: Passing by reference
allows you to override the normal scoping rules and
give a function direct access to a variable.
Passing Parameters by Reference
<?php
function doubler(&$value)
{
$value = $value << 1;
}
$a = 3;
doubler($a);
echo $a;
Variable Functions:
• If a variable name has parentheses appended to it,
PHP will look for a function with the same name as
whatever the variable evaluates to, and will
attempt to execute it.
• Such a function is called variable function.
• Variable functions can not be built with language
constructs such as include, require, echo etc.
Variable Functions:
<?php
function hello(){
echo "Hello World";
}
$var="Hello";
$var();
?>
Anonymous function
• Anonymous function is a function without any
user defined name. Such a function is also called
closure or lambda function.
• Sometimes, you may want a function for one time
use. Closure is an anonymous function which
closes over the environment in which it is defined.
• Syntax
• $var=function ($arg1, $arg2) { return $val; };
Anonymous function
• There is no function name between the function keyword
and the opening parenthesis.
• There is a semicolon after the function definition because
anonymous function definitions are expressions
• Function is assigned to a variable, and called later using the
variable’s name.
• When passed to another function that can then call it later,
it is known as a callback.
• Return it from within an outer function so that it can access
the outer function’s variables. This is known as a closure.
Anonymous function
<?php
$var = function ($x) {return pow($x,3);};
echo "cube of 3 = " . $var(3);
?>
Anonymous function as callback
<?php
$arr = [10,3,70,21,54];
usort ($arr, function ($x , $y) {
return $x > $y;
});
foreach ($arr as $x){
echo $x . "\n";
}
?>
Anonymous function as closure
Closure is also an anonymous function that can
access variables outside its scope with the help of
use keyword
<?php
$maxmarks=300;
$percent=function ($marks) use ($maxmarks)
{return $marks*100/$maxmarks;};
echo "marks=285 percentage=". $percent(285);
?>

You might also like