Sure, PHP functions are a cornerstone of the language, allowing you to group code into reusable blocks. Here's a breakdown of PHP functions:
In PHP, a function is a block of statements that can be used repeatedly in a program. A function will not execute automatically when a page loads. A function will be executed by a call to the function. Syntax:
function functionName() {
// code to be executed;
}
Example:
function sayHello() {
echo "Hello!";
}
sayHello(); // Outputs: Hello!
You can provide parameters when you call the function. Syntax:
function functionName(parameter1, parameter2, ...) {
// code to be executed;
}
Example:
function addNumbers($a, $b) {
return $a + $b;
}
echo addNumbers(5, 3); // Outputs: 8
You can specify a default value for a function parameter. If the function is called without the argument, it uses the default value. Example:
function greet($name = "Guest") {
echo "Hello, $name!";
}
greet(); // Outputs: Hello, Guest!
greet("Alice"); // Outputs: Hello, Alice!
A function can return a value using the `return` statement. Example:
function square($num) {
return $num * $num;
}
echo square(4); // Outputs: 16
In PHP, functions can be stored in variables and can be called using these variables. Example:
function greet() {
echo "Hello!";
}
$funcVar = "greet";
$funcVar(); // Outputs: Hello!
A function that calls itself is known as a recursive function. Example:
function factorial($n) {
if ($n == 0) {
return 1;
} else {
return ($n * factorial($n-1));
}
}
echo factorial(5); // Outputs: 120
Anonymous functions, also known as closures, allow the creation of functions without a specified name. They are most useful as the value of callback parameters. Example:
$add = function($a, $b) {
return $a + $b;
};
echo $add(3, 4); // Outputs: 7
Example:
// Pass by Value
function addFive($num) {
$num += 5;
}
$val = 10;
addFive($val);
echo $val; // Outputs: 10
// Pass by Reference
function addTen(&$num) {
$num += 10;
}
$val = 10;
addTen($val);
echo $val; // Outputs: 20
The scope of a variable is the part of the script where the variable can be referenced or modified. PHP has both local and global scope. Example:
$globalVar = "I am a global variable";
function checkScope() {
global $globalVar; // Using the global keyword
echo $globalVar;
}
checkScope(); // Outputs: I am a global variable
This is a brief overview of PHP functions and their different aspects. The official [PHP documentation](https://www.php.net/manual/en/language.functions.php) offers a deeper dive if you're interested.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here