Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 09

Functions in PHP (with Examples)

In this lesson: Create PHP functions with parameters, default values, return, type hints and arrow functions — examples with output.

What is a function

A function is a named block of code that does one job. You write it once, then call it whenever you need that job done. This keeps your program short and avoids repeating the same lines everywhere.

💡 PHP has thousands of built-in functions like strlen() and echo, and you can make your own too.

Define and call

Use the function keyword to define, then call it by its name with brackets.

<?php
  function sayHello() {
      echo "Hello, welcome!";
  }
  sayHello();   // calling it
?>
Hello, welcome!

Defining a function does not run it — it only runs when you call it. You can call the same function as many times as you like.

Parameters and return

Parameters let you pass values in. return sends a result back.

<?php
  function add($a, $b) {
      return $a + $b;
  }
  $sum = add(5, 3);
  echo $sum;
?>
8

Here $a and $b are parameters; 5 and 3 are the arguments. return hands back 8, which we store in $sum.

Default parameters

Give a parameter a default value so it is optional.

<?php
  function greet($name = "Guest") {
      echo "Hi, $name";
  }
  greet("Aman");   // Hi, Aman
  echo "\n";
  greet();          // Hi, Guest
?>
Hi, Aman
Hi, Guest

When no argument is given, the default ("Guest") is used. Defaults must come after non-default parameters in the list.

Scope inside functions

Variables inside a function are local — they do not affect variables outside.

<?php
  $x = 10;
  function change() {
      $x = 99;      // a separate local $x
  }
  change();
  echo $x;          // still 10
?>
10

The function's $x is a different box from the outside $x. To change an outside value, return it and reassign, or pass by reference — the clean way is usually to return.

Common mistakes

  • Defining but never calling. A function does nothing until you call it.
  • Forgetting return. Without it the function gives back NULL, so $sum would be empty.
  • Wrong argument order. Arguments fill parameters left to right — order matters.
  • Expecting inside changes to affect outside variables. They are local unless returned or passed by reference.

Summary

  • A function is reusable named code — define once, call many times.
  • Parameters pass values in; return sends a result back.
  • Default parameters make an argument optional.
  • Variables inside a function are local by default.
  • Functions keep code short, clear and easy to fix.

Next, use functions on text with PHP string functions, or organise data with arrays in PHP.

Frequently Asked Questions

What is the benefit of using functions?
A function lets you write code once and reuse it many times. It keeps your program short, avoids repeating the same lines, and makes bugs easier to fix because the logic lives in one place.
What is the difference between a parameter and an argument?
A parameter is the name in the function definition (like function add($a, $b)). An argument is the actual value you pass when calling it (like add(5, 3)). Parameter is the placeholder; argument is the real value.
What does return do in a function?
return sends a value back to wherever the function was called, and ends the function immediately. Without return, a function does its work but gives back nothing (NULL).
What is a default parameter?
A default parameter has a value that is used if the caller does not provide one. In function greet($name = "Guest"), calling greet() uses "Guest" automatically, while greet("Aman") uses "Aman".
Can a function use a variable from outside it?
Not by default — variables inside a function are separate (local). To use an outside value, pass it in as a parameter (the clean way) or bring it in with the global keyword.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicArrays in PHP Your progress: 9 / 50 (18%) · Lesson 10 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free