Functions
Last updated
What is Functions?
Functions in PHP are self-contained blocks of code that carry out specific tasks and can be reused throughout your application. A function is defined with the word "function" followed by a name, and it should return a value using the "return" statement. To use a function, you simply need to call it by its name. You can also pass parameters to functions to influence how they work. Here's a simple function:
function greet($name) {
return "Hello, " . $name;
}
echo greet("John"); // Outputs: Hello, John
In the code above, "greet" is a function that takes one parameter "name". It concatenates "Hello, " with the name and returns the result.
Free resources to learn Functions
- Official docsFunctions
More in stage 5: Functions
Functions: common questions
Can I learn Functions 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 Functions 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 Functions tutorial for beginners?
It is lesson 29 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.