PHP Basic Tutorial
MySQL Connection
Functions are blocks of code that perform specific tasks and can be reused throughout your code. Functions allow you to encapsulate logic, improve code readability, and reduce duplication. PHP offers built-in functions, and you can also define your own custom functions.
A user-defined function declaration starts with the function
keyword, followed by the function's name, parentheses (which may include parameters), and curly braces {}
that enclose the function's code.
Try it yourself
Note
The name must start with a letter or an underscore. Function names are case-insensitive.
Functions can accept parameters, which are values that you pass to the function when calling it. Parameters allow the function to work with different data each time it is invoked.
function functionName($parameter1, $parameter2, ...) { // Code that uses the parameters }
Try it yourself
PHP function accept multiple parameters within the parentheses when declaring the function. Each parameter is separated by a comma.
Try it yourself
This allows the function to be called without explicitly passing an argument for that parameter. If no value is passed for the parameter, the default value is used.
function functionName($param1 = defaultValue1, $param2 = defaultValue2, ...) { // Code that uses the parameters }
Try it yourself
greet($name = "Guest")
has a default value of "Guest"
for the $name
parameter."Guest"
as the value for $name
."Alice"
), it overrides the default value and uses the provided value.A function can return a value using the return
statement.
Try it yourself
Function parameters are passed by value by default, which means that a copy of the value is used in the function and the variable that was passed into the function cannot be changed.
However, in some cases, you may want to modify the original variable passed to the function. For this, you can pass the argument by reference. When passing by reference, any changes made to the parameter inside the function will directly affect the original variable outside the function.
Try it yourself
Create functions that accept a variable number of arguments using the ...
(spread operator). This allows you to handle a flexible number of input parameters, making your function more dynamic and versatile. This is also called a variadic function.
The variadic function argument becomes an array.
Try it yourself
You can also mix regular parameters with variable-length arguments:
Try it yourself
PHP does not support having a required parameter after a variadic parameter (like ...$items
).
If the variadic argument is not the last argument, you will get an error.
Try it yourself
PHP is a loosely typed (or dynamically typed) language, meaning you do not need to explicitly define data types for variables, function parameters, or return values. PHP automatically converts the data type depending on how the variable or value is used.
Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In the following example we try to send both a number and a string to the function without using strict
:
Try it yourself
strict_types
To enable strict typing for a file, you need to declare strict_types=1
at the top of your PHP file.
In the following example we try to send both a number and a string to the function, but here we have added the strict
declaration:
Try it yourself