Functions are blocks of functional expressions that can be used repeatedly in the program. A function is not executed when a page loads. The function is executed only when called.

PHP functions The first thing we want to say about PHP is that its real power is in its functions. There are more than 1000 built-in functions in PHP. We can also create our user defined function in PHP.

What is PHP User Defined Function? (User Defined Functions)

There are thousands of functions built into PHP. However, we can also create our own function if we want. The function created by ourselves, that is, by a user, is called a user defined function.

PHP Function Creation (Define Function)

PHP function We use the function keyword to define it. After this keyword, we specify the function name and open and close parenthesis. After defining the function, we open the curly braces, write the codes, and close the curly braces to specify its scope. The syntax is as follows.

function functionName() {
// Çalıştırılacak kodlar
}

PHP Function Creation Rules

  • PHP Function names can only start with letters, the underscore '_' and the dollar sign '}.
  • PHP Function names cannot start with a number. However, numbers can be used in characters after the first character.
  • PHP Function names cannot contain words reserved by PHP (if, break, for, const, etc.).
  • PHP Function names are not case sensitive.

PHP Function Creation Suggestions

  • PHP functions should be given names appropriate to their function. For example, if the function prints a message, the function name may be writeMsg(). However, it is meaningless to call a function that prints a message function1().
  • Naming convention rules used when naming PHP functions must be followed. If naming convention is not used, PHP PSR should be followed.
  • In functions defined as of PHP 7 type declaration/return type hint It is recommended to do.

PHP Functions

In the example below, we created a function called writeMsg using the function keyword. We printed "Hello World" with echo within the code block of the function. Then we called the function and got the output "Hello World".

function writeMsg()
{
    echo 'Hello World!';
}
writeMsg();
// Çıktı: Hello World

PHP Functions: Arguments

We can define arguments in functions in PHP. We define the argument in parenthesis after the function name. When calling the function, we value the argument we define. We did this in the example below.

Hint: We can define more than one argument.

function numbers($number1, $number2)
{
    echo "Sayı 1: {$number1} </br> Sayı 2: {$number2}";
}
echo numbers(21, 4);
/*
 * Çıktı:
Sayı 1: 21
Sayı 2: 4
*/

PHP Functions: Returning Value

In PHP we can assign default value to arguments. If no value is given when calling the function, the default value is used. In the example below, we set the default value to 30. The default value was used because we did not value the argument when we first called the function. When we called it the second time, the value we gave to the argument was used.

function calcModulus($number1, $number2)
{
    return $number1 % $number2;
}
echo calcModulus(21, 4);
// Çıktı: 1

PHP Functions: Default Argument Value

In PHP, we can return a value from the function using the return keyword. However, the return value is not printed or output; it just returns. In the example below, we returned a value from the function we defined via return.

function age($age = 30)
{
    echo "Senin yaşın: {$age} </br>";
}
age(); // Çıktı: 30
age(20); // Çıktı: 20

PHP is a language without strict rules (Loosely Typed Language)

PHP is a language without strict rules. In PHP, it is not mandatory to tell PHP what data type the variable is. Since data types are not strictly defined, we can add a string data type to a variable that contains an integer data type. The same is true for functions. As of PHP 7, we can define argument types and return value type in functions. Clean code This is recommended for the sake of writing (clean code), but is not mandatory in PHP 7.

function addNumbers(int $a, int $b)
{
    return $a + $b;
}
echo addNumbers(5, "5 gün");
// Çıktı: 10

PHP Return Type Declarations

In PHP we can define the return value type in strict mode. In the example below, we determined the value that the function will return.

function addNumbers(float $a, int $b): float
{
    return $a + $b;
}
echo addNumbers(1.2, 6);
// Çıktı: 7.2