Parameters and Return Values

Introduction

Parameters and return values are important concepts in PHP functions. Parameters allow functions to receive data, while return values allow functions to send data back after processing. These features make PHP code flexible, reusable, and organized.

Objectives

By the end of this training, you will be able to:

  • Understand parameters in PHP functions
  • Pass values into functions
  • Use multiple parameters
  • Understand return values
  • Return data from functions
  • Build reusable and efficient PHP programs

What are Parameters

Parameters are variables listed inside the function parentheses. They receive values when the function is called.

Parameters help functions work with different data without rewriting the code.

Syntax of Parameters

function functionName($parameter) {
// code
}

Example of a Function with Parameters

<?php
function greet($name) {
echo "Welcome " . $name;
}

greet("Ali");
?>

Output

Welcome Ali

Multiple Parameters

Functions can accept more than one parameter.

<?php
function addNumbers($num1, $num2) {
echo $num1 + $num2;
}

addNumbers(10, 5);
?>

Output

15

Default Parameter Values

PHP allows default values for parameters. If no value is passed, the default value is used.

<?php
function country($name = "Pakistan") {
echo $name;
}

country();
?>

Output

Pakistan

Passing Different Data Types

Parameters can store strings, integers, floats, arrays, and other data types.

<?php
function student($name, $marks) {
echo $name . " scored " . $marks;
}

student("Ahmed", 90);
?>

What are Return Values

A return value is the result sent back from a function using the return statement.

Return values help store function results in variables and use them later in the program.

Syntax of Return Statement

return value;

Example of Return Values

<?php
function multiply($a, $b) {
return $a * $b;
}

$result = multiply(4, 5);

echo $result;
?>

Output

20

Difference Between Echo and Return

Echo

  • Displays output directly
  • Cannot store the output easily

Return

  • Sends data back from a function
  • Can store the result in variables
  • Makes functions reusable

Using Return with Conditions

<?php
function checkPass($marks) {
if ($marks >= 50) {
return "Pass";
} else {
return "Fail";
}
}

echo checkPass(70);
?>

Output

Pass

Returning Strings

<?php
function message() {
return "PHP is easy to learn";
}

$text = message();

echo $text;
?>

Returning Arrays

<?php
function colors() {
return array("Red", "Blue", "Green");
}

$myColors = colors();

echo $myColors[1];
?>

Output

Blue

Advantages of Parameters and Return Values

  • Improve code reusability
  • Reduce repetition
  • Make programs organized
  • Simplify debugging
  • Increase flexibility in functions
  • Help create dynamic applications

Best Practices

  • Use meaningful parameter names
  • Keep functions simple
  • Return values when needed
  • Avoid unnecessary global variables
  • Use default values carefully

Real World Uses

Parameters and return values are commonly used in:

  • Login systems
  • Calculator applications
  • Form validation
  • Database queries
  • E-commerce websites
  • Dynamic web applications

Final Presentation

In your final presentation, explain:

  • What parameters are
  • How parameters work in PHP
  • Types of parameters
  • What return values are
  • Difference between echo and return
  • Real-world uses of return values
Home Ā» Intermediate PHP > Functions > Parameters and Return Values