CodesJava

Easy learning with example program codes

PHP Constants


PHP Constants are the identifiers that once defined, cannot be modified. PHP Constants can either be defined using a define() function or a const keyword. Like PHP Variables, PHP Constants name can only be started with a letter or an underscore only. PHP constants are globally defined only.

PHP define() function:

Syntax:

define(name, value, case-insensitive)
  • Name: This parameter is used to specify the name of the constant.
  • Value: This parameter is used to specify the value of the constant.
  • Case-insensitive: This parameter is used to specify whether the constant is to be case-sensitive or case insensitive. It can take two values: True or False. By default the value is False, i.e, the constant is understood to be case sensitive.

PHP const keyword:
PHP const keyword is a faster method to define constants as it can only define case sensitive constants and only at the compile time.

Example 1: Defining PHP Constants using define() function

<!DOCTYPE html>
<html>
<body>
 
<?php
define("Name", "Shanaya");
echo Name;
?>
 
</body>
</html>

Example 2: Defining PHP Constants using const keyword

<!DOCTYPE html>
<html>
<body>
 
<?php
const MESSAGE="Hello World";  
echo MESSAGE; 
?>
 
</body>
</html>
Posted in PHP   
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki