Function in PHP
Function Introduction
-
PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.
-
There are two parts which should be clear to us:
I. Creating a PHP Function
II. Calling a PHP Function
-
In fact we hardly need to create our own PHP function because there are already more than 1000 of built-in library functions created for different area.
function functionName()
{
code to be executed;
}
<html>
<body>
<?php
function greetings()
{
echo “Good Evening. Have a nice time";
}
greetings( );
?>
</body>
</html>
PHP Functions with Parameters
-
PHP gives us option to pass our parameters inside a function.
-
We can pass as many parameters as we like.
-
These parameters work like variables inside our function.
<html>
<head>
<title>Writing PHP Function with Parameters</title>
</head>
<body>
<?php
function addFunction($num1, $num2)
{
$sum = $num1 + $num2;
echo "Sum of the two numbers is : $sum";
}
addFunction(10, 20);
?>
</body>
</html>