PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.
You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you option to create your own functions as well.
There are two parts which should be clear to you
Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it. Following example creates a function called writeMessage() and then calls it just after creating it.
Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as shown in the following example below −
<html> <head> <title>Writing PHP Function</title> </head> <body> <?php /* Defining a PHP Function */ function writeMessage() { echo "You are really a nice person, Have a nice time!"; } /* Calling a PHP Function */ writeMessage(); ?> </body> </html>
This will display following result
You are really a nice person, Have a nice time!
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.
<html> <head> <title>Writing PHP Function with Parameters</title> </head> <body> <?php function addFunction($num1, $num2) { $sum = $num1 + $num2; echo "Sum of the two numbers is : $sum"; } addFunction(10, 20); ?> </body> </html>
This will display following result
Sum of the two numbers is : 30
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.
Following example depicts both the cases.
<html> <head> <title>Passing Argument by Reference</title> </head> <body> <?php function addFive($num) { $num += 5; } function addSix(&$num) { $num += 6; } $orignum = 10; addFive( $orignum ); echo "Original Value is $orignum<br />"; addSix( $orignum ); echo "Original Value is $orignum<br />"; ?> </body> </html>
This will display following result
Original Value is 10 Original Value is 16
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.
<html> <head> <title>Writing PHP Function which returns value</title> </head> <body> <?php function addFunction($num1, $num2) { $sum = $num1 + $num2; return $sum; } $return_value = addFunction(10, 20); echo "Returned value from the function : $return_value"; ?> </body> </html>
This will display following result
Returned value from the function : 30
You can set a parameter to have a default value if the function's caller doesn't pass it.
Following function prints NULL in case use does not pass any value to this function.
<html> <head> <title>Writing PHP Function which returns value</title> </head> <body> <?php function printMe($param = NULL) { print $param; } printMe("This is test"); printMe(); ?> </body> </html>
This will produce following result
This is test
(PHP 4, PHP 5, PHP 7, PHP 8)
number_format — Format a number with grouped thousands
$num
,$decimals
= 0,$decimal_separator
= ".",$thousands_separator
= ","Formats a number with grouped thousands and optionally decimal digits.
num
The number being formatted.
decimals
Sets the number of decimal digits. If 0
, the decimal_separator
is omitted from the return value.
decimal_separator
Sets the separator for the decimal point.
thousands_separator
Sets the thousands separator.
A formatted version of num
.
Version | Description |
---|---|
8.0.0 | Prior to this version, number_format() accepted one, two, or four parameters (but not three). |
7.2.0 | number_format() was changed to not being able to return -0 , previously -0 could be returned for cases like where num would be -0.01 . |
Example #1 number_format() Example
For instance, French notation usually use two decimals, comma (',') as decimal separator, and space (' ') as thousand separator. The following example demonstrates various ways to format a number:
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',',&