PHP Functions

PHP’s functions are what give it its real power. PHP comes with more than a thousand built-in functions, and you can also make your own.

Functions that come with PHP

PHP has more than a thousand built-in functions that can be called directly from a script to do a certain job.

Check out our PHP reference for a full list of all the functions that come with PHP.

PHP Functions Defined by Users

Besides the functions that come with PHP, you can also make your own.

  • A block of statements that can be used over and over again in a programme is called a function.
  • When a page loads, a function won’t run on its own.
  • By making a call to a function, the function will be run.

How to Make a PHP User-Defined Function

A declaration of a user-defined function starts with the word function:

Syntax

function functionName() {
  code to be execute;
}

Note: The first character in a function name must be a letter or an underscore. Case is NOT important for function names.

Tip: Name the function something that describes what it does!

In the following example, we make a function called “HelloFun().” The opening curly brace () shows where the function code starts, and the closing curly brace () shows where the function code stops. “Hello, Coderazaa!” comes out of the function. To call the function, just write the name of the function followed by ():

 

Example

<?php
function HelloFun() {
echo “Hello Coderazaa!”;
}

HelloFun(); // call the function
?>

Arguments for PHP Functions

Arguments are a way to send information to a function. An argument is just like a variable.

Inside the parentheses, which come after the name of the function, are the arguments. You can add as many reasons as you want; just put a comma between each one.

In the next example, there is a function that only takes one argument ($name). When we call the myfamily() function, we also give it a name, like Jani. The name is used inside the function, which then returns a list of first names that are all different but have the same last name:

Example

<!DOCTYPE html>
<html>
<body>

<?php
function myfamily($name) {
echo “$name Raja.<br>”;
}

familyName(“Ram”);
familyName(“Shyam”);
familyName(“Shiv”);
familyName(“Kaala”);
familyName(“bhola”);
?>

</body>
</html>

Output

Ram Raja.
Shyam Raja.
Shiv Raja.
Kaala Raja.
bhola Raja.

The following example has a function with two arguments ($name and $age):

Example

<!DOCTYPE html>
<html>
<body>

<?php
function familyName($fname, $year) {
echo “$fname Raja . Born in $year <br>”;
}

familyName(“Ram”,”2000″);
familyName(“Shyam”,”1990″);
familyName(“Shiv”,”1980″);
?>

</body>
</html>

Output

Ram Raja . Born in 2000
Shyam Raja . Born in 1990
Shiv Raja . Born in 1980

PHP is a Language with Loose Types

In the example above, notice that we did not have to tell PHP what type of data the variable is.

PHP gives the variable an automatic data type based on what it is used for. Since the data types are not set in a strict way, you can do things like add a string to an integer without making an error.

Type declarations were added in PHP 7. This lets us choose the expected data type when declaring a function. If the data type doesn’t match, adding the strict declaration will make it throw a “Fatal Error.”

In the next example, we try to send the function both a number and a string without using strict:

Example

<?php
function addNumbr(int $x, int $y) {
return $x + $y;
}
echo addNumbr(7, “7 days”);
// since strict is NOT enabled “7 days” is changed to int(7), and it will return 14
?>

Output

14

Set declare(strict types=1); is what we need to do to specify strict. This must be on the first line of the PHP file.

In the next example, we try to send the function both a number and a string, but we add the strict declaration:

Example

<?php declare(strict_types=1); // strict requirement

function addNumbers(int $x, int $y) {
return $x + $y;
}
echo addNumbers(7, “7 days”);
// since strict is enabled and “5 days” is not an integer, an error will be thrown
?>

Output

PHP Fatal error: Uncaught TypeError: Argument 2 passed to addNumbers() must be of the type integer, string given, called in /home/LhkD6U/prog.php on line 6 and defined in /home/LhkD6U/prog.php:3 Stack trace: #0 /home/LhkD6U/prog.php(6): addNumbers(7, ‘7 days’) #1 {main} thrown in /home/LhkD6U/prog.php on line 3

PHP Default Argument Value

In the example below, you can see how to use a default parameter. If we call setSize() without any arguments, it uses the default value:

Example

<?php declare(strict_types=1); // strict requirement ?>
<!DOCTYPE html>
<html>
<body>

<?php
function setSize(int $minsize = 50) {
echo “The Size is : $minsize <br>”;
}

setSize(250);
setSize();
setSize(185);
setSize(180);
?>

</body>
</html>

Output

The Size is : 250
The Size is : 50
The Size is : 185
The Size is : 180

Returning values in PHP functions

Use the return statement to let a function return a value:

Example

<?php declare(strict_types=1); // strict requirement ?>
<!DOCTYPE html>
<html>
<body>

<?php
function add(int $i, int $j) {
$k = $i + $j;
return $k;
}

echo “6 + 9 = ” . add(6,9) . “<br>”;
echo “17 + 3 = ” . add(17,3) . “<br>”;
echo “12 + 14 = ” . add(12,14);
?>

</body>
</html>

Output

The Size is : 250
The Size is : 50
The Size is : 185
The Size is : 180

 Declaring the return type in PHP

The return statement in PHP 7 can also be used with Type Declarations. Like with the type declaration for function arguments, if you turn on the strict requirement, it will throw a “Fatal Error” if the types don’t match.

To declare a type for the function return, put a colon (:) and the type right before the function’s opening curly bracket ().

In the next example, we tell the function what kind of return we want:

Example

<?php declare(strict_types=1); // strict requirement
function addNum(float $x, float $y) : float {
return $x + $y;
}
echo addNum(2.2, 5.5);
?>

Output

7.7

You can set a different return type, then the argument types, but make sure to use the return is the correct type:

Example

<?php declare(strict_types=1); // strict requirement
function addNum(float $x, float $y) : int{
return (int)($x + $y);
}
echo addNum(2.2, 5.5);
?>

Output

7

 

Passing Arguments by Reference

Most PHP arguments are passed by value, which means that the function uses a copy of the value and cannot change the variable that was passed into the function.

When a function argument is passed by reference, any changes to the argument also change the variable that was passed in. The & operator is used to change an argument to a reference:

Example

<!DOCTYPE html>
<html>
<body>

<?php
function add_Num(&$val) {
$val += 10;
}

$num = 1;
add_Num($num);
echo $num;
?>

</body>
</html>

Output

11

People also search

Leave a Comment

Scroll to Top