PHP
PHP - Functions in PHP - How to create and how to use
- What is Function in Programming?
- Functions in PHP
- How to Create & Use Custom User defined Function in PHP
- Function Argument Pass by Value
- Function Pass by Reference
- Recursive Function
¶What is Function in Programming?
In programming aspects, A Functions is a chunk of Code which can be executed multiple times without writing it again and again.
Or,
A Functions is a chunk of code which can do specific type of task.
¶Functions in PHP
In PHP there are almost 1000+ built in functions. We’ve already used some of the built in functions in our previous chapter about - Math, String and Array Built in Functions.
Today, we’ll learn Custom user defined function in PHP.
¶How to Create & Use Custom User defined Function in PHP
Let’s create a simple custom user defined function in PHP -
<?php
function funtionName( $argument1, $argument2, .... ) {
// codes
}
// Use this function
funtionName();
- To start any function in PHP, we start with
function
keyword. - After then adding a space and give function name, here -
functionName
would be your custom function name, eg:printHello()
,calculateAge("2000-01-01")
etc. - Function name must be start with letter or underscore
- Function name is not case-sensitive.
- Inside the first parenthesis
()
, we can pass any number variable, where we’ld call it. We would call that Argument. So, we can pass any number of argument. - After the argument, there would be second curly braces start and stop
{}
. - Inside the second curly braces
{}
we can write our specific tasks code.
Let’s Check some Real-life Function Example -
¶Example 1: Make a function that Print Hello World -
function printHelloWorld() {
echo "Hello World";
}
// Use this
printHelloWorld(); // Hello World
¶Example 2: Make a function that can tell if Age is Child, Adult or Old
function getAge($age) {
if ($age >= 0 && $ageage < 18) {
echo "You are aaaa Child";
} elseif ($age >= 18 && $age < 50) {
echo "You are an AdAdultult";
} elseif ($age >= 50) {
echo "You are Old";
}
}
// Use this
getAge(25); // You are an Adult
getAge(16); // You are a Child
getAge(70); // You are Old
¶Argument in Function
In our above Second example, we’ve passed an age integer value while we use this function.
function getAge($age) {
// codes
}
This passed $age
variable is the Argument. We can use as many argument as our necessity in functions.
But, practically it’s not good to use more than 4-5 arguments, cause thus makes the code more unreadable. Maximum 2-3 arguments for a function is standard.
¶Function Argument Pass by Value
<?php
function getPassByValue($value)
{
$value = $value * 2;
}
$count = 5;
getPassByValue($count);
echo "Pass by value::" . $count . "<br>"; // 5
Normally, if we pass any variable as argument to a function, then we actually pass the value of the variable. And whatever, we do inside that function, does not effect the passed variable.
This is the Passed by value example. Normally we use this kind of stuffs most of the time.
¶Function Pass by Reference
<?php
function getPassByReference(&$value)
{
$value = $value * 2;
}
$count = 5;
getPassByReference($count);
echo "Pass by reference::" . $count; // 10
Now to pass the reference of the variable, we can do that by adding &
before function’s argument name. Like the above example -
function getPassByReference(&$value)
And from calling part, we create a variable $count = 5
, but after calling that function - getPassByReference($count);
After that the $count
variable changed to 10, that’s because, we passed the reference of that variable. And after updating that reference inside the function, we actually updated the $count
variable.
This is the Pass by reference.
For more -
- https://www.php.net/manual/en/language.references.whatare.php
- https://www.php.net/manual/en/language.references.arent.php
- https://www.php.net/manual/en/language.references.pass.php
¶Recursive Function
There is a special type of function which is called directly or indirectly by the own function is Recursive function.
¶Example of a Recursive Function
<?php
function printNumberUpto20($a)
{
if ($a <= 20) {
echo "$a\n";
printNumberUpto20($a + 1); // This call is called Recursive function call
}
}
// Call that function once
printNumberUpto20(1);
?>
So, in Recursive function there would be some criteria -
- Own function will be called. For the above example, look
printNumberUpto20($a + 1);
the function was called itself. - Function must need to break in a condition. For the above example -
$a <= 20
is that condition
PHP Bonus - Online Judge Problem Solving Using PHP
PHP Loosely Type Language - Strict type from PHP 7
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development