Tuesday, June 15, 2021
More

    Php Constants

    Must Read

    Programmerhttp://www.improgrammer.net
    We started this site to inspire young minds to motivate and encourage them towards Programming Language. In this site you will get programming tutorials, tech, programming facts, programming fun and programming blogs.

     

    If you look up the word constant in a dictionary it will probably tell you that the word is used to describe something that is non-changing and non-variable, and this is exactly the purpose of constants in PHP. A PHP constant is the opposite of a variable in that once it has been defined it cannot be changed.

    Constants are particularly useful for defining a value that you frequently need to refer to that does not ever change.

    A constant is a name or an identifier for a simple value. A constant value cannot change during the execution of the script. By default a constant is case-sensitive. By rule, constant identifiers are always uppercase. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores (no $ sign before the constant name). If you have defined a constant, it can never be changed or undefined.

    To set a constant, use the define() function – it takes three parameters: The first parameter defines the name of the constant, the second parameter defines the value of the constant, and the optional third parameter specifies whether the constant name should be case-insensitive. Default is false.

    Only scalar data (boolean, integer, float and string) can be contained in constants.

     

    The example-1 below creates a case-sensitive constant, with the value of “improgrammer.net!” :

    Example-1:

    <?php
    // define a case-sensitive constant
    define(“PROGRAMMER”, “Welcome to improgrammer.net!”);
    echo PROGRAMMER;
    echo “<br>”;
    // will not output the value of the constant
    echo programmer;
    ?>

    Output:
    Welcome to improgrammer.net!
    programmer

    The example-2 below creates a case-insensitive constant, with the value of “improgrammer.net!” :

    Example-2:

    <?php
    // define a case-insensitive constant
    define(“PROGRAMMER”, “Welcome to improgrammer.net!” , true);
    echo PROGRAMMER;
    echo “<br>”;
    // will also output the value of the constant
    echo programmer;
    ?>

    Output:
    Welcome to improgrammer.net!
    Welcome to improgrammer.net!

    Now,The differences between constants and variables are:

    • There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.
    • Constants cannot be defined by simple assignment, they may only be defined using the define() function.
    • Constants may be defined and accessed anywhere without regard to variable scoping rules.
    • Once the Constants have been set, may not be redefined or undefined.

    PHP Predefined Constant :

    PHP provides a large number of predefined constants to any script which it runs.

    There are five magical constants that change depending on where they are used. For example, the value of __LINE__ depends on the line that it’s used on in your script. These special constants are case-insensitive and are as follows:

    Name Description
    LINE The current line number of the file.
    FILE The full path and filename of the file. If used inside an include,the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path whereas in older versions it contained relative path under some circumstances.
    FUNCTION The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
    CLASS The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
    METHOD The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).

    Latest Articles

    More Recipes Like This