A Comprehensive Guide to PHP Functions (With Examples)

By Shahnail Khan Last updated : December 11, 2023

PHP Functions

In programming, a function is a named block of code that performs a specific task. PHP functions play a crucial role in web development by allowing you to organize your code into reusable blocks. In this tutorial, we will explore the fundamentals of PHP functions, including how to create and use them effectively.

Creating a PHP Function

To create a PHP function, you use the function keyword, followed by the function name and a pair of parentheses. The function body, where the code executes, is enclosed in curly braces.

Syntax

Below is the syntax to create a PHP function:

function myFunction () {
    // Code to be executed
}

Here, myFunction is a function name.

PHP Function Parameters

Functions can accept input through parameters. Parameters are variables declared in the function definition and used to pass values when calling the function.

Example

This example demonstrates how to use parameters in a PHP function.

<?php
function greet($name)
{
    echo "Hello, $name!";
}

// Calling the function with a parameter
greet("Bharat");
?>

The output of the above example is:

Hello, Bharat!

Here $name is the parameter used to pass the value ("Bharat") when the function is called.

The return Statement in PHP Functions

A return statement sends a value back from the function to the calling code. This is helpful when you want the function to calculate or process data and then provide a result.

Example

This example demonstrates the return statement (return value) in the PHP function.

<?php
function add($num1, $num2)
{
    $sum = $num1 + $num2;
    return $sum;
}

// Calling the function and storing the result
$result = add(5, 3);
echo "Sum: $result";
?>

The output of the above example is:

Sum: 8

In this code, there's a function called 'add' that takes two numbers, adds them together, and then gives back the result using the 'return' statement. When we use the function with numbers 5 and 3, the 'echo' statement shows the sum, which is 8.

PHP Function - Call By Reference

In PHP, when you pass a variable to a function, it is usually passed by value, which means the function receives a copy of the variable's value. However, you can explicitly specify that you want to pass a variable by reference, meaning the function receives a reference to the original variable rather than a copy. This allows the function to modify the original variable directly. To indicate that you are passing a variable by reference, you use the ampersand (&) symbol in both the function declaration and the function call.

Example

This example demonstrates the call by reference in PHP function.

<?php
// Function that takes a parameter by reference
function multiplyByTwo(&$number)
{
    $number *= 2;
}

// Example usage
$value = 5;

echo "Before: $value<br>";

// Passing $value by reference to the function
multiplyByTwo($value);

echo "After: $value";
?>

The output of the above example is:

Before: 5
After: 10

In this example, the multiplyByTwo() function takes a parameter $number by reference. When you pass the variable $value to this function, any changes made to the $number inside the function will directly affect the original variable $value.

PHP Function - Default Argument Value

In PHP, you can assign default values to function parameters. This means that if a value is not provided for a parameter when the function is called, the default value will be used.

Example

This example demonstrates the default argument value in a PHP function.

<?php
function myFunc($website = "Includehelp")
{
    echo "Website name is $website!" . "<br>";
}

// Calling the function without
// providing a value for $website
myFunc();

// Calling the function with
// a specific value for $website
myFunc("Google");
?>

The output of the above example is:

Website name is Includehelp!
Website name is Google!

In the myFunc() function, the parameter $website is assigned a default value of "Includehelp". If you call the function without providing a value for $website, it will use the default value, i.e., "Includehelp". If you provide a value, it will override the default.

PHP Function with Multiple Parameters

Multiple arguments can also be passed as the function parameters with and without default values.

Example

Here's an example with multiple parameters.

<?php
// Function definition with three parameters
function calculateTotal($price, $quantity, $discount)
{
    $total = $price * $quantity * (1 - $discount);
    echo "Total: $total";
}

// Calling the function with specific values
// for the parameters
calculateTotal(20, 2, 0.1);
?>

The output of the above example is:

Total: 36

In this example, we can see that calculateTotal() is the name of the function.

$price, $quantity, and $discount are the parameters. They represent the price of an item, the quantity, and the discount rate, respectively. Inside the function, the three parameters are used to calculate the total cost.

When you call the function calculateTotal (20, 2, 0.1); you provide values 20, 2, and 0.1 for the parameters $price, $quantity, and $discount respectively. The function then calculates the total cost and displays the result.


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.