PHP Functions
PHP Functions
function functionName(){
// Code to be executed
}
• The declaration of a user-defined function start with the word function,
followed by the name of the function you want to create followed by
parentheses i.e. () and finally place your function's code between curly
brackets {}.
• While creating a user defined function we need to keep few things in
mind:
Output
PHP Functions with Parameters
• PHP gives you option to pass your parameters inside a
function. You can pass as many as parameters your like. These
parameters work like variables inside your function. Following
example takes two integer parameters and add them together
and then print them.
Output
PHP Functions returning value
• A function can return a value using the return statement in
conjunction with a value or object. return stops the execution of
the function and sends the value back to the calling code.
• You can return more than one value from a function
using return array(1,2,3,4).
• Following example takes two integer parameters and add
them together and then returns their sum to the calling
program. Note that return keyword is used to return a value
from a function.
Output
Passing Arguments by Reference
• It is possible to pass arguments to functions by reference. This
means that a reference to the variable is manipulated by the
function rather than a copy of the variable's value.
• Any changes made to an argument in these cases will change
the value of the original variable. You can pass an argument by
reference by adding an ampersand to the variable name in
either the function call or the function definition.
Output
PHP Function arguments: Passing arrays to
functions
Output
Variable Number of Arguments
By using the ... operator in front of the function parameter, the function accepts an unknown number of
arguments. This is also called a variadic function.The variadic function argument becomes an array.
Example
A function that do not know how many arguments it will get:
function sumMyNumbers(...$x) {
$n = 0;
$len = count($x);
for($i = 0; $i < $len; $i++) {
$n += $x[$i];
}
return $n;
}
$a = sumMyNumbers(5, 2, 6, 2, 7, 7);
echo $a;
Recursive Functions
• A recursive function is a function that calls itself again and again until
a condition is satisfied. Recursive functions are often used to solve
complex mathematical calculations, or to process deeply nested
structures.
Output
PHP is a Loosely Typed Language
• In the example above, notice that we did not have to
tell PHP which data type the variable is.
• PHP automatically associates a data type to the
variable, depending on its value.
<?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:
<?php declare(strict_types=1); // strict requirement
Output
The following table lists the most important
elements that can go inside $_SERVER
PHP $_REQUEST
• PHP $_REQUEST is a PHP super global variable which is used to
collect data after submitting an HTML form.
• The example below shows a form with an input field and a
submit button. When a user submits the data by clicking on
"Submit", the form data is sent to the file specified in the action
attribute of the <form> tag. In this example, we point to this file
itself for processing form data. If you wish to use another PHP
file to process form data, replace that with the filename of your
choice. Then, we can use the super global variable $_REQUEST
to collect the value of the input field:
Output
PHP $_POST
• PHP $_POST is a PHP super global variable which is used to collect form data
after submitting an HTML form with method="post". $_POST is also widely
used to pass variables.
• The example below shows a form with an input field and a submit button.
When a user submits the data by clicking on "Submit", the form data is sent
to the file specified in the action attribute of the <form> tag. In this example,
we point to the file itself for processing form data. If you wish to use another
PHP file to process form data, replace that with the filename of your choice.
Then, we can use the super global variable $_POST to collect the value of the
input field.
PHP $_GET
• _GET : $_GET is a super global variable used to collect data from the
HTML form after submitting it. When form uses method get to
transfer data, the data is visible in the query string, therefore the
values are not hidden. $_GET super global array variable stores the
values that come in the URL. GET is used to request data from a
specified resource.
Eg.
OUTPUT
$_FILES is a super global variable which can be used to upload
files. Here we will see an example in which our php script checks
if the form to upload the file is being submitted and generates a
message if true.
OUTPUT