Lesson 27 of 50 – PHP Function Arguments
54%

PHP Function Arguments

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.

1. Understanding Parameters and Arguments

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");
?>
Output:
Hello, Rahul

Here, $name is the parameter and "Rahul" is the argument.

2. Passing a Single Argument

A function can accept one argument to perform a task using the supplied value.

<?php
function square($number) {
    echo $number * $number;
}

square(5);
?>
Output:
25

Try changing 5 to another number. The function will calculate the square of that number.

3. Passing Multiple Arguments

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);
?>
Output:
30

Another example:

<?php
function studentInfo($name, $course, $city) {
    echo "Name: " . $name . "<br>";
    echo "Course: " . $course . "<br>";
    echo "City: " . $city;
}

studentInfo("Amit", "PHP", "Patna");
?>
Output:
Name: Amit
Course: PHP
City: Patna
4. Arguments Are Passed in Order

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);
?>
Output:
Name: Rahul
Age: 20

If the arguments are reversed, the values are assigned to the opposite parameters.

<?php
displayDetails(20, "Rahul");
?>
Output:
Name: 20
Age: Rahul

PHP allows this without type declarations, but the resulting values may not make sense for your application.

5. Default Arguments

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");
?>
Output:
Welcome, Student!
Welcome, Priya!

Example with two parameters:

<?php
function calculatePrice($price, $quantity = 1) {
    echo $price * $quantity;
}

calculatePrice(100);
echo "<br>";
calculatePrice(100, 3);
?>
Output:
100
300

Put required parameters before optional parameters in your function definitions.

6. Passing Arguments by Value

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;
?>
Output:
Inside function: 100
Outside function: 50

The original variable $value remains unchanged because the function modifies its local parameter.

7. Passing Arguments by Reference

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;
?>
Output:
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.

8. Type-Declarations for Arguments

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);
?>
Output:
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");
?>
Output:
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.

9. Practical Example: Calculate Student Marks

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) . "%";
?>
Output:
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.

Practice Assignments
  1. Create a function that accepts a name and displays a greeting.
  2. Create a function that accepts two numbers and displays their sum.
  3. Create a function that accepts three numbers and returns their average.
  4. Create a function with a default parameter for a student's course.
  5. Create a function that accepts a price and quantity and returns the total cost.
  6. Create a function that accepts a number and returns its cube.
  7. Write a function that changes a variable using a reference parameter.
  8. Create a function with integer parameter declarations to multiply two numbers.
  9. Create a function that accepts three subject marks and returns the total.

🧠 Quick Quiz

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?