In this discussion, we will discuss about PHP Function. We really need PHP Function in certain circumstances when coding. The genuine intensity of PHP originates from its capacities. PHP has in excess of 1000 inherent capacities, and moreover you can make your own custom capacities.
Other than the inherent PHP capacities, it is conceivable to make your own capacities.
- A capacity is a square of explanations that can be utilized over and again in a program.
- A capacity won’t execute consequently when a page loads.
- A capacity will be executed by a call to the capacity.
Create a User Defined Function in PHP
A user-defined function declaration starts with the word function:
Note: A function name must start with a letter or an underscore. Function names are NOT case-sensitive.
Tip: Give the function a name that reflects what the function does!
In the example below, we create a function named “writeMsg()”. The opening curly brace ( { ) indicates the beginning of the function code, and the closing curly brace ( } ) indicates the end of the function. The function outputs “Hello world!”. To call the function, just write its name followed by brackets ():
1 2 3 4 5 6 |
<?php function writeMsg() { echo "Hello world!"; } writeMsg(); // call the function ?> |
PHP Function Arguments
Data can be gone to capacities through contentions. A contention is much the same as a variable.
Contentions are determined 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 accompanying model has a capacity with one contention ($fname). At the point when the familyName() work is called, we likewise go along a name (for example Jani), and the name is utilized inside the capacity, which yields a few distinctive first names, yet an equivalent last name. See the example below:
1 2 3 4 5 6 7 8 9 10 |
<?php function familyName($fname) { echo "$fname Refsnes.<br>"; } familyName("Jani"); familyName("Hege"); familyName("Stale"); familyName("Kai Jim"); familyName("Borge"); ?> |
The following example has a function with two arguments ($fname and $year):
1 2 3 4 5 6 7 8 |
<?php function familyName($fname, $year) { echo "$fname Refsnes. Born in $year <br>"; } familyName("Hege", "1975"); familyName("Stale", "1978"); familyName("Kai Jim", "1983"); ?> |
PHP is a Loosely Typed Language
In the model above, notice that we didn’t need to disclose to PHP which information type the variable is.
PHP naturally relates an information type to the variable, contingent upon its worth. Since the information types are not set in an exacting sense, you can do things like adding a string to a whole number without causing a mistake.
In PHP 7, type assertions were included. This gives us an alternative to indicate the normal information type while pronouncing a capacity, and by including the severe announcement, it will toss a “Lethal Error” if the information type confuse.
In the accompanying model we attempt to send both a number and a string to the capacity without utilizing severe. See the example below:
1 2 3 4 5 6 7 |
<?php function addNumbers(int $a, int $b) { return $a + $b; } echo addNumbers(5, "5 days"); // since strict is NOT enabled "5 days" is changed to int(5), and it will return 10 ?> |
To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file.
In the following example we try to send both a number and a string to the function, but here we have added the strict declaration:
1 2 3 4 5 6 7 |
<?php declare(strict_types=1); // strict requirement function addNumbers(int $a, int $b) { return $a + $b; } echo addNumbers(5, "5 days"); // since strict is enabled and "5 days" is not an integer, an error will be thrown ?> |
PHP Default Argument Value
The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:
1 2 3 4 5 6 7 8 9 |
<?php declare(strict_types=1); // strict requirement function setHeight(int $minheight = 50) { echo "The height is : $minheight <br>"; } setHeight(350); setHeight(); // will use the default value of 50 setHeight(135); setHeight(80); ?> |
PHP Functions – Returning values
To let a function return a value, use the return statement:
1 2 3 4 5 6 7 8 9 |
<?php declare(strict_types=1); // strict requirement function sum(int $x, int $y) { $z = $x + $y; return $z; } echo "5 + 10 = " . sum(5, 10) . "<br>"; echo "7 + 13 = " . sum(7, 13) . "<br>"; echo "2 + 4 = " . sum(2, 4); ?> |
PHP Return Type Declaration
PHP 7 additionally bolsters Type Declarations for the arrival proclamation. Like with the sort assertion for work contentions, by empowering the exacting prerequisite, it will toss a “Lethal Error” on a kind confound.
To proclaim a sort for the capacity return, include a colon ( : ) and the sort directly before the opening wavy ( { )section while announcing the capacity.
In the accompanying model we indicate the arrival type for the capacity:
1 2 3 4 5 6 |
<?php declare(strict_types=1); // strict requirement function addNumbers(float $a, float $b) : float { return $a + $b; } echo addNumbers(1.2, 5.2); ?> |
You can specify a different return type, than the argument types, but make sure the return is the correct type:
1 2 3 4 5 6 |
<?php declare(strict_types=1); // strict requirement function addNumbers(float $a, float $b) : int { return (int)($a + $b); } echo addNumbers(1.2, 5.2); ?> |