Free download web templates, books and more. Free php projects ,games and apps. GTA mods and more

Breaking

Php constants |€ how to use pHP constants

How To Use PHP Constants

Php constant consider the key and value.
PHP constants are name or identifier that can't be changed during the execution of the script. PHP constants can be defined by 2 ways:
  1. Using define() function
  2. Using const keyword
PHP constants follow the same PHP variable rules. For example, it can be started with letter or underscore only.
Conventionally, PHP constants should be defined in uppercase letters.

PHP constant method: define()

Let's see the syntax of define() function in PHP.
  1. define(name, value, case-insensitive)  
  1. name: specifies the constant name
  2. value: specifies the constant value
  3. case-insensitive: Default value is false. It means it is case sensitive by default.
Let's see the example to define PHP constant using define().
File: constant.php
  1. <?php  
  2. define("MESSAGE","Hello samadroid PHP");  
  3. echo MESSAGE;  
  4. ?>  
Output:
Hello samadroid PHP
File: constant.php
  1. <?php  
  2. define("MESSAGE","Hello samadroid PHP",true);//not case sensitive  
  3. echo MESSAGE;  
  4. echo message;  
  5. ?>  
Output:
Hello samadroid PHPHello JavaTpoint PHP
File: constant.php
  1. <?php  
  2. define("MESSAGE","Hello samadroid PHP",false);//case sensitive  
  3. echo MESSAGE;  
  4. echo message;  
  5. ?>  
Output:
Hello samadroid PHP
Notice: Use of undefined constant message - assumed 'message' 
in C:\wamp\www\vconstant3.php on line 4
messagee

PHP constant: const keyword

The construction key word is very easy method
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.
File: constant.php
  1. <?php  
  2. const MESSAGE="Hello const by samadroid PHP";  
  3. echo MESSAGE;  
  4. ?>  
Output:
Hello const by samadroid PHP