dots Created with Sketch.
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.

amazon

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

#Jul 22 Is there a CSS parent selector? #Jul 22 Difference between :where and :is in CSS #Jul 22 Does :is() pseudo selector hint at CSS preprocessing in the future? #Jul 22 Control Scrolling with CSS Scroll Snap #Jul 21 Control rendering using CSS content-visibility property #Jul 21 How to use @supports rule in CSS?
You might also like these
How to use @supports rule in CSS?CSSbin2hex() and chr() String Functions in PHPPHPHow to animate list items using CSS and JavaScript?CSSJavaScript String fromCharCode() MethodJavaScriptHow to create a simple slider with CSS and jQuery?CSSEXISTS and NOT EXISTS in PostgresPostgresCanvas Drawing in HTML5HTMLHow to hide a DIV on clicks outside of it using jQuery?jQueryHow to center an image horizontally and vertically?CSSHow to use data-* Attributes in HTML?HTMLLearn to Establish Connection using MySQLi (object-oriented), MySQLi (procedural) and PDO with Example CodePHPHow to get current timestamp in Swift 4 and 5?Swift