Course Content
State Management
0/1
Regular Expressions?
0/1
About Lesson

Function Parameters:

Function parameters allow you to provide input to the function and perform operations on that input. There are several types of parameters:

  1. Required Parameters: These are parameters that must be passed to the function for it to work properly. If you define a parameter in a function’s declaration, but the caller doesn’t pass a value for it, PHP will throw an error.

Example:

PHP
<?php

function greet($name) {

    echo "Hello, $name!";

}

greet("John"); // Outputs: Hello, John!
 
?>

2.Default Parameters: Default parameters allow you to define values for function parameters in case the caller doesn’t provide them when calling the function. This feature makes paramteres optional providing a default value if no value is provided by the caller.

Example:

PHP
<?php

function greet($name = "World") {

    echo "Hello, $name!";

}

greet(); // Outputs: Hello, World!

greet("Alice"); // Outputs: Hello, Alice!

?>

In the above example, the ‘greet( )’ function has a parameter ‘$name’ with a default value of “world”. If no argument is passed when calling ‘greet( )’, it will use the default value “world”. However, if an argument is provided, it will override the default value.

Key points about default parameters in PHP:

  1. Default parameters can be any valid PHP expression: This means you can use constants, variables, or even function calls to define default values.
  2. Default parameters must be at the end of the parameter list: Ifyou have multiple parameters in a function declaration, parameters with default values must come after paramters without default values. For example, you cannot define  a default parameter before a non-default one.
  3. Default parameters provide flexibility: They allow you to define functions that can be called with fewer arguments, providing sensible defaults when necessary.

Another example:

PHP
<?php

function sayGreeting($greeting, $name = "Guest") {

    echo "$greeting, $name!";

}

sayGreeting("Hello"); // Outputs: Hello, Guest!

sayGreeting("Hi", "Alice"); // Outputs: Hi, Alice!

?>

In the ‘sayGreeting( )’ function, ‘$greeting’ is a required parameters, while ‘$name’ is optional with a default value of “Guest”. You can choose to provide a name or rely on the default value.Top of Form

3.Variable-Length Parameter Lists (Variadic Functions): You can use the ’…’ operator before the last parameter in a function’s parameter list to indicate that the function can accept an arbitrary number of arguments.

Example:

PHP
<?php

function sum(...$numbers) {

    $total = 0;

    foreach ($numbers as $num) {

        $total += $num;

    }

    return $total;

}

echo sum(1, 2, 3, 4); // Outputs: 10

?>

4.Passing by reference: You can pass parameters by reference using the ‘&’ symbol before the parameter name. This allows the function to directly modify the original variable passed to it.

Example:

PHP
<?php

function double(&$num) {

    $num *= 2;

}

$value = 5;

double($value);

echo $value; // Outputs: 10

?>

These are the basic ways you can work with function parameters in PHP.