Yogesh Chauhan's Blog

How to define constants in PHP?

in PHP on June 15, 2021

Constants are more likely to be used as identifiers since their values CAN NOT change. The magic constants are a bit different in that sense.

Constant identifiers are case-sensitive but they are always defined in uppercase.

Before PHP 8, that wasn’t the case. If you had used define() function to define constants, they might not be case-sensitive!

The scope of a constant is global.

Rules to define a constant

Constant names are like any other variable names.

A valid constant name would start with a letter or underscore and you can add as many numbers, letters, underscores you want.

This is how you can define constants:


define("SITE", "yogeshchauhan.com");
define("BLOG", "yogeshchauhan.com");

You CAN NOT start a constant name with a number:


//NOT VALID
define("1SITE", "yogeshchauhan.com");
define("2BLOG", "yogeshchauhan.com");

This is valid but not recommended:


//NOT VALID
define("__SITE__", "yogeshchauhan.com");
define("__BLOG__", "yogeshchauhan.com");

PHP has magic constants and if in future they use the same name to add magic constant then that might break your code.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Dec 4 What is Etrieve Flow? #Dec 2 The unique operator($) in Envision Basic #Nov 25 Steps to Install Microsoft SQL Server on a MacOS #Nov 11 What is DevOps? #Nov 10 The * arithmetic operator in Envision Basic #Nov 10 What are Big Data Clusters in SQL Server?
You might also like these
Column and Table Alias in PostgresPostgresGeneral concepts in DrupalDrupalLEFT JOIN in PostgresPostgresWhat happens when we add Numbers and Strings in JavaScript?JavaScriptHow to remove a specific item from an array in JavaScript?JavaScriptWindow setInterval() Method in JavaScriptJavaScriptJavaScript arrays: a separate data type or Objects?JavaScriptHow to get query string values in JavaScript? (URL Manipulation using URLSearchParams)JavaScriptCanvas Drawing in HTML5HTMLWhat’s a Log File and What are Log File Monitors?MiscellaneousCasting in PostgreSQLPostgresHow to disable scrolling on html body on menu click using JavaScript?JavaScript