PHP offers a wide range of functions that can be used for various tasks. A function is defined using the
function keyword, followed by the name of the function, a list of parameters inside parentheses, and
the function body enclosed in curly braces. Functions enable code reusability, which makes the program clearer and
easier to maintain.
The true power of PHP comes from its functions. PHP has more than 1000 built-in functions that are ready for immediate use, while also giving us complete freedom to create our own custom solutions.
In addition to the rich library of built-in functions, PHP allows us to write our own custom functions. A user-defined function name must start with a letter or an underscore (not a number) and is **not** case-sensitive, although it is best practice to write them consistently. The code inside curly braces will not execute when the page loads, but only when the function is officially called in the script using its name and parentheses.
Information can be passed to functions upon calling via **arguments** (variables inside the parentheses).
Arguments act just like local variables within the function itself. A function can accept any number of arguments
separated by commas. Crucially, the return statement stops the execution of the function and "hands"
its final result back to the spot where it was originally called. This allows us to store the calculated value in a
variable or output it later.
PHP provides several built-in functions that allow us to check the existence of functions, analyze their arguments, or retrieve a list of all defined functions within the system:
| Function Name | Description | Example (Code for Simulator) |
|---|---|---|
function_exists() |
Checks if a function with a specific name is already defined and ready to be called (returns true/false). | var_dump(function_exists("is_numeric")); |
is_callable() |
Verifies if the contents of a variable can be called as a function (highly useful for callback functions). | $f = "strlen"; var_dump(is_callable($f)); |
get_defined_functions() |
Returns a multidimensional associative array of all built-in (internal) and user-defined (user) functions. | print_r(get_defined_functions()["user"]); // Outputs only your custom functions |
func_num_args() |
Returns the number of arguments actually passed to the function (used inside the function body). | function test() { echo func_num_args(); } test("a", "b", "c"); // Outputs 3 |
func_get_arg() |
Returns a specific argument from the list of passed arguments based on its index offset (counting from 0). | function test() { echo func_get_arg(1); } test("First", "Second"); // Outputs Second |
func_get_args() |
Returns an indexed array containing all arguments passed to the function. | function test() { print_r(func_get_args()); } test(10, "PHP", true); |
call_user_func() |
Calls a custom or built-in function specified by the first parameter and passes the remaining arguments to it. | echo call_user_func("strtoupper", "hello"); |