How to Create PHP Functions and Functional programming
How to Create PHP Functions and Functional programming Real workflow of PHP Comes from PHP Functions. and with the help of PHP Functions, Build our own functions, it’s more than 1000 build in Functions.
PHP functions is a piece of code that can be reused many times to build function. It can take an argument list as input and return value. There are thousands of built-in functions in PHP.

In PHP Functions, we can define a Conditional function, Function within Function and Recursive function etc.
The advantage of PHP Functions
Code Reusability: PHP functions are defined only once and can be reused many times to build PHP function, Just like as in other programming languages.
Less Code: It saves a lot of code because you don’t need to write the logic many times. you can write the logic only once and reuse it.
Easy to understand: PHP functions separate the programming logic. So we can understand in the easier way the flow of the application because every logic is divided in the form of functions.
PHP User-defined Functions
We can declare and call user-defined functions easily. Let’s see the syntax to declare user-defined functions.
- In PHP functions, we can create or Build our own functions.
- A block of statements that can be used repeatedly in a program.
- it will not execute immediately when a page loads.
- the function will be executed by a call to the function.
Syntax
|
1 2 3 |
function functionname(){ //code to be executed } |
Note: Function name must start with the letter and underscore only like other labels in PHP. It can’t start with numbers or special symbols.
PHP Functions Example
|
1 2 3 4 5 6 |
<?php function welCome(){ echo "TutorialScan PHP Function"; } welCome();//calling function ?> |
|
1 |
TutorialScan PHP Function |
PHP Function Arguments
Hence, pass the information in PHP function through arguments which are separated by a comma.
PHP supports Call by Value (default), Call by Reference, Default argument values and Variable-length argument list.
Let’s see the example to pass a single argument in PHP function.
Syntax
|
1 2 3 4 5 6 7 8 |
<?php function welCome($name){ echo "Hello $name<br/>"; } welCome("Sonoo"); welCome("Vimal"); welCome("John"); ?> |
Output:
|
1 2 3 |
welCome Sonoo welCome Vimal welCome John |
Example:
|
1 2 3 4 5 6 7 8 |
<?php function sayHello($name,$age){ echo "Hello $name, you are $age years old<br/>"; } sayHello("Krishna",27); sayHello("Sonu",29); sayHello("Nitin",23); ?> |
Output:
|
1 2 3 |
Hello Krishna, you are 27 years old Hello Sonu, you are 29 years old Hello Nitin, you are 23 years old |
PHP Call By Reference
The value passed to the function doesn’t modify the actual value by default (call by value). But we can do so by passing the value as a reference.
Finally when the value passed to the function is called by value. To pass the value as a reference, you need to use the ampersand (&) symbol before the argument name.
Example:
|
1 2 3 4 5 6 7 8 9 |
<?php function adder(&$str2) { $str2 .= 'Call By Reference'; } $str = 'Welcome '; adder($str); echo $str; ?> |
|
1 |
Welcome Call By Reference |
PHP Function: Default Argument Value
We can specify a default argument value in the function. While calling PHP function if you don’t specify any argument, it will take the default argument.
|
1 2 3 4 5 6 7 8 |
<?php function sayHello($name="Adil"){ echo "Welcome $name<br/>"; } sayHello("Rajesh"); sayHello();//passing no value sayHello("Esha"); ?> |
Output:
|
1 2 3 |
Hello Rajesh Hello Adil Hello Esha |
PHP Function: Returning Value
|
1 2 3 4 5 6 |
<?php function cube($n){ return $n*$n*$n; } echo "Cube of 4 is: ".cube(4); ?> |
Output:
|
1 |
Cube of 4 is: 64 |
Resource
PHP Function