0
0
PHPprogramming~5 mins

Parameters and arguments in PHP

Choose your learning style9 modes available
Introduction

Parameters and arguments help you send information to functions so they can work with different data.

When you want a function to add two numbers you give it the numbers as arguments.
When you want to greet different people, you pass their names as arguments.
When you want to calculate the area of different rectangles, you pass width and height as arguments.
Syntax
PHP
<?php
function functionName($parameter1, $parameter2) {
    // code using parameters
}

functionName(argument1, argument2);

Parameters are placeholders in the function definition.

Arguments are the actual values you give when calling the function.

Examples
This function takes one parameter $name and prints a greeting using the argument "Alice".
PHP
<?php
function greet($name) {
    echo "Hello, $name!";
}
greet("Alice");
This function adds two numbers passed as arguments and returns the sum.
PHP
<?php
function add($a, $b) {
    return $a + $b;
}
$result = add(5, 3);
echo $result;
This function uses two parameters to describe a fruit's color and name.
PHP
<?php
function describe($color, $fruit) {
    echo "The $fruit is $color.";
}
describe("red", "apple");
Sample Program

This program defines a function multiply with two parameters. It multiplies the arguments and prints the result.

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

$product = multiply(4, 7);
echo "4 times 7 is $product.";
OutputSuccess
Important Notes

Parameters must be inside the parentheses in the function definition.

Arguments must match the order of parameters when calling the function.

You can have as many parameters as you need, separated by commas.

Summary

Parameters are names used in function definitions to accept values.

Arguments are the actual values you pass to functions when calling them.

Using parameters and arguments makes functions flexible and reusable.