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();
?>
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");
?>
Welcome Harsh
Welcome Amit
Welcome Rahul
Types of Functions in PHP
- Built-in Functions
- User-Defined Functions
1. Built-in Functions
PHP already provides many ready-made functions.
strlen() Example
<?php
echo strlen("Harsh");
?>
strtoupper() Example
<?php
echo strtoupper("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();
?>
Function with Parameters
<?php
function student($name)
{
echo "Hello $name";
}
student("Harsh");
?>
Multiple Parameters
<?php
function student($name, $course)
{
echo "$name is studying $course";
}
student("Harsh", "PHP");
?>
Function with Return Value
<?php
function add($a, $b)
{
return $a + $b;
}
$result = add(10, 20);
echo $result;
?>
Function with Default Parameter
<?php
function country($name = "India")
{
echo $name;
}
country();
echo "<br>";
country("Japan");
?>
India
Japan
Square Function
<?php
function square($num)
{
return $num * $num;
}
echo square(5);
?>
Real-Life Example: Online Shopping
<?php
function placeOrder($product)
{
echo "$product ordered successfully!";
}
placeOrder("Laptop");
?>
Function Calling Another Function
<?php
function hello()
{
echo "Hello ";
}
function user()
{
hello();
echo "Harsh";
}
user();
?>
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
- Create a function to print your name.
- Create a function that adds two numbers.
- Create a function that finds the largest of three numbers.
- Create a function with a default country name.
- Create a function that returns the square of a number.
- 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.