Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

What is a Function? How can we call a function

In PHP, a function is a block of code that performs a specific task. It is designed to be reusable, which means that you can call the same function multiple times from different parts of your code. Functions can accept arguments and return values.

To define a function in PHP, you use the function keyword followed by the function name, a set of parentheses, and a pair of curly braces enclosing the code that should be executed when the function is called.

Here’s an example of a simple function in PHP that accepts two arguments and returns their sum:

function sum($a, $b) {
   return $a + $b;
}

To call this function, you simply use its name followed by a set of parentheses enclosing the values that you want to pass as arguments.

For example:

$result = sum(5, 10);
echo $result; // Output: 15

In this example, we call the sum() function with two arguments, 5 and 10. The function adds these two numbers and returns the result, which is then stored in the $result variable. Finally, we use the echo statement to display the result on the screen.

You can also define and call functions that do not accept any arguments or return any values.

For example:

function printMessage() {
   echo "Hello, world!";
}

printMessage(); // Output: Hello, world!

In this example, the printMessage() function simply prints the message “Hello, world!” to the screen. We call this function using its name followed by a set of parentheses, as there are no arguments to pass.