Function arguments are values passed to a function when it is called. Parameters receive those values inside the function. Arguments allow us to use the same function with different data.
A parameter is a variable declared in a function definition. An argument is the value supplied when the function is called.
<?php
function greet($name) {
echo "Hello, " . $name;
}
greet("Rahul");
?>
Hello, Rahul
Here, $name is the parameter and
"Rahul" is the argument.
A function can accept one argument to perform a task using the supplied value.
<?php
function square($number) {
echo $number * $number;
}
square(5);
?>
25
Try changing 5 to another number.
The function will calculate the square of that number.
A function can accept two or more arguments. Separate the parameters and arguments with commas.
<?php
function add($a, $b) {
echo $a + $b;
}
add(10, 20);
?>
30
Another example:
<?php
function studentInfo($name, $course, $city) {
echo "Name: " . $name . "<br>";
echo "Course: " . $course . "<br>";
echo "City: " . $city;
}
studentInfo("Amit", "PHP", "Patna");
?>
Name: Amit
Course: PHP
City: Patna
In a regular positional function call, arguments are assigned to parameters in the order they are supplied.
<?php
function displayDetails($name, $age) {
echo "Name: " . $name . "<br>";
echo "Age: " . $age;
}
displayDetails("Rahul", 20);
?>
Name: Rahul
Age: 20
If the arguments are reversed, the values are assigned to the opposite parameters.
<?php
displayDetails(20, "Rahul");
?>
Name: 20
Age: Rahul
PHP allows this without type declarations, but the resulting values may not make sense for your application.
A default argument value is used when the caller omits the corresponding optional argument.
<?php
function greet($name = "Student") {
echo "Welcome, " . $name . "!<br>";
}
greet();
greet("Priya");
?>
Welcome, Student!
Welcome, Priya!
Example with two parameters:
<?php
function calculatePrice($price, $quantity = 1) {
echo $price * $quantity;
}
calculatePrice(100);
echo "<br>";
calculatePrice(100, 3);
?>
100
300
Put required parameters before optional parameters in your function definitions.
By default, PHP passes arguments by value. The parameter receives a copy of the argument's value, so changing that parameter does not change the original variable.
<?php
function changeNumber($number) {
$number = 100;
echo "Inside function: " . $number . "<br>";
}
$value = 50;
changeNumber($value);
echo "Outside function: " . $value;
?>
Inside function: 100
Outside function: 50
The original variable $value remains
unchanged because the function modifies its local
parameter.
Use & before a parameter name when
you want the function to modify the original variable.
<?php
function increaseNumber(&$number) {
$number = $number + 10;
}
$value = 50;
increaseNumber($value);
echo $value;
?>
60
The original variable changes from 50 to 60 because the parameter is passed by reference.
Use references when needed; avoid them when a normal return value would make the code easier to understand.
Parameter type declarations specify the expected type of an argument, helping make function requirements clearer.
<?php
function multiply(int $a, int $b): int {
return $a * $b;
}
echo multiply(4, 5);
?>
20
The parameters are declared as integers, and the return type is also declared as an integer.
Example with a string parameter:
<?php
function welcome(string $name): string {
return "Welcome, " . $name;
}
echo welcome("Amit");
?>
Welcome, Amit
PHP's handling of scalar types can depend on strict typing. With strict typing enabled, incompatible scalar arguments can cause a TypeError rather than being coerced.
This example uses multiple arguments to calculate a student's total and percentage.
<?php
function calculateTotal($english, $math, $computer) {
return $english + $math + $computer;
}
function calculatePercentage($total, $maximumMarks) {
if ($maximumMarks <= 0) {
return 0;
}
return ($total / $maximumMarks) * 100;
}
$name = "Rahul";
$english = 80;
$math = 90;
$computer = 85;
$total = calculateTotal($english, $math, $computer);
$percentage = calculatePercentage($total, 300);
echo "Student: " . $name . "<br>";
echo "Total Marks: " . $total . " / 300<br>";
echo "Percentage: " . number_format($percentage, 2) . "%";
?>
Student: Rahul
Total Marks: 255 / 300
Percentage: 85.00%
This program demonstrates passing multiple arguments, reusing functions, returning values, and using one function's result as another function's input.
1. What is an argument in a function call?
2. What does passing by value normally do?
3. Which symbol is used before a parameter for passing by reference?