PHP Tutorials
PHP Forms
PHP Advanced
PHP OOP
The real power of PHP comes from its functions.
PHP has over 1000 built-in functions, and in addition you can create your own custom functions.
PHP has over 1000 built-in functions that can be called directly, within a script, to perform a specific task.
Apart from the built-in PHP functions, you may have created your own tasks.
User-defined job announcement starts with the word:
function functionName()
{
code to be executed;
}
Note: The username must start with a letter or underscore. Job names are NOT sympathetic to the same cases.
Tip: Give the task a name that reflects what the task is doing!
In the example below, we create a function called "writeMsg ()". The curly opening brace ({) indicates the start of the activation code, and the curly closing brace (}) indicates the end of the operation. The function releases "Hello world!". To call an activity, just type their name in brackets ():
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the function
?>
Information can be passed on to activities through arguments. Arguments are just like flexibility.
Arguments are clarified after the name of the function, within brackets. You can add as many arguments as you like, just separate them with commas.
The following example contains a function with one argument ($ fname). When the FamilyName () function is called, we also pass the word (e.g. Jani), and the name is used within the function, which produces several different first names, but with the same surname:
<?php
function familyName($fname) {
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
The following example contains a two-argument function ($ fname and $ year):
<?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>
In the example above, note that we should not have told PHP what kind of data is variable.
PHP automatically associates data type to variable, depending on its value. Since data types are not set in a solid way, you can do things like add a character unit to the perfect number without causing an error.
In PHP 7, genre announcements have been added. This gives us the option to specify the type of data expected when announcing a job, and by adding a strong declaration, it will throw a "Dangerous Error" if the data type does not match.
In the following example we are trying to send both the number and the unit of character to the work without the use of solid:
<?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 clarify it we need to set the declare (strict_types = 1) ;. This should be in the first line of the PHP file.
In the following example we are trying to send both the number and the unit of character to work, but here we have added a strong declaration:
<?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
?>
A strong declaration compels items to be used in the intended manner.
The following example shows how to use the default parameter. When we call a function Height () without arguments it assumes the default value as an argument:
<?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);
?>
To allow the function to restore value, use the return statement:
<?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 7 also supports the Announcement Statement of the return statement. As a type of job argument declaration, by enabling a strict requirement, it will throw a "Dangerous Error" into the type match.
To announce the return function type, add a colon (:) and a type to the right before the folded opening bracket ({) when declaring a function.
In the following example we explain the return type of work:
<?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 type of return, there are different types of arguments, but make sure the return is the correct type:
<?php declare(strict_types=1); // strict requirement
function addNumbers(float
$a, float $b) : int {
return (int)($a + $b);
}
echo addNumbers(1.2, 5.2);
?>
In PHP, disputes are usually transferred in value, which means that a copy of the value is used in the work and the variables transferred to the job cannot be changed.
When a job dispute is passed for reference, changes in the argument also change the alternative to the transfer. To turn a job dispute into a reference, the & operator is used:
Use a pass-by-reference argument to update a variable:
<?php
function add_five(&$value) {
$value += 5;
}
$num
= 2;
add_five($num);
echo $num;
?>