Functions in PHP
A function is a set of code that can optionally accept parameters and return a value. A PHP function may take zero or more parameters.
A function has three major parts:
- The name of the function
- Parentheses include Arguments, but Arguments are optional, An An argument is simply a value (or a reference to a value) that the function is expecting. A function might expect zero, one, two, three, or more arguments.
- Body of the function between curly brackets (opening and closing)
Every function is stated with function keyword. The syntax of PHP function is as follows:
function
function_name (optional $arg1, $arg2) {
block of code
}
block of code
}
The parameters of function are optional. you can use function without paramters. For example:
function topwebsite() {
print "http://www.programmersbank.com";
}
topwebsite();
print "http://www.programmersbank.com";
}
topwebsite();
Output:
http://www.programmersbank.com
Another example of PHP with argument is:
function
topwebsite($website) {
print $website;
}
print $website;
}
In this examples you have used one argument $website.
topwebsite("http://www.programmersbank.com");
See another function:
function valueExpressions($value1,$value2,$symbol) {
$symbol;
$result=$value1."".$symbol."".$value2;
}
$symbol;
$result=$value1."".$symbol."".$value2;
}
This function valueExpressions has three arguments $value1, $value2, $symbol. The user will call this function as follows:
valueExpressions(70,65,'+');
Note: A function is a block of code that is not immediately executed but can be called by your scripts when needed.
