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
● Function Definition Rules
● Using the Defined Function● Unlimited Parameter Transmission
● Reference Parameter Transmission
● Type Behavior in Parameters● Rotating Variety Speculation
● $ 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 }
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 | |
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';
}
The defined functions are used with their names.
ilkFonksiyonum();
# 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!');
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);
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);
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;
10
You can specify the type of parameters that the function will accept.
function turDayatmaliFonksiyonum(string $deger)
{
echo $deger;
}
turDayatmaliFonksiyonum('Tür dayatmalı ilk fonksiyonum!');
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']);
# 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!');
If you look at the example above, the function we defined is now returnable.
You can force a function to return a value, such as in a parameter.
function sehir(string $isim) : array
{
return $isim;
}
echo sehir('istanbul');
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');
# 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();
The reason for this error is that the $ number variable is not in the newSync () function domain.
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();
The alternative is the global term.
$sayi = 10;
function yeniSayi()
{
global $sayi;
return $sayi;
}
echo yeniSayi();
You can not assign a value to a global variable that is defined globally.
global $sayi = 10;
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();
2
3
Note that in the above usage, each time the method is used, the statically defined $ number variable preserves its final value.