Default / Optional Params
Last updated
What is Default / Optional Params?
In PHP, you can assign default values to your function parameters. These are called default or optional parameters. This is exceptionally useful when you want to make the function argument optional so if a value is not provided when the function is called, then the default value is used instead. Here's a simple code example:
function greet($name = "guest") {
echo "Hello, $name!";
}
greet(); // Outputs: Hello, guest!
greet("John"); // Outputs: Hello, John!
Free resources to learn Default / Optional Params
- Official docsDefault Parameters
More in stage 5: Functions
Default / Optional Params: common questions
Can I learn Default / Optional Params in Hindi?
This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Default / Optional Params in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.
Is this Default / Optional Params tutorial for beginners?
It is lesson 32 of 120 in the free PHP roadmap, in stage 5, Functions. If you are new to PHP, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.