0% found this document useful (0 votes)
1 views3 pages

User Defined Functions in PHP, how to define a function in php

User-defined functions in PHP allow developers to create reusable code, reducing redundancy and improving maintainability. Functions can accept arguments and return values, enhancing the flexibility of the code. They are defined with the 'function' keyword and can be invoked regardless of their position in the program, with case-insensitivity in function names.

Uploaded by

jeba
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)
1 views3 pages

User Defined Functions in PHP, how to define a function in php

User-defined functions in PHP allow developers to create reusable code, reducing redundancy and improving maintainability. Functions can accept arguments and return values, enhancing the flexibility of the code. They are defined with the 'function' keyword and can be invoked regardless of their position in the program, with case-insensitivity in function names.

Uploaded by

jeba
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/ 3

Creating User-Defined Functions

A function is simply a set of program statements that perform a specific task,


and that can be called, or executed, from anywhere in a program.

Functions are a Good Thing for three important reasons:

■User-defined functions enable developers to extract commonly used pieces of code into
separate packages, thereby reducing unnecessary code repetition and redundancies. This
separation of code into independent subsections also makes the code easier to understand
and debug.

■Because functions are defined once (but used many times), they are easy to maintain. A
change to the function code need only be implemented in a single place—the function
definition—with no changes needed anywhere else.

■Because functions force developers to think in abstract terms (define input and output
values, set global and local scope, and turn specific tasks into generic components), they
encourage better software design and help in creating extensible applications.

Defining and Invoking Functions


In PHP, functions are defined using the special function keyword. This
keyword is followed by the name of the function, a list of arguments (optional) in
parentheses, and the function code itself, enclosed in curly braces.

Syntax:
function functionname(list of arguments)
{
function code to execute;
--------
--------
{

Example : a function without arguments.


<?php
// define a function
function displayShakespeareQuote() {
echo 'Some are born great, some achieve greatness, and some have greatness thrust upon
them';
}
displayShakespeareQuote(); # invoke a function
?>

In PHP 3.x, functions could only be invoked after they had been defined.
In PHP 4.x and PHP 5.0, functions can be invoked even if their definitions appear further
down in the program.
Using Arguments and Return Values
It is possible to create functions that accept different values from the main
program and operate on those values to return different, more pertinent results on each
invocation. These values are called arguments, and they add a whole new level of power and
flexibility to your code.
When a function is invoked with arguments, the variables in the argument list are
replaced with the actual values passed to the function and manipulated by the statements
inside the function block to obtain the desired result.

The Name Game


Function invocations are case-insensitive—PHP will find and execute the named function
even if the case of the function invocation doesn’t match that of the definition

example: a function with a single argument list.


<?php
// define a function
// with a single-argument list
function convertMilesToKilometres($miles)
{
echo "$miles miles = " . $miles * 1.60 . " km";
}
// invoke a function
// pass it a single argument
convertMilesToKilometres(50);
?>

Depending on the value passed to the function, conversion is performed between two
different measurement scales.
Return value:
Usually, when a function is invoked, it generates a return value. This return value is
explicitly set within the function with the return statement.
example:
<?php
// define a function
function getTriangleArea($base, $height)
{
$area = $base * $height * 0.5;
return $area;
}
// invoke a function
echo 'The area of a triangle with base 10 and height 50 is ' . getTriangleArea(10, 50);
?>
 Here, when the getTriangleArea() function is invoked with two arguments, it
performs a calculation and assigns the result to the $area variable.
 This result is then returned to the main program through the return statement.
 when PHP encounters a return statement within a function, it stops processing the
function and returns control to the statement that invoked the function.
 If a functionis invoked with an incorrect number of arguments, PHP will generate a
warning, but still attempt to process the function. To avoid this, either make
arguments optional by setting default values for them or define your function with
support for variable-length argument lists.

You might also like