In this article Constants in PHP we give the information about Constants are used to store a value that never changes during the execution of code.

Constants in PHP:

Constants and magic constants in PHP are used to store data that does not change throughout the course of the code. These are very useful for programming.

PHP Constants

What are the constants?

Constants are used to store a value that never changes during the execution of code.

Features of Constants:

  1. Once defined, the value of constants cannot be changed.
  2. Names of constants are written without the $ sign.
  3. Constants in PHP are declared with define() or const keyword.
  4. Constants are always global in scope.

Ways to declare constants

a) use of define()

<?php

    define(“GREETING”, “Hello World!”);

    echo GREETING; //Output: Hello world!

?>

b) Use of const keyword

<?php

    const PI = 3.14;

    echo PI; // output: 3.14

?>

Use of Constants

<?php

    define(“SITE_NAME”, “My Website”);

    define(“MAX_USERS”, 100);

        echo “Website Name: “. SITE_NAME. “<br>”;

    echo “Maximum users: ” . MAX_USERS;

?>

Output:

Website Name: My Website

Max Users: 100

Case-sensitivity with constants

In define() you can create case-sensitive or case-insensitive constants.

<?php

    define(“HELLO”, “Hello!”, true); // Third parameter enables case-insensitive

    echo hello; // Output: Hello!

?>

Arrays with Constants (PHP 7.0 and later)

Arrays can now be used for constants.

<?php

    const COLORS = [“red”, “green”, “blue”];

    echo COLORS[1]; // output: green

?>

PHP Magic Constants

What are magic constants?

Magic constants are pre-defined constants in PHP that provide special information about code. Their value may change according to the code.

List of magic constants

magic constant description

__LINE__ Returns the current line number of the code.

__FILE__ Returns the full path and name of the file.

__DIR__ Returns the directory path of the file.

__FUNCTION__ Returns the name of the function.

__CLASS__ Returns the name of the class.

__TRAIT__ Returns the name of the trait.

__METHOD__ Returns the name of the method of the class.

__NAMESPACE__ Returns the name of the current namespace.

Examples of Magic Constants

a) __LINE__

<?php

    echo “This is the line number: ” . __LINE__;

?>

Output:

This is line number: 2

b) __FILE__

<?php

    echo “Path to file: ” . __FILE__;

?>

Output:

File path: /var/www/html/example.php

c) __DIR__

<?php

    echo “Directory: ” . __DIR__;

?>

Output:

Directory: /var/www/html

d) __FUNCTION__ and __METHOD__

<?php

    function myFunction() {

        echo “Function Name: ” . __FUNCTION__;

    }

    myFunction();

?>

Output:

Function Name: myFunction

<?php

    class MyClass {

        public function myMethod() {

            echo “Method name: ” . __METHOD__;

        }

    }

    $obj = new MyClass();

    $obj->myMethod();

?>

Output:

Method Name: MyClass::myMethod

e) __CLASS__ and __NAMESPACE__

<?php

    namespace MyNamespace;

    class MyClass {

        public function displayClass() {

            echo “Class Name: ” . __CLASS__ . “<br>”;

            echo “Namespace name: ” . __NAMESPACE__;

        }

    }

    $obj = new MyClass();

    $obj->displayClass();

?>

Output:

Class Name: MyNamespace\MyClass

Namespace name: MyNamespace

Conclusion

Constants are used to store static data, whereas Magic constants provide information about specific parts of the program. Both of these help in making PHP code more effective and organized.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *