PHP Constants (With Examples)

By Shahnail Khan Last updated : December 7, 2023

Whether you're a beginner or looking to refresh your knowledge, this tutorial aims to simplify the understanding of PHP constants.

What are PHP Constants?

PHP constants are like containers for values that don't change throughout the script's execution. Once defined, their values remain constant throughout the entire script. Constants are useful for storing configuration settings, fixed values, or any data that should remain unchanged during the script's execution.

Create a PHP Constant

In PHP, constants are created using the define() function.

Syntax

The basic syntax for defining a constant is as follows:

define("CONSTANT_NAME", "constant_value");

Here,

  • define: This is the keyword used to create a constant in PHP.
  • "CONSTANT_NAME": This is the name of the constant. It follows the same rules as variable names, but it's recommended to use uppercase letters with underscores for constants (e.g., INCLUDE_HELP).
  • "constant_value": This is the value assigned to the constant. It can be a string, number, or any valid PHP expression.

Example

Here's an example of how to define a constant named BEST_TUTORIAL with the value "PHP":

define("BEST_TUTORIAL", "PHP");

Once a constant is defined, you can use it throughout your script, and its value cannot be changed during the script's execution.

To use a constant, you simply refer to it by its name, like this:

echo BEST_TUTORIAL;

Output of PHP Constants

Remember that constants are case-sensitive by default, so BEST_TUTORIAL and best_tutorial would be considered two different constants.

It is a good practice to define constants without the leading dollar sign ($), which is used for variables in PHP. Constants are accessed without the dollar sign, making it clear that they are constants rather than variables.

PHP const keyword

In addition to using the define() function, PHP also provides the const keyword to define constants. The const keyword is a language construct and can be used only at the start of your script, outside of any function or class.

Syntax

Here's the syntax for using const to define a constant:

const CONSTANT_NAME = "constant_value";

Here,

  • const: This is the keyword used to create a constant using the const syntax.
  • CONSTANT_NAME: This is the name of the constant, following the same rules as variables .
  • constant_value: This is the value assigned to the constant. It can be a string, number, or any valid PHP expression.

Examples on PHP Constants

Let's take some examples to learn how to use constants in PHP.

Example 1

<?php
// Using define() to create a constant
define("Website", "Includehelp");

// Using const keyword to create a constant
const tutorial = "PHP";

// Accessing and displaying the values of the constants
echo "Welcome to " . Website . "<br>";
echo "Tutorial_1 - " . tutorial;
?>

The output of the above example is:

Welcome to Includehelp
Tutorial_1 - PHP

In this example, we define a constant Website using the define() function and another constant tutorial using the const keyword. We then use these constants in the echo statements to display their values.

Example 2

You are working on an e-commerce platform, and you need to create a PHP script that calculates the discounted price of a product based on a given percentage discount.

Create a PHP script that does the following:

  • Define a constant for the product price.
  • Define a constant for the discount percentage (e.g., 10%).
  • Calculate the discounted price using the formula: Discounted Price = Product Price - (Product Price * Discount Percentage) / 100.
  • Display the original price, discount percentage, and the discounted price.
<?php
// Define a constant for the product price
const PRODUCT_PRICE = 500.0;

// Using define() for discount percentage
define("DISCOUNT_PERCENTAGE", 25);

// Calculate the discounted price
$discountedPrice = PRODUCT_PRICE - (PRODUCT_PRICE * DISCOUNT_PERCENTAGE) / 100;

// Display the information without number_format
echo "Original Price: Rs. " . PRODUCT_PRICE . "<br>";
echo "Discount Percentage: " . DISCOUNT_PERCENTAGE . "<br>";
echo "Discounted Price: Rs. " . $discountedPrice;
?>

The output of the above example is:

Original Price: Rs. 500
Discount Percentage: 25
Discounted Price: Rs. 375

In this example, we define constants for the product price and the discount percentage. It then calculates the discounted price and displays the original price, discount percentage, and the discounted price.

This problem introduces the use of constants in a more practical scenario.

Are PHP Constants Global?

Yes! All constants in PHP are global (automatically). Hence, you can use them across the entire scrip.

Learn: PHP Variable Scope.

Example:

This example demonstrates the global scope of PHP constants.

<?php
define("WELCOME", "Welcome to PHP Tutorial.");

function printWelcomeMessage()
{
    echo WELCOME;
}

printWelcomeMessage();
?>

The output of the above example is:

Welcome to PHP Tutorial.

PHP Constants Vs. PHP Variables

This table provides a quick overview of the key differences between PHP constants and variables.

Feature PHP Constants PHP Variables
Definition Defined using define() or const keyword. Defined using the $variableName syntax.
Modification Cannot be modified or redefined once set. Can be modified and reassigned throughout the script.
Scope Global scope by default. Can be local (inside a function or block) or global depending on where they are defined.
Usage Used for values that should remain constant throughout the script. Used for values that may change during the script's execution.
Syntax define("CONSTANT_NAME", "constant_value"); or const CONSTANT_NAME = "constant_value"; $variableName = "variable_value";
Access Accessed without the $ sign. (e.g., CONSTANT_NAME) Accessed with the $ sign. (e.g., $variableName)
Example define("PI", 3.14); $radius = 5;<br>$area = 50;

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.