Defining functions
In PHP, you can define functions to encapsulate reusable blocks of code. Here's the basic syntax for defining a function:
function functionName($parameter1, $parameter2, ...) {
// Code to be executed
return $result; // Optional: Return a value
}
functionName
: The name of the function.
$parameter1
, $parameter2
, ...: Optional parameters that the function accepts.
return
: Optional keyword to return a value from the function.
Here's an example of a simple function that adds two numbers:
functionName
: The name of the function.
$parameter1
, $parameter2
, ...: Optional parameters that the function accepts.
return
: Optional keyword to return a value from the function.
Here's an example of a simple function that adds two numbers:
function add($num1, $num2) {
$sum = $num1 + $num2;
return $sum;
}