PHP Part 5: Functions In PHP
Please Subscribe Youtube| Like Facebook | Follow Twitter
Functions in PHP
In this article, we will provide a detailed overview of Functions in PHP and provide examples of how they are used in PHP programming.
PHP offers a plethora of features, one of which is functions. Functions in PHP allow you to encapsulate a piece of code that performs a specific task and reuse it multiple times throughout your code.
In this article, we will discuss functions in PHP, their syntax, types, and examples. We will also look at how to call a function and pass parameters to it. So, let’s get started.
Syntax of Functions in PHP
In PHP, a function is defined using the following syntax:
function function_name(parameter1, parameter2, ...){ // function body return value; }
Let’s break down the syntax:
function: This is the keyword that tells PHP that you are defining a function.
function_name: This is the name of the function that you want to define.
parameter1, parameter2, …: These are the parameters that the function accepts. You can define any number of parameters, separated by commas.
function body: This is the code that the function executes when it is called.
return value: This statement returns a value from the function back to the calling code. If you don’t want to return anything, you can omit this statement.
Types of Functions in PHP
PHP supports two types of functions:
- Built-in Functions
- User-defined Functions
Built-in functions are already defined in PHP and can be used directly in your code. Examples of built-in functions include echo(), print(), strlen(), substr(), array(), isset(), etc.
User-defined functions are created by the developer and can be used in the same way as built-in functions. User-defined functions can be defined and used anywhere in your PHP code.
Creating and Calling a User-defined Function in PHP
Let’s create a simple user-defined function that adds two numbers and returns the result.
function add_numbers($num1, $num2){
$result = $num1 + $num2;
return $result;
}
In the above example, we have created a function called add_numbers() that takes two parameters $num1 and $num2. It adds the two parameters and returns the result.
Now, let’s call this function:
echo add_numbers(5, 10);
Output
The output of the above code will be 15.
Example Function in PHP with no return type and no parameters
<?php
function sayHello() {
echo "Hello, world!";
}
sayHello();
?>
Output
Hello, world!
Passing Parameters to a Function in PHP
You can pass parameters to a function in PHP using two methods:
- Pass-by-Value
- Pass-by-Reference
Pass-by-Value
In pass-by-value, a copy of the variable is passed to the function. Any changes made to the variable inside the function do not affect the original variable outside the function.
function add_one($number){
$number += 1;
return $number;
}
$num = 5;
echo add_one($num); // Output: 6
echo $num; // Output: 5
Output
6
5
In the above example, we have defined a function add_one() that takes a parameter $number. The function adds 1 to the parameter and returns it. We have also defined a variable $num and assigned the value 5 to it. We then call the function add_one() and pass $num as an argument. The function returns 6, but the value of $num remains 5.
Pass-by-Reference
In pass-by-reference, a reference to the variable is passed to the function. Any changes made to the variable inside the function affect the original variable outside the function.
function add_one(&$number){
$number += 1;
return $number;
}
$num = 5;
echo add_one($num); // Output: 6
echo $num; // Output: 6
Output
6 6
In the above example, we have defined a function add_one() that takes a parameter $number passed by reference using the & symbol. The function adds 1 to the parameter and changes its value directly. We have also defined a variable $num and assigned the value 5 to it. We then call the function add_one() and pass $num as an argument. The function changes the value of $num to 6, which is reflected in the output.
Returning Values from a Function in PHP
You can return a value from a function in PHP using the return keyword. A function can return a single value or an array of values.
function calculate($num1, $num2){
$sum = $num1 + $num2;
$difference = $num1 - $num2;
$product = $num1 * $num2;
$quotient = $num1 / $num2;
return array($sum, $difference, $product, $quotient);
}
$result = calculate(10, 5);
echo "Sum: " . $result[0] . "\n";
echo "Difference: " . $result[1] . "\n";
echo "Product: " . $result[2] . "\n";
echo "Quotient: " . $result[3];
Output
Sum: 15
Difference: 5
Product: 50
Quotient: 2
In the above example, we have defined a function calculate() that takes two parameters $num1 and $num2. The function performs four arithmetic operations on the parameters and returns an array of the results. We then call the function and assign the returned array to a variable $result. We use the values in the array to display the results of the arithmetic operations.
Conclusion
Functions in PHP are an essential feature that allows you to reuse code and perform specific tasks multiple times throughout your code. We have covered the syntax of functions in PHP, the two types of functions, creating and calling user-defined functions, passing parameters to functions, and returning values from functions. Understanding how functions work in PHP can make your code more efficient and organized.
PHP Beginner Tutorial Series
- PHP Part 1: Setup and Introduction
- PHP Part 2: Basic Data Types and Variables in PHP
- PHP PART 3: OPERATORS AND EXPRESSIONS IN PHP
- PHP PART 4: CONTROL FLOW STATEMENTS IN PHP
- PHP Part 5: Functions In PHP
- PHP PART 6: ARRAYS IN PHP
- PHP Part 7: String Manipulation In PHP
- PHP Part 8: Object-Oriented Programming In PHP (Classes And Objects)
- PHP Part 9: Object-oriented Programming In PHP (OOP Pillars)
- PHP Part 10: Exception Handling in PHP