What is Constant in php?

constant is a user defined name or identifier. which can't be changed during the execution of the php program. it is the final variable. its value can't be change.


in php, there are two ways to define constant.

  • Using define() function
  • Using const keyword
  • NOTE : Conventionally, PHP constants should be defined in uppercase letters.


    How to define constant in php :

    Syntax :


    define(name, value, case-insensitive)


  • name: specifies the constant name
  • value: specifies the constant value
  • case-insensitive: Default value is false. It means it is case sensitive by default.
  • File name : index.php

    <?php
    define("MSG","Hello ittutorial");
    echo MSG;
    ?>

    Output :-

    Hello itechtuto

    File name : index.php

    <?php
    define("MSG","Hello ittutorial", true);
    // //not case sensitive echo MSG;
    echo msg;
    ?>

    Output :-

    Hello itechtuto Hello itechtuto

    File name : index.php

    <?php
    define("MSG","Hello ittutorial", false);
    // //case sensitive echo MSG;
    echo msg;
    ?>

    Output :-

    Hello itechtuto Notice: Use of undefined constant message - assumed 'msg' in C:\wamp\www\vconstant3.php on line 4 msg

    const keyword :-

  • const keyword is used for defien constant variable.
  • it is faster than define()
  • It is always case sensitive.
  • <?php
    const MSG = "Hello ittutorial";
    echo MSG;
    ?>




    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here