WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Functions



PHP User Defined Functions

Besides the implicit PHP capacities, we can make our own functions.

A capacity is a square of proclamations that can be utilized over and again in a program.

A capacity won't execute quickly when a page loads.

A capacity will be executed by a call to the function.


Create a User Defined Function in PHP

A client characterized capacity statement begins with "function":

Syntax

work functionName() {
    code to be executed;
}

Note: A capacity name can begin with a letter or underscore (not a number).

Tip: Give the capacity a name that reflects what the work does!

In the model underneath, we make a capacity named "writeMsg()". The opening wavy prop ( { ) demonstrates the start of the capacity code and the end wavy support ( } ) demonstrates the finish of the capacity. The capacity yields "Hi world!". To call the capacity, simply compose its name:

Example

<?php
function writeMsg() {
    reverberation "Hi world!";
}

writeMsg();/call the function
?>
Run precedent »

PHP Function Arguments

Information can be gone to capacities through contentions. A contention is simply like a variable.

Arguments are indicated after the capacity name, inside the enclosures. You can include the same number of contentions as you need, simply separate them with a comma.

The following precedent has a capacity with one contention ($fname). Whenever the CustomerName() work is called, we additionally go along a name (for example Adam L. Holden), and the name is utilized inside the capacity, which yields a few distinctive first names, be that as it may, an equivalent last name:

Example

<?php
function CustomerName($fname) {
    reverberation "$fname Refsnes.<br>";
}

CustomerName("Adam L. Holden");
CustomerName("Maria Z. Scott");
CustomerName("Betty C. Amaya");
CustomerName("Lottie P. Moore");
CustomerName("Leila J. Richert");
?>
Run model »

The following model has a capacity with two contentions ($fname and $year):

Example

<?php
function CustomerName($fname, $year) {
    reverberation "$fname Refsnes. Conceived in $year <br>";
}

CustomerName("Leila J. Richert", " July 23, 1987");
CustomerName("Gregory A. Passage", "January 1, 1982");
CustomerName("Robert N. Waters", "1993");
?>
Run model »

PHP Default Argument Value

The following model tells the best way to utilize a default parameter. In the event that we call the work setHeight() without contentions it takes the default an incentive as argument:

Example

<?php
function setHeight($minheight = 120) {
    reverberation "The tallness is : $minheight <br>";
}

setHeight(285);
setHeight();/will utilize the default estimation of 120
setHeight(135);
setHeight(70);
?>

PHP Functions - Returning values

To let a capacity return an esteem, utilize the arrival statement: 

Example

<?php
function sum($x, $y) {
    $z = $x + $y;
    return $z;
}

reverberation "2 + 3 = " . sum(2, 3) . "<br>";
echo "4 + 5 = " . sum(7, 13) . "<br>";
echo "3 + 1 = " . sum(3, 1);
?>
Run precedent »