Functions



The main task of the functions is to compile code that does a certain job and to reuse the same code in similar situations without having to rewrite it. That is, once a code has the logic of using it over and over again. For example, you use database connections in all your projects. Repeating the same codes in each project is not a very smart way. Such a path will cause serious time losses. Instead, you can make your database connection codes function once and use them in your projects. Or, if you need to use the same code more than once in the same project, you can use these functions instead of rewriting them instead of rewriting them.

 

 

# Section Headings


# How to define the function?

Function Definition Rules
Using the Defined Function

# Parameter Send

Unlimited Parameter Transmission
Reference Parameter Transmission
Type Behavior in Parameters 

# Rotating Value

Rotating Variety Speculation

# Variable Domain

$ GLOBALS Superclass
Global Statement
Static Statement

 

 

# How to define the function?


The syntax of function definitions in the PHP programming language is as follows.

Rule: function functionAdd ( $ parameter1 , $ parameter2 , ... $ parameterN ) {  // your codes }

Function Definition Rules

In function definitions, the variable definition rules are the same. Additionally;

PHP's reserved key statements can not be given as functions. Example: foreach

 
Reserved Key Words
__halt_compiler () abstract and array () ace
break callable (as of PHP 5.4) casa catcher class
clone const continua declare default
die () do echo else elseif
empty () enddec to EndFor endforeach endif
endswitch endwhile eval () exit () extends
final finally (as of PHP 5.5) for foreach function
global goto ( since PHP 5.3) iF implements include
include_once instanceof insteadof ( since PHP 5.4) interface isset ()
list () namespace ( since PHP 5.3) new or print
protected the public require require_once private
return static switch throw trait (as of PHP 5.4)
try unset () user there is while
x yield (as of PHP 5.5) int (as of PHP 7 ) float (as of PHP 7 ) bool (as of PHP 7 )
string (as of PHP 7 ) true (as of PHP 7 ) false (as of PHP 7 ) null (as of PHP 7 ) resource (as of PHP 7 )
object (as of PHP 7 ) mixed (as of PHP 7 ) numeric (as of PHP 7 ) PHP_ * Constants __sihirliyontem of ()
__CLASS__ __NAMESPACE__ __FUNCTION__ T_ * Constants E_ * Constants
function ilkFonksiyonum()
{
    echo 'İlk Fonksiyonum';
} 
Using the Defined Function

The defined functions are used with their names.

ilkFonksiyonum();
First Function

 

 

# Parameter Send


To send data from outside to functions, how many data should be sent in parentheses can be defined by specifying that number of parameters. Since parameters are variables, they are subject to variable definition rules.

function ilkFonksiyonum($deger)
{
    echo $deger;
} 

ilkFonksiyonum('Parametre gönderimli ilk fonksiyonum!');
My first function with parameter assignment!
Unlimited Parameter Send

Unlimited parameter passing can be used in situations where the number of parameters of a function is ambiguous. For an unlimited parameter, the parameter is sent by putting ... (three dots) before the variable.

function sonsuzParametreGonderimi(...$parametreler)
{
    var_dump($parametreler);
}

sonsuzParametreGonderimi(1, 2, 3, 'hey', true);
[0] => int ( 1 ) [1] => int ( 2 ) [2] => int ( 3 ) [3] => string ( 3 ) "hey" [4] => Boolean ( true )

A normal parameter definition can not be made after unlimited parameter definition. That is, if some parameters assume a specific task, they must be specified before unlimited parameter definition.

function sonsuzParametreGonderimi($parametre, ...$parametreler)
{
    $parametreler[] = $parametre;

    var_dump($parametreler);
}

sonsuzParametreGonderimi('benim parametrem', 1, 2, 3, 'hey', true);
[0] => int ( 1 ) [1] => int ( 2 ) [2] => int ( 3 ) [3] => string ( 3 ) "hey" [4] => Boolean ( true ) [5 ] => string (my parameter)
Reference Parameter Send

In reference parameter transmission, & symbol is added immediately before parameter variable . In reference parameter sending, the purpose is to process the passed parameter in the function and to pass the final value out.

function referansParametreKullanimi($parametre, &$referansParametre)
{
    $referansParametre = 10;
   
    echo $parametre;
}

referansParametreKullanimi('merhaba', $referans);

echo $referans;
hello
10
Type Specification in Parameters 

You can specify the type of parameters that the function will accept.

function turDayatmaliFonksiyonum(String $deger)
{
    echo $deger;
} 

turDayatmaliFonksiyonum('Tür dayatmalı ilk fonksiyonum!');
My first function with type imposition!

In the above definition we have to specify that the parameter must be a string type parameter. If an invalid data type other than this type is sent, the system will produce a fatal error.

turDayatmaliFonksiyonum(['Dizi gönderemem']);
Fatal error: Uncaught TypeError: Argument 1 passed to turDayatmaliFunction () must be of type string, array given, called in /var/www/html/test/test.php

 

 

# Rotating Value


In the above examples, we produced output directly with echo in the function. However, it is not a very healthy method to output values ​​by echo in functions. Instead, it must be used in such a way that the output generated within the function will return a value. The return key word is used to return the value . 

function degerDondurenFonksiyonum($deger)
{
    return $deger;
} 

echo degerDondurenFonksiyonum('Değer döndüren ilk fonksiyonum!');
The first function that returns value!

If you look at the example above, the function we defined is now returnable. 

Rotating Variety Type Dynamics

You can force a function to return a value, such as in a parameter. 

function sehir(String $isim) : Array
{
    return $isim;
} 

echo sehir('istanbul');
Fatal error: Uncaught TypeError: Argument 1 passed to sehir () must be of type string, array given, called in /var/www/html/test/test.php on line 12

We forced the return value of the function defined above to be an array , but the function string produced a fatal error because it returns a value of type.

function sehir(String $isim) : String
{
    return $isim;
} 

echo sehir('istanbul');
Istanbul

 

 

# Variable Domain 


A variable domain is usually a concept that an externally defined variable can be accessed from within a function or class. That is, code blocks created with {} brackets. Normal variables are not suitable for such use. For example, the following example will give a warning error.

$sayi = 10;

function yeniSayi()
{
    return $sayi;
}

echo yeniSayi();
Notice : Undefined variable: number in  C: \ xampp \ htdocs \ test.php  on line  7

The reason for this error is that the $ number variable is not in the newSync () function domain.

$ GLOBALS Superblock

We are bringing the solution to this problem with $ GLOBALS superclass or global key phrase. 

$sayi = 10;

function yeniSayi()
{
    return $GLOBALS['sayi']; # Değişkenin adı dizi anahtarı olarak kullanılır.
}

echo yeniSayi();
10
Global Statement

The alternative is the global term.

$sayi = 10;

function yeniSayi()
{
    global $sayi;
    return $sayi;
}

echo yeniSayi();
10

You can not assign a value to a global variable that is defined globally.

global $sayi = 10;
Parse error : syntax error, unexpected '=' , expecting ',' or '; in  C: \ xampp \ htdocs \ test.php  on line  7 
Static Statement 

One of the important properties of the variable domain is static variables. A static variable is only valid in the domain of the function, and the program does not lose its value when the operation goes out of that domain.

function yeniSayi()
{
    static $sayi = 1;

    return $sayi++ . ">";
}

echo yeniSayi();
echo yeniSayi();
echo yeniSayi();
1
2
3

Note that in the above usage, each time the method is used, the statically defined $ number variable preserves its final value.