PHP Constant

PHP 7 Constants are similar to variables, holding information to be accessed later, except that they are what they sound like—constant. In other words, once you have defined one, its value is set for the remainder of the program and cannot be altered.
One example of a use for a constant might be to hold the location of your server root
(the folder with the main files of your website). You would define such a constant like
this:

 define("ROOT_LOCATION", "/user/local/www/");

Then, to read the contents of the variable, you just refer to it as a regular variable (but
it isn’t preceded by a dollar sign):

 $directory = ROOT_LOCATION;

Now, whenever you need to run your PHP code on a different server with a different
folder configuration, you have only a single line of code to change.

 

The main two things you have to remember about constants are that
they must not be prefaced with a $ (as with regular variables), and
that you can define them only using the define function.

It is generally considered a good practice to use the only uppercase for constant variable names, especially if other people will also read your code.

Predefined Constants

PHP comes ready-made with dozens of predefined constants that you generally will be
unlikely to use as a beginner to PHP. However, there are a few—known as the magic
constants—that you will find useful. The names of the magic constants always have two underscores at the beginning and two at the end, so that you won’t accidentally try to name one of your own constants with a name that is already taken. They are detailed in Table 3-5. The concepts referred to in the table will be introduced in future PHP tutorials.

Table 3-5. PHP’s magic constants

Constant 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.
In PHP4.0.2, __FILE__ always contains an absolute path with symbolic links resolved, whereas in older
versions it might contain a relative path under some
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is
equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the
root directory. (Added in PHP 5.3.0.)
__FUNCTION__ The function name. (Added in PHP 4.3.0.) As of PHP 5, returns the function name as it was declared (case sensitive).
In PHP 4, its value is always lowercase.
__CLASS__ The class name. (Added in PHP 4.3.0.) As of PHP 5, returns the class name as it was declared (case-sensitive).
In PHP 4, its value is always lowercase.
__METHOD__ The class method name. (Added in PHP 5.0.0.) The method name is returned as it was declared (case-sensitive).
__NAMESPACE__ The name of the current namespace (case-sensitive). This constant is defined at compile time. (Added in
PHP 5.3.0.)

Basically, One handy use of these variables is for debugging purposes when you need to insert a line of code to see whether the program flow reaches it:

echo "This is a line " . __LINE__ . " of file". __FILE__;

the current program line in the current file (including the path) causes by it being executed to be output to the web browser.

 

 

<< Prev Next >>

Loading