Constants
Constants are one of the elements of the programming language. Constants are defined by the define () function. Constellations are often designed to hide varying power values. Constants can store a numeric value or an array that can be computed. In addition, a defined constant can be accessed from within all functions and objects. It has a global domain.
# How is Fixed Defined?
The syntax of constants in the PHP programming language is as follows.
Rule: define ( 'CONSTANT_NAME' , 'CONSTANT_VALUE' ) ;
In programming languages, constants are written in uppercase as standard, and underscores (_) are used to separate multiple words.
# Constant Description Rules
As with any programming language, there are some rules in PHP that must be followed in a constant definition. These are the following.
● Constants must start with an underscore (_) or an alphabetic character .
● Latin characters can not contain any special characters, including letters such as ğ, ü, ş .
● The words reserved by the PHP language can not be given as fixed names.
# Possible Values of Constants
It can be a fixed constant and store array type data.
arrays are always represented by single or double quotes.
define('SITE_URL', 'https://znframework.com');
echo SITE_URL
Sabitlere sayısal değerler de aktarılabilir. Aritmetik için kullanılacak sayısal ifadelerin tırnaksız yazılması doğru bir kullanım olur. Diğer programlama dillerindeki gibi farklı sayı türleri için veri türü belirtilmesine gerek yoktur.
define('KDV', 18);
echo KDV;
18
Sabitlerde diziler de aktarılabilir.
define('CITIES', ['istanbul', 'izmir']);
echo CITIES[1];
# Tanımsız Sabit Hataları
Bir sabit tanımlanmadan kullanılırsa uyarı (Notice) türünden bir hata oluşur.
echo UNDEFINED_CONSTANTS;
UNDEFINED_CONSTANTS
Bir sabitin tanımlı olup olmadığından emin değilseniz ve uyarı türünden hata ile de karşılaşmak istemiyorsanız defined() fonksiyonunu kullanın.
if( defined('UNDEFINED_CONTANTS') )
{
echo UNDEFINED_CONTANTS;
}
else
{
echo 'Undefined: UNDEFINED_CONSTANTS';
}