Constants in PHP

Constants in PHP
 
Constants in PHP
  • Currently 0 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Rating: 0/5 (0 votes cast)

Thank you for rating!

You have already rated this page, you can only rate it once!

Your rating has been changed, thanks for rating!

Log in or create a user account to rate this page.


In some cases the variables are quite difficult to use for storing any defined values that do not change while the program is run. These values can be mathematical constants, file paths, different passwords, etc. For this purpose PHP provides such a construct, as a constant.

A constant is a named value that doesn't change while the program (script) in running.

Unlike a variable, you can not change the value of a constant that has been assigned to it when declared. Constants are useful for storing values that shouldn't be changed at run time. Constants may contain only scalar data (integer, boolean, float or string).

In PHP constants are defined by the function define. This function has the following format:

define( $name, $value, $case_sen ), where:

$name - name of the constant;
$value - value of the constants;
$case_sen - optional logical parameter, it indicates whether or not to ignore the case-sensitivity (true) or not (false).

An example of how to define and use constants in PHP:

define( 'pi', 3.14, true );
echo pi;
// Output 3.14

If the parameter $case_sen is true, the interpreter will take into account the case when working with the constant. Note that the dollar sign $ is not used before a constant.

Differences between constants and variables:

  • Constants don't have a dollar sign ($) before them;
  • Constants may only be defined using the function define, and not by assigning value;
  • Constants may be defined and accessed anywhere without regard to variable scoping rules;
  • Once defined, constants may not be redefined or undefined;
  • Constants may only have scalar values.

How to verify the existence of constants

To verify the existence of constants you can use the function defined. This function returns true, if the constant was declared. Here is an example:

// Declare constant pi
define( 'pi', 3.14, true );
if( defined( 'pi' ) == true ) echo "Constant 'pi' is declared!";
// Script output "Constant 'pi' is declared!"

Predefined constants PHP

In PHP, the following predefined constants exist:

PHP provides a large number of predefined constants for every type of executed script. Many of these constants are defined by various extensions, and will be present only if the extensions were available in the process of dynamic load or as a result of static linking.

There are five pre-defined constants that change their values depending on the context in which they are used. For example, the constant __LINE__ depends on the line in the script, in which this constant is positioned. Special constants are case-insensitive and are listed below:

Name

Description

__LINE__

The current line in the file.

__FILE__

Full path and name of the current file.

__FUNCTION__

Name of the function. (Added in PHP 4.3.0.)

__CLASS__

Name of the class. (Added in PHP 4.3.0.)

__METHOD__

 Name of the method of the class. (Added in PHP 5.0.0)


 

Read also

What is PHP?

What is PHP?

Classes and objects in PHP

Classes and objects in PHP

How to access classes and objects in PHP

How to access classes and objects in PHP

Post comment

basicuse.net
html5_css
106160485398655174790

Quick navigation

General navigation