- Declaring Variables in PHP
- PHP Data Types
- PHP Arrays
- Types of PHP Operators
- PHP Strings
- PHP Expressions
- PHP Control Structures
- PHP Functions
- PHP Form Handling – Read Form Inputs & Handle File Uploads
- How to Connect PHP to MySQL Database Using MySQLi
- Executing Simple Queries in PHP
- Handling Results in PHP
- Handling Sessions and Cookies
PHP Functions
PHP functions help us write clean, reusable code by grouping logic into blocks. PHP has many built-in functions, and we can also create our own.
What is a Function in PHP?
A function is a named block of code that runs only when we call it. Functions help us avoid repeating code again and again.
Functions can take arguments (input values)
They can return a result
They are not case-sensitive
Built-in vs User-defined Functions
Built-in Functions
PHP provides 1000+ built-in functions for various tasks like string handling, array processing, date formatting, etc.
Example:
echo strlen("Hello"); // Output: 5
User-defined Functions
You can create your own functions to solve specific problems in your program.
Creating a User-defined Function
Use the function
keyword to define a function.
Syntax:
function functionName() {
// code to run
}
Example:
function greet() {
echo "Hello, world!";
}
greet();
Output:
Hello, world!
PHP Function with Parameters
You can pass data to a function using parameters.
Example:
function showName($name) {
echo "Welcome, $name!<br>";
}
showName("Amit");
showName("Priya");
Output:
Welcome, Amit!
Welcome, Priya!
Function with Multiple Parameters
You can also pass more than one parameter.
Example:
function studentDetails($name, $year) {
echo "$name joined in $year<br>";
}
studentDetails("Raj", 2022);
studentDetails("Neha", 2023);
Output:
Raj joined in 2022
Neha joined in 2023
Default Argument Value
If a parameter is not passed, PHP can use a default value.
Example:
function setHeight($minHeight = 100) {
echo "Minimum height: $minHeight<br>";
}
setHeight(150);
setHeight();
Output:
Minimum height: 150
Minimum height: 100
Returning Values from a Function
Use return
to send back a result from a function.
Example:
function add($a, $b) {
return $a + $b;
}
echo add(4, 5);
Output:
9
Pass by Value vs Pass by Reference
By default, PHP passes arguments by value (a copy). But you can use &
to pass by reference.
Example (by value):
function update($x) {
$x += 10;
}
$a = 5;
update($a);
echo $a; // Output: 5
Example (by reference):
function update(& $x) {
$x += 10;
}
$a = 5;
update($a);
echo $a; // Output: 15
Best Practices for PHP Functions
Use clear and meaningful names
Avoid too many parameters
Keep functions short and focused
Always return a value if needed
Real-world Example
Example: A function to calculate total price including GST
function getTotalPrice($amount, $gst = 18) {
return $amount + ($amount * $gst / 100);
}
echo getTotalPrice(1000); // Output: 1180
Conclusion
PHP functions help make your code simple, reusable, and maintainable. You can use both built-in and user-defined functions to manage logic and reduce repetition.