PHP Functions
Functions in PHP are blocks of code designed to perform specific tasks. They help organize and reuse code, making it easier to manage and maintain. Functions can take inputs, process them, and return outputs. Here’s a detailed overview of how functions work in PHP:
Basic Syntax
Defining a Function:
function functionName($parameter1, $parameter2) {
// Code to be executed
return $result;
}
function
: Keyword used to declare a function.functionName
: The name of the function. Should be unique within the same scope.$parameter1, $parameter2
: Optional parameters that the function can accept. Parameters are used to pass values into the function.return $result;
: Optional return statement that outputs a value from the function.
Example:
<?php
function greet($name) {
return "Hello, " . $name . "!";
}
echo greet("Alice"); // Outputs: Hello, Alice!
?>
Explanation:
- The
greet
function takes one parameter,$name
, and returns a greeting message. - The
echo
statement calls thegreet
function with the argument "Alice" and displays the returned message.
Function Parameters
Functions can have zero or more parameters. Parameters are values passed into the function to be used within its code.
Example with Multiple Parameters:
<?php
function add($a, $b) {
return $a + $b;
}
echo add(5, 10); // Outputs: 15
?>
Explanation:
- The
add
function takes two parameters,$a
and$b
, and returns their sum.
Default Values: You can provide default values for parameters. These values are used if the function is called without specifying those arguments.
Example:
<?php
function greet($name = "Guest") {
return "Hello, " . $name . "!";
}
echo greet(); // Outputs: Hello, Guest!
echo greet("Bob"); // Outputs: Hello, Bob!
?>
Explanation:
- The
greet
function has a default parameter$name
set to "Guest". If no argument is passed, "Guest" is used.
Return Values
Functions can return values using the return
keyword. The function execution stops when return
is executed, and the specified value is returned to the caller.
Example:
<?php
function square($number) {
return $number * $number;
}
$result = square(4);
echo $result; // Outputs: 16
?>
Explanation:
- The
square
function returns the square of the input number. The returned value is stored in$result
and then output.
Variable Scope
Local Scope: Variables defined inside a function are local to that function and cannot be accessed outside it.
Example:
<?php
function myFunction() {
$localVar = "I am local";
echo $localVar;
}
myFunction(); // Outputs: I am local
// echo $localVar; // Error: Undefined variable
?>
Global Scope:
To access a global variable inside a function, use the global
keyword or the $GLOBALS
array.
Example with global
Keyword:
<?php
$globalVar = "I am global";
function myFunction() {
global $globalVar;
echo $globalVar;
}
myFunction(); // Outputs: I am global
?>
Example with $GLOBALS
Array:
<?php
$globalVar = "I am global";
function myFunction() {
echo $GLOBALS['globalVar'];
}
myFunction(); // Outputs: I am global
?>
Function Overloading
PHP does not support function overloading (defining multiple functions with the same name but different parameters). Instead, PHP uses function names that are unique within the same scope.
Anonymous Functions
PHP supports anonymous functions (also known as closures), which are functions without a name. They are often used as callback functions.
Example:
<?php
$square = function($number) {
return $number * $number;
};
echo $square(5); // Outputs: 25
?>
Explanation:
- An anonymous function is assigned to the variable
$square
. It can be called using$square()
.