Constant in PHP


Prev Tutorial Next Tutorial

Constant in PHP

Constants are name or identifier that can't be changed during the execution of the script. In php constants are define in two ways;

  • Using define() function
  • Using const keyword

In php decalare constants follow same rule variable declaration. Constant start with letter or underscore only.

Create a PHP Constant

Create constant in php by using define() function.

Syntax

define((name, value, case-insensitive)
  • name: Specifies the name of the constant
  • value: Specifies the value of the constant
  • case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false

In below example we create constant with case-sensitive

Example of constant in php

<?php
define("MSG","Hello world!");  
echo MSG;  
?>

Output

Hello world!

In below example we create constant with case-insensitive

Example of constant in php

<?php
define("MSG","Hello world!", true);  
echo msg;  
?>

Output

Hello world!

Define constant using cons keyword in php

The const keyword defines constants at compile time. It is a language construct not a function. It is bit faster than define(). It is always case sensitive.

Example of constant in php

<?php
cons MSG="Hello world!";  
echo MSG;
?>

Output

Hello world!

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements