0% found this document useful (0 votes)
11 views13 pages

Web Engineering

The document explains PHP functions, which are blocks of code that enhance reusability and maintainability. It covers the basic syntax, components of a function, and examples including user-defined functions, functions with return values, and recursive functions. Additionally, it highlights the benefits of using functions such as simplified debugging, improved readability, and code reusability.

Uploaded by

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

Web Engineering

The document explains PHP functions, which are blocks of code that enhance reusability and maintainability. It covers the basic syntax, components of a function, and examples including user-defined functions, functions with return values, and recursive functions. Additionally, it highlights the benefits of using functions such as simplified debugging, improved readability, and code reusability.

Uploaded by

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

cPHP

FUNCTIONS
By
FATIMA SHOAIB
ZAINAB FATIMA
SARA
PHP Functions

Functions in PHP are blocks of code that perform specific tasks. They improve code
reusability and maintainability. PHP offers built-in functions as well as the ability to
create user-defined functions.
Basic
Syntax
function function_name (parameter1,
parameter2, ...)
{
// Code to be executed
return value; // Optional return statement
}
Components of a Function
 Declaration: A function in PHP is defined using the function keyword followed
by the function name and parentheses ().
 Name: Function names must start with a letter or an underscore and can be
followed by letters, numbers, or underscores. Function names are case-
insensitive in PHP.
 Parameters: Functions can accept arguments (also called parameters) to work
with. Parameters are defined inside the parentheses and can have default values.
 Body: The code block within {} defines the functionality of the function.
 Return Value: Functions can return a value using the return keyword. If a return
is not specified, the function returns null by default .
CREATING A SIMPLE
OUTPUT
function greet($name) {

echo "Hello, $name! How are you?";


}

greet("Alice");
Function with Return Value
function calculateArea($length, $width) {
OUTPUT
$area = $length * $width;
return $area;
}

$area = calculateArea(5, 10);


echo "The area of the rectangle is: $area";
Default Parameter Value
function power($base, $exponent = 2) OUTPUT
{
$result = pow($base, $exponent);
return $result;
}

echo power(2);
Default Parameter Value
function power($base, $exponent = 2) OUTPUT
{
$result = pow($base, $exponent);
return $result;
}

echo power(3,3);
Recursive Function
function factorial($n)
{
OUTPUT
if ($n == 0) {
return 1; }
else {
return $n * factorial($n - 1);
}
}

echo factorial(5); // Output: 120


Built In

TYPES OF For string manipulation, array


handling, file operations
Examples strlen(), array_merge(), and

FUNCTIONS fopen().

User Defined

Developers can create custom


functions to perform specific tasks.
These functions can accept
parameters and return values
Built In Function
$string = "Hello, PHP!"; OUTPUT
echo strlen($string); // Outputs: 11
User Defined Function
function greet($name) OUTPUT
{
return "Hello, $name!";
}

echo greet(“Alice");
Simplified Debugging:
Errors are isolated within functions. If
something goes wrong, it’s easier to pinpoint
the issue within a specific function rather
than searching through a large script

Improved Readability:
Easier to understand and maintainImproved

Benefits of Using reliability means that the code is more likely


to function correctly under various conditions,
reducing the chance of errors..
Functions in PHP Code Reusability:
Write once, reuse multiple times. Saves time
and effort by not rewriting the same code
repeatedly.
Encourages consistency since the same
function can be reused across different parts
of an application.
.

You might also like