In PHP, functions allow you to package a piece of code into a separate named block. This block can be called repeatedly in different places in the program, which makes the code cleaner, more readable, and easier to reuse.
How user functions work
A user function is one that a programmer creates themselves. A function declaration begins with a keyword, followed by its name and a list of parameters (if any). Next comes the body of the function — the actions it must perform.
Functions can return values, pass parameters, perform actions, or simply be a helpful tool for organizing code.
What can be placed inside a function
Inside a function, you can use regular PHP code, call other functions, create variables, write conditions and loops. It is also possible to define new functions and even classes — although this approach is not often used.
Function names: what is important
Function names are subject to the same rules as variable names:
- must start with a letter or underscore;
- can contain letters, numbers, and underscores;
- They are not case sensitive (i.e., myFunction and MYFUNCTION are the same name in PHP).
However, it is considered good style to call functions exactly as they were defined, respecting case sensitivity.
Where and when you can declare functions
Unlike some other programming languages, in PHP you can call functions before they are declared, provided that the declaration is not inside conditions or other functions.
However, if a function is created inside an if, while, or other construct, it will only exist after that block is executed. Such functions are called conditional functions. They are useful when you need to create functions “on the fly.”
It is also permissible to define one function inside another. Such nested functions will only appear in the code after the “parent” function is called.
Features of working with functions in PHP
Global scope: once a function is created, it can be called from any part of the program, provided that it has already been defined at the time of the call.
No function overloading: PHP does not allow you to create multiple functions with the same name, even if they have different parameters.
You cannot redefine or delete an already declared function: such actions will cause an error.
Working with parameters
Functions can accept arguments — data that is passed to them. This allows you to make the same function more versatile and flexible. For example, you can pass different numbers, strings, or arrays — and get different results depending on these input data.
Also possible:
- default arguments — when you are not required to pass a value if a standard one is set;
- variable number of arguments — when you do not know in advance how many parameters will be passed to the function.
Recursion: a function calls itself
PHP allows functions to call themselves — this is called recursion. This approach is often used to solve problems that can be broken down into repetitive subtasks — for example, to process a data tree, calculate factorials, and other algorithms.
But it is important to be careful: if the depth of recursion is too great (usually 100 or more), it can lead to an error due to call stack overflow.
Good to know
- If you need to reuse code, write it as a function.
- Divide responsibilities: one function — one task.
- Don’t overload functions — keep them short and understandable.
- Always try to give functions meaningful names that reflect their purpose.