Google News
logo
PHP Program to Create a function
In the following example of PHP program that creates a function :
Program :
<?php

// Declare a function
function addNumbers($num1, $num2) {
    $sum = $num1 + $num2;
    return $sum;
}

// Call the function
$result = addNumbers(5, 10);

// Print the result
echo "The sum of 5 and 10 is: $result";

?>
Output :
The sum of 5 and 10 is: 15
In the above example, we have declared a function `addNumbers` that takes two parameters `$num1` and `$num2`. Inside the function, we calculate the sum of the two numbers and return it using the `return` statement.

We then call the function with arguments `5` and `10` and assign the result to the variable `$result`. Finally, we print out the result using the `echo` statement.