PHP Functions
PHP true power is derived from its functions.
PHP includes more than 1000 built-in functions, and you may even develop your own individual functions. These functions are used to execute a particular operation from within a script.
PHP User Defined Functions
A function is a collection of statements that may be reused throughout a program.
When a page loads, a function does not run by itself.
By calling the function, a function will be performed.
Create a User Defined Function in PHP
The function
keyword is used to create a user-defined function in a PHP program.
Syntax :-
function functionName() {
code to be executed;
}
Note :-
- A function name must be start with a letter or an underscore characters. There is no case sensitivity in function names.
- The opening curly brace ( { ) denotes the start of the function code, and the closing curly brace ( } ) denotes the end of the function.
- To invoke or call the function, just type its name followed by brackets ().
Example :- In the following example, we define a method called sayHello()
and it
print a message "Hello".
Output :-
PHP Function Arguments
A function may be incomplete without arguments. Because in many situation, we have to take some inputs from users or other methods to finish the task.
Inputs can be sent via arguments to functions. An argument resembles a variable.
Arguments within the parenthesis are provided by the function name. You may add as many arguments as you would like, only separate them with a comma.
Example 1 :- The following example contains a function with a only one argument ($name).
";
}
sayWelcome("James");
sayWelcome("Rohan");
sayWelcome("Peter");
?>
Output :-
Welcome Rohan.
Welcome Peter.
Example 2 :- The following example has a function with two arguments sum($a and $b) and make an addition of two numbers.
";
}
sum(1, 2);
sum(5, 10);
sum(20, 5);
?>
Output :-
The sum is : 15
The sum is : 25
Note:- You have to create a function only once but you can call a function as many times you want.
PHP Default Argument Value
Every function argument must be provided while calling the function. You cannot missing or skip an argument.
But when a function has default value argument, We can call the function with or without the arguments if you set a default value to an argument.
Example :- A default parameter is used in the following example.
";
}
setPrice(350);
setPrice(); // will use the default value of 50
setPrice(135);
?>
Output :-
The price is : 50
The price is : 135
Related Links
PHP Functions - Returning values
The return
statement is used to allow a function to return a value.
Example :-
";
echo "5 + 6 = " . sum(5, 6);
?>
Output :-
5 + 6 = 11
PHP Return Type Declarations
For the return
statement, PHP 7 now enables Type Declarations. When you enable the strict
requirement, it will raise a "Fatal Error" if there is a type mismatch, much like it does when you enable the type declaration for function arguments.
To specify the return
function type, enter colon (:) and type curly ({) bracket on declared function, just before opening.
Example 1 :- In the next example, we provide the function's return type.
Output :-
Example 2 :- You can provide a different return
type than the argument types,
but you must ensure that the return is of the right type :
Output :-
Passing Arguments by Reference
Arguments are normally supplied by value in PHP, which implies that the function uses a copy of the value and the variable supplied into the function cannot be altered.
Changes to the argument modify the variable that was passed in when a function parameter is given by reference. The & operator is used to convert a function parameter into a reference.
Example :- To make changes to a variable, use a pass-by-reference parameter.
Output :-
Related Links
PHP is a Loosely Typed Language
Depending on the value of the variable, PHP automatically assigns a data type to it. Because the data types are not strictly defined, you may do operations such as adding a string to an integer without producing an error.
Type declarations were added to PHP 7 to provide for more flexibility. This allows us to define the intended data type when creating a function, and the strict
declaration ensures that if the data type mismatches, a "Fatal Error" will be thrown.
Example 1 :- In the next example, we attempt to submit a number as well as a string to the function without using strict
:
Output :-
Declare(strict types=1)
; must be used to indicate strict
. The first line of the PHP file must contain this information.
Example 2 :- In the next example, we attempt to submit both a number and a string to the function, but this time we include the strict
declaration.
Output :-
The strict
declaration requires that objects be utilized in the way that they were intended.