Constants are fixed that once they are
defined they cannot be changed .
Example
3:
Constants are name or identifier that can't be changed during the
execution of the script.
Rules:
It can be started with letter or
underscore only.
PHP constants should be defined in
uppercase letters
It can be defined by 2
ways:
Using define () function
Using const keyword
define()
Syntax:
define(name, value, case-insensitive)
name: specifies the
constant name
value: specifies the
constant value
case-insensitive: Default
value is false. By default it is case sensitive.
Example 1:
<?php
define("MSG","LearningPoint92");
echo MSG;
?>
Output:
LearningPoint92
Example 2:
<?php
define("MSG","LearningPoint92",true);//not case sensitive
echo msg;
?>
Output:
LearningPoint92
<?php
define("MSG","LearningPoint92",false);//case sensitive (It provides error)
echo msg;
?>
Output:
error
Notice:
Use of undefined constant msg - assumed 'msg'.
const keyword
const keyword defines constants at compile
time.
It is always case sensitive.
Example:
<?php
const MSG="LearningPoint92";
echo MSG;
?>
Output:
LearningPoint92
No comments:
Post a Comment