Parameters & Return Values in PHP: A Complete Beginner’s Guide

11/19/2025
All Articles

php parameters with example

Parameters & Return Values in PHP: A Complete Beginner’s Guide

Parameters & Return Values in PHP: A Complete Beginner’s Guide

Functions become powerful when you can pass data into them and receive results back. In PHP, this is done using parameters and return values. Understanding these two concepts helps you build dynamic, reusable, and professional PHP applications.

This guide explains parameters, arguments, return values, default values, type declarations, and includes examples to make learning easier.


What Are Parameters in PHP?

Parameters are variables defined in the function declaration. They allow you to send data into a function.

Example:

function greet($name) {
    echo "Hello, $name";
}

Here, $name is a parameter.
When calling:

greet("Shubham");

"Shubham" is an argument.


Function with Single Parameter

<?php
function displayAge($age) {
    echo "Your age is $age";
}

displayAge(25);
?>

⭐ Function with Multiple Parameters

<?php
function add($a, $b) {
    echo $a + $b;
}

add(5, 10); // Output: 15
?>

Default Parameter Values

A default value is used when no argument is passed.

<?php
function greetUser($name = "Guest") {
    echo "Welcome, $name!";
}

greetUser();          // Output: Welcome, Guest!
greetUser("Amit");    // Output: Welcome, Amit!
?>

Passing Parameters by Value (Default)

PHP normally passes parameters by value, meaning the original variable is not changed.

<?php
function increment($num) {
    $num++;
}

$value = 10;
increment($value);
echo $value;  // Output: 10 (unchanged)
?>

Passing Parameters by Reference

Use & to modify the original value.

<?php
function incrementRef(&$num) {
    $num++;
}

$value = 10;
incrementRef($value);
echo $value; // Output: 11
?>

Variable-length Parameters (…)

PHP allows unlimited arguments using ... (variadic functions).

<?php
function sumAll(...$numbers) {
    return array_sum($numbers);
}

echo sumAll(1, 2, 3, 4); // Output: 10
?>

What Are Return Values in PHP?

A return value is the data that a function sends back to the calling code.

Syntax:

return value;

Function with Return Value

<?php
function multiply($x, $y) {
    return $x * $y;
}

$result = multiply(5, 4);
echo $result; // Output: 20
?>

Return Statements Stop Function Execution

<?php
function test() {
    echo "Step 1";
    return;
    echo "Step 2"; // This will NOT run
}

test();
?>

Return Type Declarations

PHP allows you to specify what type of value a function should return.

<?php
function divide(int $a, int $b): float {
    return $a / $b;
}

echo divide(10, 3);
?>

⭐ Practical Example: Calculate Discount

<?php
function getDiscount($price, $percent) {
    return $price - ($price * $percent / 100);
}

echo getDiscount(1000, 20); // Output: 800
?>

Practical Example: Full Name Generator

<?php
function fullName($first, $last) {
    return $first . " " . $last;
}

echo fullName("Shubham", "Mishra");
?>

Common Mistakes

  • Forgetting to return a value when needed
  • Confusing arguments and parameters
  • Passing too many or too few arguments
  • Not using default parameters properly

Why Parameters & Return Values Matter

These concepts help you:

  • Reuse code efficiently

  • Break logic into smaller functions

  • Reduce duplication

  • Improve readability

  • Build flexible, powerful PHP applications


Conclusion

Understanding parameters and return values is essential for writing clean, modular, and professional PHP functions. Master these basics, and you’ll be ready to handle more advanced concepts like recursion, OOP methods, and closures.

Article