functions in php notes
functions in php notes
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())