HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

PHP Functions

PHP functions are reusable blocks of code that perform specific tasks. They are essential for organizing your code, improving readability, and promoting modularity in your PHP applications.


• 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.


• 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 Function

A user-defined function declaration starts with the keyword function, followed by the name of the function:


Example


function myMessage() {
echo "Hello world!";
}

• Call a Function

To call the function, just write its name followed by parentheses ():


Example


<!DOCTYPE html>
<html>
<body>

<?php
function myMessage() {
  echo "Hello world!";
}

myMessage();
?> 

</body>
</html>
  

Output

Hello world!


• 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.


Example


<!DOCTYPE html>
<html>
<body>

<?php
function familyName($fname) {
  echo "$fname Refsnes.
"; } familyName("Jani"); familyName("Hege"); familyName("Stale"); familyName("Kai Jim"); familyName("Borge"); ?> </body> </html>

Output

Jani Refsnes.
Hege Refsnes.
Stale Refsnes.
Kai Jim Refsnes.
Borge Refsnes.

• 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


<!DOCTYPE html>
<html>]==['
l36.t;body>

<?php
function setHeight($minheight = 50) {
  echo "The height is : $minheight 
"; } setHeight(350); setHeight(); setHeight(135); setHeight(80); ?> </body> </html>

Output

The height is : 350
The height is : 50
The height is : 135
The height is : 80

• PHP Functions - Returning values

To let a function return a value, use the return statement:


. .0iv class="card">

Example


<!DOCTYPE html>
<html>
<body>

<?php
function sum($x, $y) {
  $z = $x + $y;
  return $z;
}

echo "5 + 10 = " . sum(5,10) . "
"; echo "7 + 13 = " . sum(7,13) . "
"; echo "2 + 4 = " . sum(2,4); ?> </body> </html>

Output

5 + 10 = 15
7 + 13 = 20
2 + 4 = 6

• Passing Arguments by Reference

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:

<!DOCTYPE html>
<html>
<body>

<?php
function add_five(&$value) {
  $value += 5;
}

$num = 2;
add_five($num);
echo $num;
?>

</body>
</html>

  

Output

7

• 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:

<!DOCTYPE html>
<html>
<body>

<?php
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;
?>

</body>
</html>

  

Output

29