---
title: PHP Constants
description: "## PHP – Class Constants ## PHP – Ангийн тогтмолууд Constants cannot be changed once it is declared. Тогтмолыг зарласны дараа өөрчлөх боломжгүй. Class constants can be useful if you need to define some constant data within a class. Хэрэв…"
url: "https://apprentice.mn/docs/php-constants-2/"
updated: "2021-07-19"
category: PHP
---

# PHP Constants

## PHP – Class Constants

## PHP – Ангийн тогтмолууд

Constants cannot be changed once it is declared.

Тогтмолыг зарласны дараа өөрчлөх боломжгүй.

Class constants can be useful if you need to define some constant data within a class.

Хэрэв та анги доторх зарим тогтмол өгөгдлийг тодорхойлох шаардлагатай бол ангийн тогтмол байдал нь ашигтай байж болно.

A class constant is declared inside a class with the `const` keyword.

Ангиллын тогтмолыг класст `const` түлхүүр үгээр зарласан болно.

Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.

Ангийн тогтмолууд нь том жижиг үсгийн мэдрэмжтэй байдаг. Гэхдээ тогтмолыг бүх том үсгээр нэрлэхийг зөвлөж байна.

We can access a constant from outside the class by using the class name followed by the scope resolution operator (`::`) followed by the constant name, like here:

Ангийн гаднаас бид тогтмолыг ангийн нэрийг ашиглан, дараа нь хамрах хүрээний нарийвчлалын операторыг (`::`), дараа нь тогтмол нэрийг дараахь байдлаар ашиглаж болно.

### Example

### Жишээ

      `<?php
class Goodbye {
  const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
}

echo Goodbye::LEAVING_MESSAGE;
?>`

Or, we can access a constant from inside the class by using the `self` keyword followed by the scope resolution operator (`::`) followed by the constant name, like here:

Эсвэл бид `self` гэсэн түлхүүр үгийг ашиглан дараа нь хамрах хүрээний нарийвчлалын операторыг (`::`), дараа нь тогтмол нэрийг дараах байдлаар ашиглан анги дотроос тогтмолыг олж авах боломжтой:

### Example

### Жишээ

      `<?php
class Goodbye {
  const LEAVING_MESSAGE = "Thank you for visiting Apprentice.mn!";
  public function byebye() {
    echo self::LEAVING_MESSAGE;
  }
}

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