← CodePrimer
PHP Beginner Basics

Default function arguments

How do I make a parameter optional?

Default values.

Sample
<?php
function greeter(string $name = 'Guest'): string {
  return 'Hi, ' . $name;
}
echo greeter();
echo greeter('Ada');

Notes

Defaults must be on the right-most parameters.

Keywords: default · argument · function

Related

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share