"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Tuesday, January 23, 2024

Default Arguments in Functions in PHP

Default Arguments in Functions in PHP

In PHP, default arguments in functions allow you to specify default values for parameters. If a value is not provided for a parameter when calling the function, the default value is used. This provides flexibility when calling functions, as certain parameters can be optional with predefined defaults.

Example:

<?php

// Function with default values for parameters

function greetUser($name = "Guest", $greeting = "Hello") {

    echo "$greeting, $name!";

}

// Call the function without providing specific values

greetUser(); // Outputs: Hello, Guest!

// Call the function with specific values

greetUser("John", "Good morning"); // Outputs: Good morning, John!

?>

Output:

Hello, Guest!

Good morning, John!

Explanation

  1. The function greetUser is defined with two parameters, $name and $greeting, both of which have default values ("Guest" for $name and "Hello" for $greeting).
  2. Inside the function, it echoes a greeting message using the provided or default values.
  3. The function greetUser is called without providing specific values for $name and $greeting.
  4. As a result, the default values ("Guest" and "Hello") are used, and the function echoes: "Hello, Guest!"
  5. The function greetUser is called with specific values for $name ("John") and $greeting ("Good morning").
  6. The provided values override the default values, and the function echoes: "Good morning, John!"

Example 2:

<?php

// Function with default values for parameters

function generateGreeting($name, $occasion = "day", $greeting = "Hello") {

    echo "$greeting, $name! Have a good $occasion.";

}

// Call the function without providing specific values

generateGreeting("Alice"); // Outputs: Hello, Alice! Have a good day.

// Call the function with specific values

generateGreeting("Bob", "evening", "Good evening"); // Outputs: Good evening, Bob! Have a good evening.

?>

Output:

Hello, Alice! Have a good day.

Good evening, Bob! Have a good evening.

Explanation:

  1. The generateGreeting function takes three parameters: $name, $occasion, and $greeting. Both $occasion and $greeting have default values.
  2. If no values are provided for $occasion and $greeting, the default values ("day" for $occasion and "Hello" for $greeting) are used.
  3. The function is called with different sets of arguments to demonstrate the flexibility of providing specific values or using the default values.

 Example 3:

<?php

// Function with default values for parameters

function calculateArea($length, $width = 5) {

    $area = $length * $width;

    return $area;

}

// Call the function with specific values

$rectangle1 = calculateArea(8, 6);

echo "Area of Rectangle 1: $rectangle1\n"; // Outputs: Area of Rectangle 1: 48

// Call the function without providing width (uses default value)

$rectangle2 = calculateArea(10);

echo "Area of Rectangle 2: $rectangle2\n"; // Outputs: Area of Rectangle 2: 50

?>

Output:

Area of Rectangle 1: 48

Area of Rectangle 2: 50

Explanation:

  1. The calculateArea function takes two parameters: $length and $width. The $width parameter has a default value of 5.
  2. If a specific value for $width is provided when calling the function, it overrides the default value. If not provided, the default value is used.
  3. The function calculates the area of a rectangle using the provided or default values.

No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template