Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.

Get Started Now!

What is Function Arguments in PHP ?

PHP function argument refers to a value that is passed into a function when it is called. PHP Arguments supports Call by Value, Call by Reference, Default Argument Values and Variable-length argument.

1. Call by Value:

In Call by Value, the value of a variable is passed directly. This means if the value of a variable within the function is changed, it does not get changed outside of the function. 

Example:

<?php  
function increment($i)  
{  
    $i++;  
}  
$i = 10;  
increment($i);  
echo $i;  
?>  

Output:

10

2. Call by Reference:

In PHP, call by reference is a mechanism in which a function can directly modify the original value of a variable outside of the function. By default, function arguments are passed by value in PHP, which means that a copy of the argument’s value is passed to the function. However, you can pass arguments by reference using the & symbol to allow the function to modify the original value of the variable.

Example:

<?php  
function increment(&$i)  
{  
    $i++;  
}  
$i = 10;  
increment($i);  
echo $i;  
?> 

Output:

11

3. Default Argument Values:

When you calling PHP function if you don’t specify any argument, it will take the default argument.

Example:

<?php  
function Hello($name="Amit"){  
echo "Hello $name <br>";  
}  
Hello("Abishek");  
Hello();//passing no value  
Hello("Vijay");  
?>  

Output:

Hello Amit
Hello Abhishek
Hello Vijay

4. Variable Length Argument:

It is used when we need to pass n number of arguments in a function. To use this, we need to write three dots inside the parenthesis before the argument. 

Example:

<?php  
function add(...$nums) {  
    $sum = 0;  
    foreach ($nums as $n) {  
        $sum += $n;  
    }  
    return $sum;  
} 
echo add(2, 4, 6, 8);  
?>  

Output:

20

Related Posts

How to Install Node.js and npm (Step-by-Step Guide)

How to Install Node.js and npm (Step-by-Step Guide) Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine, and npm (Node Package Manager) is the package Read More

Read More

Creating a WordPress Post with Google Image Search Results in PHP

In this blog post, we will explore a PHP script that utilizes the Google Custom Search API to search for images based on user-defined keywords. We’ll also Read More

Read More

How to Automatically Generate Image Search Queries Based on Keywords Using Google API

To automatically generate image search queries using Google API, you can use the Custom Search JSON API. Here’s a step-by-step guide: Step 1: Create a Google Cloud Read More

Read More

How to Build an Image Search Engine with Google Custom Search API and PHP

Building an image search engine using the Google Custom Search API and PHP involves several steps. Here is a detailed guide: Prerequisites Step 1: Create a Custom Read More

Read More

What is Composer, and why is it used in PHP development?

Composer is a dependency management tool specifically designed for PHP. It streamlines the process of managing libraries and packages required for PHP projects, making development more efficient Read More

Read More

Understanding PHP Data Types: A Comprehensive Guide with Examples

In PHP, data types refer to the various kinds of data that can be stored and manipulated within the language. PHP is a loosely typed language, meaning Read More

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x