---
title: PHP Constants
description: "> Constants are like variables except that once they are defined they cannot be changed or undefined. > > > > Тогтмол нь хувьсагчтай адилхан бөгөөд хэрэв тэдгээрийг тодорхойлсны дараа тэдгээрийг өөрчлөх эсвэл тодорхойлох боломжгүй юм. > ## PHP Constants…"
url: "https://apprentice.mn/docs/php-constants/"
updated: "2021-07-08"
category: PHP
---

# PHP Constants

> Constants are like variables except that once they are defined they cannot be changed or undefined.
>
>
>
> Тогтмол нь хувьсагчтай адилхан бөгөөд хэрэв тэдгээрийг тодорхойлсны дараа тэдгээрийг өөрчлөх эсвэл тодорхойлох боломжгүй юм.

> ## PHP Constants – Тогтмол PHP
>
>
>
> A constant is an identifier (name) for a simple value. The value cannot be changed during the script. 
>
>
>
> Тогтмол гэдэг нь энгийн утгын танигч (нэр) юм. Скриптийн явцад утгыг өөрчлөх боломжгүй.
>
>
>
> A valid constant name starts with a letter or underscore (no $ sign before the constant name).
>
>
>
> Хүчин төгөлдөр тогтмол нэр нь үсэг эсвэл доогуур зурааснаас эхэлдэг (тогтмол нэрний өмнө $ тэмдэг байхгүй).
>
>
>
> **Note:** Unlike variables, constants are automatically global across the entire script.
>
>
>
> Тэмдэглэл: Хувьсагчуудаас ялгаатай нь тогтмол нь бүх скриптэд автоматаар глобалчлагддаг.

## Create a PHP Constant – PHP Constant үүсгэх

To create a constant, use the `define()` function.

Тогтмол үүсгэхийн тулд `define()` функцийг ашиглана уу.

### Syntax – Синтакс

  define(*name*, *value*, *case-insensitive*)

тодорхойлох (нэр, утга, жижиг үсгээр бичих )

Parameters:

- *name*: Specifies the name of the constant
  нэр: Байнгын нэрийг зааж өгнө
- *value*: Specifies the value of the constant
  утга: Тогтмол утгыг заана
- *case-insensitive*: Specifies whether the constant name should be case-insensitive. Default is false
  case-insensitive: Тогтмол нэр нь жижиг үсгээр тэмдэглэгдсэн  байх ёстойг тодорхойлдог. Анхдагч нь худал

### Example

### Жишээ

Create a constant with a **case-sensitive** name:

Жижиг үсгээр бичсэн нэрээр тогтмол үүсгэх:

       `<?php
define("GREETING", "Welcome to Apprentice.mn!");
echo GREETING;
?>`

### Example

### Жишээ

Create a constant with a **case-insensitive** name:

Жижиг үсгээр бичсэн нэрийг тогтмолыг үүсгээрэй:

      `<?php
define("GREETING", "Welcome to Apprentice.mn!", true);
echo greeting;
?>`

## PHP Constant Arrays – PHP тогтмол багц

In PHP7, you can create an Array constant using the `define()` function.

PHP7 дээр `define()` функцийг ашиглан тогтмол багцуудыг үүсгэж болно.

### Example

### Жишээ

Create an Array constant:

Тогтмол багцыг үүсгэнэ үү:

      `<?php
define("cars", [
  "Alfa Romeo",
  "BMW",
  "Toyota"
]);
echo cars[0];
?>`

## Constants are Global – Тогтмол нь олон улсынх

Constants are automatically global and can be used across the entire script.

Тогтмолууд нь автоматаар глобалчлагдсан бөгөөд бүх скриптэд ашиглах боломжтой.

### Example

### Жишээ

This example uses a constant inside a function, even if it is defined outside the function:

Энэ жишээнд функцийн гадна тодорхойлогдсон байсан ч гэсэн функцын доторх тогтмолыг ашигладаг.

       `<?php
define("GREETING", "Welcome to Apprentice.mn!");

function myTest() {
  echo GREETING;
}

myTest();
?>`        [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-constants%2F&linkname=PHP%20Constants](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-constants%2F&linkname=PHP%20Constants)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-constants%2F&linkname=PHP%20Constants](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-constants%2F&linkname=PHP%20Constants)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-constants%2F&linkname=PHP%20Constants](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-constants%2F&linkname=PHP%20Constants)[https://www.addtoany.com/share](https://www.addtoany.com/share)
