✍️

PHP Functions Explained Step-by-Step for Beginners

PHP Functions Explained with Examples

PHP Functions Explained

Built-in and User-Defined Functions with Real-Life Examples

Introduction

When writing PHP programs, you often need to perform the same task multiple times. Instead of writing the same code repeatedly, PHP provides Functions.

A function is a reusable block of code that performs a specific task whenever it is called.

  • Easier to read
  • Easier to maintain
  • Reusable
  • Faster to develop

What is a Function in PHP?

A function is a block of code that executes only when it is called.

Syntax

function functionName() {
    // Code
}

functionName();

Real-Life Example

Think of a function like a coffee machine. You press a button and it prepares coffee.

Press Button
      ↓
Coffee Machine Works
      ↓
Coffee Ready

Simple Function Example

<?php

function welcome()
{
    echo "Welcome to Learn PHP!";
}

welcome();

?>
Output: Welcome to Learn PHP!

Why Do We Need Functions?

Without Function

<?php

echo "Welcome Harsh";
echo "<br>";

echo "Welcome Amit";
echo "<br>";

echo "Welcome Rahul";

?>

With Function

<?php

function welcomeUser($name)
{
    echo "Welcome $name <br>";
}

welcomeUser("Harsh");
welcomeUser("Amit");
welcomeUser("Rahul");

?>
Output:
Welcome Harsh
Welcome Amit
Welcome Rahul

Types of Functions in PHP

  1. Built-in Functions
  2. User-Defined Functions

1. Built-in Functions

PHP already provides many ready-made functions.

strlen() Example

<?php

echo strlen("Harsh");

?>
Output: 5

strtoupper() Example

<?php

echo strtoupper("php");

?>
Output: PHP

Popular Built-in Functions

Function Purpose
strlen() Count characters
str_word_count() Count words
strtoupper() Convert to uppercase
strtolower() Convert to lowercase
date() Current date
sqrt() Square root

2. User-Defined Functions

These are functions created by the programmer.

<?php

function greet()
{
    echo "Good Morning!";
}

greet();

?>
Output: Good Morning!

Function with Parameters

<?php

function student($name)
{
    echo "Hello $name";
}

student("Harsh");

?>
Output: Hello Harsh

Multiple Parameters

<?php

function student($name, $course)
{
    echo "$name is studying $course";
}

student("Harsh", "PHP");

?>
Output: Harsh is studying PHP

Function with Return Value

<?php

function add($a, $b)
{
    return $a + $b;
}

$result = add(10, 20);

echo $result;

?>
Output: 30

Function with Default Parameter

<?php

function country($name = "India")
{
    echo $name;
}

country();
echo "<br>";
country("Japan");

?>
Output:
India
Japan

Square Function

<?php

function square($num)
{
    return $num * $num;
}

echo square(5);

?>
Output: 25

Real-Life Example: Online Shopping

<?php

function placeOrder($product)
{
    echo "$product ordered successfully!";
}

placeOrder("Laptop");

?>
Output: Laptop ordered successfully!

Function Calling Another Function

<?php

function hello()
{
    echo "Hello ";
}

function user()
{
    hello();
    echo "Harsh";
}

user();

?>
Output: Hello Harsh

Variable Scope

Local Variable

<?php

function demo()
{
    $x = 10;
    echo $x;
}

demo();

?>

Global Variable

<?php

$x = 100;

function demo()
{
    global $x;
    echo $x;
}

demo();

?>

Advantages of Functions

  • Reduce duplicate code
  • Improve readability
  • Easy debugging
  • Easy maintenance
  • Reusable code
  • Better organization

Common Interview Questions

1. What is a function in PHP?

A function is a reusable block of code that performs a specific task.

2. What are the two types of functions?

Built-in Functions and User-Defined Functions.

3. What is the difference between echo and return?

echo displays output directly.

return sends a value back to the caller.

Practice Questions

  1. Create a function to print your name.
  2. Create a function that adds two numbers.
  3. Create a function that finds the largest of three numbers.
  4. Create a function with a default country name.
  5. Create a function that returns the square of a number.
  6. Create a function that checks even or odd number.

Conclusion

Functions are one of the most important features in PHP because they allow developers to write clean, reusable, and maintainable code.

❮ Previous Next ❯