PHP Constant | Variables & Constants Examples

Constants are like variables but there are many differences between constants and variables. The constant value cannot be changed during execution time. A variable value can be changes execution time using a PHP script. If the value of the constant is defined they can not be changed.In another programming languages constants have same behavior.

As you know that, we do not need to use PHP define() function to declare a variable but if you are creating a constant, you have to use define() function. Without define() function, you can not able to create constant. Generally, constants are in global scope.

By default, Constants are case sensitive. We can enable case insensitivity while defining constants. PHP constants should be define uppercase letter.You can start constant with underscore(_) or letter. Constants are not defined with $ sign, unlike variables. Never use $ sign before the constants name.

Let's have a look of constants syntax.

PHP constant syntax


    
define(name, value, case-insensitive)

Defining PHP constants

Name- Specified the name of constant

Value –Refers to constant value


Case-insensitive –define the constant case insensitive behaviour .


The above syntax cleared that you have to define a constants name, value, and case insensitive.

The following example will help you understand PHP constants better.

Example


<?php
define("LEARN", "Learn on technosmarter.com!");
echo LEARN ;
?>
		
		
Run

In above example , we have declared & defined -

Constant name- LEARN

Constant Value -Learn on technosmarter.com!

case-insensitive- By default "False" .

When we run the script, It gives constant value "Learn on technosmarter.com" as an output.

As we have discussed before constants global scope. Constants are global that cross the entire script.

Now we will implement with an example.

Here, we will declare & define a constant using define a function. After that, we will create a function study() and get the constant key-value (constant is defined as a key-value pair). The key will help you to get the value of constant.

Example


<?php
define("LEARN", "Learn on technosmarter.com!");

function study() {
    echo LEARN;
}
 
study();
?
>
Run

In the above example, .we have declared and defined constant value and created a function study().

When we get the key-value under the function it gives the value of constant "Learn on technosmarter.com!" as output. This example proved that constants are automatically global because we have declared and define constant outside the function and got the value under the function. If the constants are not in global scope, they give an error.

As you that, constants are case sensitive by default. We can create them case insensitive. To make them case insensitive, we have to set the case insensitive flog to true.

Example


<?php
define("LEARN", "Learn on technosmarter.com!",true);
echo learn ;
?>
		
		
Output-
 

Learn on technosmarter.com!

In the above example, we made constant case insensitive because -

1. We defined constants name in uppercase "LEARN".

2. We got the value of constant using lowercase key-value "learn".

Constant() function-



The constant() function is more essential to retrieve the value of the constant by creating an id. The constant function code uses to get the value of constant by id.

  
     define("CI",8.0);
define("NAME","JACKSON");
$id = "CI";
echo constant($constant);
     
     
     

Difference between constants and variables

Constants and variables are very different. 1. Constants never start with a $ sign but variables are declared with the $ sign. Constant- Learn. Variable - $Learn.

2. By default, constants have global scope, they can across the entire script but the variables are not in global scope.

3. The value can not be changed during the execution time if the constants are defined. Variables can be changed after defined using PHP script.

4. If the variables are defined outside the function and you want to call under the function it produces an error.

 
    
    <?php
   $x=10; 
   
   function myTest() 
   
   {
   echo $x; 
   
   } 
   
   myTest();
?>
    
      
In the above example, we declared and defined the variable outside the function. We called the variable inside the function. It gives an error.
 
      Notice: Undefined variable: x in C:\xampp\htdocs\PHP\constant.php on line 7
      
By default, variables are not in global scope as like constants.

let's take the same example for constant.
 
    
    <?php
 define("x",10);
   function myTest() 
   
   {
   echo x; 
   
   } 
   
   myTest();
?>
    
      
Output-
 
      
      10
      

Hence! constants are global. We can make global scope variables using variables using .

  
<?php      
$x=1;    
$y=7;

function test()
{
 global $x,$y,$z; 
 $z=$a+$b;
   
} 

test();
echo $z;

?>

Output:
 
8

Hope! Now you have understood constants and variables.