What are constants in php?

Simply constant is an identifier or a name of a value that you will define in your php script. The constant value will never be changed during run time .So we see that in php how we define the constant .

We create the constant by a php function define() function.

Syntax of define() function

define(constant_name,constant_value,case-insensitive);

constant_name: this is a first parameter of a function which tells the name of your constant .but ensure that your constant are in UPPERCASE. this is way to write a name of constant in php. constant_value: this is a second parameter of a function which tells the value of your constant. case-insensitive: Third parameter says that your lowercase and uppercase name of your constant are equal. but by default this options is false .if you want to print your constant by lowercase but you define the constant name in UPPERCASE. so third parameter you manually write true .

let’s see the example of constant

<?php
  define('PI',3.14);
  echo "The value of 22/7 is:".PI;
?>

So in above example we define a constant name PI .The constant value is global.you can use anywhere inside the function or within your script.

If you want to print the constant by lowercase.so you manually pass true parameter in define() function .

Let’s see the example of printing the value of constant by lowercase

<?php
  define('PI',3.14,true);
  echo "The value of 22/7 is:".pi;
?>

So in above example you can print the value of constant by lowercase name. if you write BASE instead this you can print it by base.

0 0 vote
Article Rating

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x