Types of Functions in PHP
✅ 1. User-Defined Functions
These are functions you create yourself to organize and reuse code.
Syntax:
✅ Example:
When to Use:
-
When a block of code is repeated often
-
For better organization and readability
-
To handle specific logic (e.g. calculations, validations)
✅ 2. System-Defined (Built-in) Functions
These are predefined functions provided by PHP. You can use them directly without defining them.
Examples:
| Category | Function | Description |
|---|---|---|
| String | strlen() |
Returns length of a string |
| Array | array_push() |
Adds item to end of an array |
| Math | round() |
Rounds a number |
| Date/Time | date() |
Formats date and time |
| File | fopen() |
Opens a file |
| Misc | isset() |
Checks if a variable is set |
✅ Example:
Comparison Table
| Feature | User-Defined | System-Defined (Built-in) |
|---|---|---|
| Who defines it? | You (the programmer) | PHP itself |
| Customizable? | Yes | No (fixed behavior) |
| Usage | Must be declared before use | Can be used anytime |
| Examples | greet(), calculateSum() |
strlen(), array_push(), date() |
