Variables are one of the most important elements of the programming language. Variables are assigned by the assignment operator. Variables are particularly useful for representing varying values ​​in code. Variables can sometimes store a computable numeric value, a function, or a method of a class as well as store any string. In cases where a value is often used in code, the use of this variable as a variable is highly critical in terms of its modifiability.



# Section Headings

# How to define a variable
# Variable Definition Rules

Invalid Variable Definitions
Valid Variable Definitions

# Variable Acceptable Values


# Unknown Variable Errors

Null Control

# Type Conversions



# How to define a variable

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

Rule: $ degiskenAdi = 'Variable Value' ;

As in other programming languages ​​in PHP, the variable type is not specified according to the value of the data type. All variables start with a $ sign;



# Variable Definition Rules

As with any programming language, there are some rules in PHP that must be followed in variable declaration. These are the following.

Variable names must start with an underscore (_) or an alphabetic character .
Latin characters can not contain any special characters, including letters such as ğ, ü, ş .

Illegal Variable Definitions

 Below are examples of invalid variable definitions.

$1sayi = 10;    # 1. kural ihlal edilmiştir.
$sayı = 10;     # 2. kural ihlal edilmiştir.
$yeni degisken; # 2. kural ihlal edilmiştir.
$yeniDeğişken;  # 2. kural ihlal edilmiştir.
$yeni#degisken  # 2. kural ihlal edilmiştir
Valid Variable Definitions

The following are examples of suitable variable definitions for rules.

$sayi1 = 10;
$sayi = 10;
Note: When defining variables, they do not have to contain the predefined value.



# Variable Values

A variable can hold data in any form. It depends on the purpose for which this variable is used.


Arrays are always represented by single or double quotes. This also applies when assigning a variable to a predefined value.

$email = ''; 

echo $email;

Arrays can also be passed to variables.

$sehirler = ['istanbul', 'izmir'];

echo $sehirler[1];

Function assignments can also be made to variables.

$fonksiyon = function(String $value){ return $value; };

echo $fonksiyon('ZN Framework');
ZN Framework

Or it can store any variables.

$a = 10;
$b = $a;

echo $b;



# Unknown Variable Errors

Developers use a variable that does not have a generic error definition in variable definitions. If a variable is used before it is defined, it throws an error of type Notice.

echo $a;
Notice : Undefined variable: a in  C: \ xampp \ htdocs \ test.php  on line  3

This is usually the case when a loop is used to add new values ​​to a sequence or to format a string.

$str .= 'abc';
$str .= 'xyz';

echo $str;
Notice : Undefined variable: str in  C: \ xampp \ htdocs \ test.php  on line  3
N u ll Control

If you are not sure whether a variable takes a value, and you do not want to encounter a warning type of error, you should use the null control operator (??).

echo $a ?? 'tanımsız';
$a = NULL;

echo $a ?? 'tanımsız';
$a = false;

echo $a ?? 'tanımsız';

If a NULL -defined variable is printed, it will not cause any errors.

$a = NULL;

echo $a;



# Type Conversions

The types of variables can be changed after they are defined. But not every species can be transformed into every species.

Which Species What Can Be Converted
Array - - - Object -
Object - - Array - -
String Bool Int Array Object float
Int String Bool Array Object float
Bool String Int Array Object float
float String Int Array Object float
$a = '10';

echo gettype($a)
$a = '10';
$a = (int) $a;

echo gettype($a);