PHP Tutorial
Home
PHP Install Xampp
PHP Syntax & Comments
PHP Variables
PHP Constants
PHP Data Types
PHP Echo & Print
PHP Strings
PHP If...Else...Elseif
PHP Ternary Operator
PHP Loops
PHP Functions
PHP Arrays
PHP GET & POST
PHP Advanced
PHP Date and Time
PHP Include and Require
PHP File Upload
PHP Sessions
PHP Cookies
PHP Send Email
PHP JSON Parsing
PHP MySQL Database
PHP MySQL Introduction
PHP Connect to MySQL
PHP MySQL Create DB
PHP MySQL Create Table
PHP MySQL Insert Data
PHP MySQL Select Data
PHP MySQL Delete Data
PHP MySQL Update Data
PHP MySQL Where
What is Constant in PHP
Constant is a name for a fixed value. Constants are such as variables and it's accept that once they are defined they cannot be changed.
There are two ways to define a constant variable in PHP.
1. Using the define() method.
2. Using the const keyword.
Using define()
Create constants in PHP
Syntax :
define(name, value, case-insensitive)
Create a constant with a case-sensitive name:
<?php // Defining constant define("SITE_URL", "https://howtowebcode.com/"); // Using constant echo 'Thank you for visiting - ' . SITE_URL; ?>
Create a constant with a case-insensitive name:
<?php // Defining constant define("SITE_URL", "https://howtowebcode.com/", true); // Using constant echo 'Thank you for visiting - ' . site_url; ?>
Using the const Keyword
We can also create constants in PHP using the const keyword. But we can only use the const keyword to define the constants. only integers, booleans and strings, floats, while define() can be used to define array and resource constants as well although they are not used oftenly.
<?php const SITE_URL = "https://howtowebcode.com/"; echo SITE_URL; ?>