PHP Constants

← Back to Index


Constant 1: SITE_NAME

define("SITE_NAME", "UWM INFOST 440 Course Portal");
echo SITE_NAME;
    

Output: UWM INFOST 440 Course Portal


Constant 2: PI_VALUE

define("PI_VALUE", 3.14159265);
$radius = 5;
$area = PI_VALUE * $radius * $radius;
echo "Area of circle with radius 5: " . $area;
    

Output: Area of circle with radius 5: 78.5398


Constant 3: MAX_STUDENTS

define("MAX_STUDENTS", 30);
echo "Max class size: " . MAX_STUDENTS;
    

Output: Max class size: 30


Explanation

In PHP, a constant is a named value defined once using define() that cannot be changed or redefined during the script. Constants do not use the $ prefix that variables use, and by convention their names are written in ALL UPPERCASE. Unlike variables, constants are automatically globally accessible throughout the entire script without any special declaration. Variables begin with $, can be reassigned at any time, and are limited in scope to the block or function where they are declared. Constants are ideal for values that should never change, such as mathematical constants, configuration settings, or fixed limits, because they protect against accidental modification and make your code easier to read and maintain.

INFOST 440 — Activity 3 — Nyla Broughton