Functions in PHP (Hypertext Preprocessor)
What is function and explain how to create a function? Explain how to call a
function with an example program?
A function in PHP is a block of reusable code that performs a specific task. Functions help in
writing clean, modular, and efficient code, making the program more readable and
maintainable.
Why Use Functions?
Code Reusability – Write once, use multiple times.
Improves Readability – Makes the program structured and easier to understand.
Easier Maintenance – Fixing bugs or making changes becomes simpler.
Reduces Redundancy – Eliminates code duplication.
Types of Functions in PHP:
PHP supports two main types of functions:
1. Built-in Functions
2. User-defined Functions
1. Built in functions:
PHP provides a wide set of built-in functions to perform common tasks like string
manipulation, array processing, file handling, mathematical operations, and more.
Categories of Built-in Functions:
String Functions (e.g., strlen(), str_replace())
Array Functions (e.g., array_push(), array_merge())
Math Functions (e.g., abs(), pow())
Date & Time Functions (e.g., date(), strtotime())
File Handling Functions (e.g., fopen(), fwrite())
Error Handling Functions (e.g., die(), error_reporting())
User-Defined Functions in PHP
A user-defined function in PHP is a function that you create yourself to perform a specific task.
These functions help in code reusability, modularity, and maintainability by reducing
redundancy and organizing code into smaller, manageable parts.
Syntax of a User-Defined Function
A function in PHP is defined using the function keyword, followed by the function name and
parentheses ().
Basic Syntax
function functionName()
{
// Function body (code to execute)
}
Example
<?php
function greet() //called function
{
echo "Hello, welcome to PHP!";
}
greet(); // Calling the function
?>
Output
Hello, welcome to PHP!
Types of User-Defined Functions in PHP:
1. Functions Without Parameters
2. Functions With Parameters
3. Functions With Default Parameters
1. Functions Without Parameters
A simple function that does not take any input values.
Example
<?php
function sayHello() {
echo "Hello, PHP!";
}
sayHello(); // Output: Hello, PHP!
?>
2. Functions With Parameters
A function can accept input values (parameters) to perform operations.
Example
<?php
function greetUser($name) {
echo "Hello, $name!";
}
greetUser("Hema"); // Output: Hello, Hema!
?>
Multiple Parameters
<?php
function add($a, $b) {
echo "Sum: " . ($a + $b);
}
add(5, 10); // Output: Sum: 15
?>
3. Functions With Default Parameters
If a parameter is not passed, a default value is used.
Example
<?php
function greet($name = "Guest") {
echo "Hello, $name!";
}
greet("Hema"); // Output: Hello, Hema!
greet(); // Output: Hello, Guest!
?>
Advantages of Using Functions in PHP
✅ Code Reusability – Write once, use multiple times.
✅ Modular Approach – Organize code into smaller blocks.
✅ Easy Debugging – Fixing bugs is easier.
✅ Reduces Redundancy – Avoids repetition of code.
✅ Improves Readability – Makes programs easier to understand.