PHP Functions
✅ PHP Functions
In PHP Functions is a block of code that can be reused multiple times.
Functions help make code modular, reusable, and easier to maintain.
Why Use Functions?
-
Reuse the same code without rewriting it
-
Better organization
-
Easier debugging
-
Reduces duplication
-
Makes code cleaner and structured
Types of Functions
-
Built-in Functions →
strlen(),date(),var_dump(), etc. -
User-defined Functions → Functions you create.
📝 PHP User-Defined Function Syntax
Example 1: Basic Function
Example 2: Function with Parameters
Example 3: Function with Multiple Parameters
Example 4: Default Parameter Values
Example 5: Returning Values
Example 6: Functions with Type Declarations
Example 7: Passing Arguments by Reference
Example 8: Variable-Length Argument List (...$args)
Example 9: Anonymous Functions (Closures)
Example 10: Arrow Functions
🎯 Summary
| Feature | Description |
|---|---|
function name() |
Define function |
| Parameters | Pass data into function |
return |
Send data back |
| Default values | Optional parameters |
Reference (&) |
Modify original variable |
...$var |
Unlimited parameters |
| Closures | Anonymous functions |
| Arrow functions | Short, modern syntax |
If you want, I can continue with the next topic:
