Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Constants

Constants are named values, that never change.

Constants may be created with the const keyword or the define() function. They may be tested for existence with the defined() function.

There are global constants, which are accessible with their name and their namespaces. There are also constants in classes, interfaces, enums and traits. Those are called class constant.

Constants are namespace dependent. They can be imported using the use const command.

Constants are usually written in uppercase.

Global constants used to be case insensitive, when created with define(). This feature was removed in version 8.0. Nowadays, both const and define create case insensitive values.

<?php

const A = 1;
define('B', 2);

// displays 1
echo A;

?>

Documentation

See Also