A consistent is a name or an identifier for a basic quality. A consistent quality can’t change amid the execution of the script. As a matter of course, a consistent is case-touchy.

To characterize a consistent you need to utilize define() function and to recover the estimation of a steady, you need to just indicating its name.
Example:-
<?php

define(“India“, 50);

echo India;

echo constant(“India”);

?>
Differences between constants and variables are:-

  • There is no compelling reason to compose a dollar sign ($) before a consistent, where as in Variable one needs to compose a dollar sign..
  • Constants might be characterized and got to anyplace without respect to variable perusing rules.
  • Once the Constants have been set, may not be reclassified or indistinct.

PHP Magic constants:-
PHP gives countless constants to any script which it runs. There are five enchanted constants that change contingent upon where they are utilized.

  1. __LINE__:- The current line number of the file.
  2. __FILE__:- The full way and filename of the record. In the event that utilized inside an incorporate, the name of the included document is returned. Subsequent to PHP 4.0.2, __FILE__ dependably contains an outright way though in more seasoned forms it contained relative way under a few circumstances.
  3. __FUNCTION__:- The capacity name. (Included PHP 4.3.0) As of PHP 5 this consistent returns the capacity name as it was pronounced (case-delicate). In PHP 4 its worth is constantly lowercased.
  4. __CLASS__:- The class name. (Included PHP 4.3.0) As of PHP 5 this consistent returns the class name as it was pronounced (case-delicate). In PHP 4 its worth is constantly lowercased.
  5. __METHOD__:- The class strategy name. (Included PHP 5.0.0) The strategy name is returned as it was pronounced (case-delicate).
Categories: PHP

0 thoughts on “PHP Constants”

Leave a Reply