Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
Practical Paper
Industrial Training
PHP Functions
The real power of PHP comes from its functions.
PHP has more than 1000 built-in functions, and in addition you can create your own custom functions.
PHP Built-in Functions
PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task.
Please check out our PHP reference for a complete overview of the PHP built-in functions.
PHP User Defined Functions
Besides the built-in PHP functions, it is possible to create your own functions.
- A function is a block of statements that can be used repeatedly in a program.
- A function will not execute automatically when a page loads.
- A function will be executed by a call to the function.
Create a User Defined Function in PHP
A user-defined function declaration starts with the word function:
Syntax
function functionName() {
code to be executed;
}
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 ():
Example
< ?php
class="phpkeywordcolor" style="color:mediumblue">function writeMsg() {
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"Hello world!";
class="phpnumbercolor" style="color:red"> }
writeMsg(); class="commentcolor" style="color:green">// call the function
class="phptagcolor" style="color:red">?>
PHP Function Arguments
Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:
Example
< ?php
class="phpkeywordcolor" style="color:mediumblue">function familyName($fname) {
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"$fname Refsnes.< br>";
class="phpnumbercolor" style="color:red"> }
familyName(class="phpstringcolor" style="color:brown">"Jani");
familyName(class="phpstringcolor" style="color:brown">"Hege");
class="phpnumbercolor" style="color:red"> familyName(class="phpstringcolor" style="color:brown">"Stale");
familyName(class="phpstringcolor" style="color:brown">"Kai Jim");
familyName(class="phpstringcolor" style="color:brown">"Borge");
class="phptagcolor" style="color:red">?>
The following example has a function with two arguments ($fname and $year):
Example
< ?php
class="phpkeywordcolor" style="color:mediumblue">function familyName($fname, $year) {
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"$fname Refsnes. Born in $year < br>";
}
familyName(class="phpstringcolor" style="color:brown">"Hege", class="phpstringcolor" style="color:brown">"1975");
class="phpnumbercolor" style="color:red"> familyName(class="phpstringcolor" style="color:brown">"Stale", class="phpstringcolor" style="color:brown">"1978");
familyName(class="phpstringcolor" style="color:brown">"Kai Jim", class="phpstringcolor" style="color:brown">"1983");
class="phptagcolor" style="color:red">?>
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. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a "Fatal Error" if the data type mismatches.
In the following example we try to send both a number and a string to the function without using strict:
Example
< ?php
class="phpkeywordcolor" style="color:mediumblue">function addNumbers(int $a, int $b) {
class="phpkeywordcolor" style="color:mediumblue">return $a class="phpnumbercolor" style="color:red"> + $b;
}
class="phpnumbercolor" style="color:red"> class="phpkeywordcolor" style="color:mediumblue">echo addNumbers(class="phpnumbercolor" style="color:red">5,
class="phpstringcolor" style="color:brown">"5 days");
class="commentcolor" style="color:green">// since strict is NOT enabled "5 days" is changed to int(5), and it will return 10
class="phptagcolor" style="color:red">?>
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:
Example
< ?php class="phpkeywordcolor" style="color:mediumblue">declare(strict_types=class="phpnumbercolor" style="color:red">1); class="commentcolor" style="color:green">// strict requirement
class="phpkeywordcolor" style="color:mediumblue">function class="phpnumbercolor" style="color:red"> addNumbers(int $a, int $b) {
class="phpkeywordcolor" style="color:mediumblue">return $a + $b;
}
class="phpkeywordcolor" style="color:mediumblue">echo addNumbers(class="phpnumbercolor" style="color:red">5, class="phpstringcolor" style="color:brown">"5 days");
class="commentcolor" style="color:green">// since strict is enabled and "5 days" is not an integer, an error will be thrown
class="phptagcolor" style="color:red">?>
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:
Example
< ?php class="phpkeywordcolor" style="color:mediumblue">declare(strict_types=class="phpnumbercolor" style="color:red">1); class="commentcolor" style="color:green">// strict requirement
class="phpkeywordcolor" style="color:mediumblue">function setHeight(int $minheight = class="phpnumbercolor" style="color:red">50) {
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"The height is : $minheight < br>";
}
setHeight(class="phpnumbercolor" style="color:red">350);
setHeight(); class="commentcolor" style="color:green">// will use the default value of 50
setHeight(class="phpnumbercolor" style="color:red">135);
setHeight(class="phpnumbercolor" style="color:red">80);
class="phptagcolor" style="color:red">?>
PHP Functions - Returning values
To let a function return a value, use the return statement:
Example
< ?php class="phpkeywordcolor" style="color:mediumblue">declare(strict_types=class="phpnumbercolor" style="color:red">1); class="commentcolor" style="color:green">// strict requirement
class="phpkeywordcolor" style="color:mediumblue">function sum(int $x, class="phpnumbercolor" style="color:red"> int $y) {
$z = $x + $y;
class="phpkeywordcolor" style="color:mediumblue">return $z;
}
class="phpnumbercolor" style="color:red"> class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"5 + 10 = "
. sum(class="phpnumbercolor" style="color:red">5, class="phpnumbercolor" style="color:red">10) . class="phpstringcolor" style="color:brown">"< br>";
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"7 + 13 = " . sum(class="phpnumbercolor" style="color:red">7,
class="phpnumbercolor" style="color:red">13) . class="phpstringcolor" style="color:brown">"< br>";
class="phpkeywordcolor" style="color:mediumblue">echo class="phpstringcolor" style="color:brown">"2 + 4 = " . sum(class="phpnumbercolor" style="color:red">2,
class="phpnumbercolor" style="color:red">4);
class="phptagcolor" style="color:red">?>
PHP Return Type Declarations
PHP 7 also supports Type Declarations for the return statement. Like with the type declaration for function arguments, by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch.
To declare a type for the function return, add a colon ( : ) and the type right before the opening curly ( { )bracket when declaring the function.
In the following example we specify the return type for the function:
Example
< ?php class="phpkeywordcolor" style="color:mediumblue">declare(strict_types=class="phpnumbercolor" style="color:red">1); class="commentcolor" style="color:green">// strict requirement
class="phpkeywordcolor" style="color:mediumblue">function addNumbers(float class="phpnumbercolor" style="color:red"> $a, float $b) : float {
class="phpkeywordcolor" style="color:mediumblue">return $a + $b;
}
class="phpkeywordcolor" style="color:mediumblue">echo addNumbers(class="phpnumbercolor"
style="color:red">1.2, class="phpnumbercolor" style="color:red">5.2);class="phpnumbercolor" style="color:red">
class="phptagcolor" style="color:red">?>
You can specify a different return type, than the argument types, but make sure the return is the correct type:
Example
< ?php class="phpkeywordcolor" style="color:mediumblue">declare(strict_types=class="phpnumbercolor" style="color:red">1); class="commentcolor" style="color:green">// strict requirement
class="phpkeywordcolor" style="color:mediumblue">function addNumbers(float class="phpnumbercolor" style="color:red"> $a, float $b) : int {
class="phpkeywordcolor" style="color:mediumblue">return (int)($a + $b);
}
class="phpnumbercolor" style="color:red"> class="phpkeywordcolor"
style="color:mediumblue">echo addNumbers(class="phpnumbercolor" style="color:red">1.2, class="phpnumbercolor" style="color:red">5.2);
class="phptagcolor" style="color:red">?>
Passing Arguments by Reference
In PHP, arguments are usually passed by value, which means that a copy of the value is used in the function and the variable that was passed into the function cannot be changed.
When a function argument is passed by reference, changes to the argument also change the variable that was passed in. To turn a function argument into a reference, the & operator is used:
Example
Use a pass-by-reference argument to update a variable:
< ?php
class="phpkeywordcolor" style="color:mediumblue">function add_five(&$value) {
$value += class="phpnumbercolor" style="color:red">5;
}
$num class="phpnumbercolor" style="color:red"> = class="phpnumbercolor" style="color:red">2;
add_five($num);
class="phpnumbercolor" style="color:red"> class="phpkeywordcolor" style="color:mediumblue">echo $num;
class="phptagcolor" style="color:red">?>
