Constants in PHP

Constants are supported by various data types such as character, string, boolean, and numeric values.

Explanation

Here is an example demonstrating the use of constants.

<?php
define("S", "constant");

echo S;

define declares a constant value.

<?php
define("N", 500000000);

A define statement can appear anywhere a variable declaration can.

<?php
define("D", 3e20 / N);
echo D;

Constant expressions perform arithmetic with arbitrary precision.

<?php
echo (int)D;

A numeric constant has no type until it’s given one, such as by an explicit conversion.

<?php
echo sin(N);

A number can be given a type by using it in a context that requires one, such as a variable assignment or function call. For example, here sin expects a float.

$ php constant.php 
constant
6E+11
600000000000
-0.28470407323754

Next example: For.