PHP Constant

PHP Constants

PHP CONSTANTS | WORLDVIEWITPHP CONSTANTS | WORLDVIEWIT

PHP CONSTANTS

A constant is described as the fixed value, who cannot change. In PHP Constants are the name of the value that cannot be changed at that time when a PHP script is in processing form.

Constant are unlike to the variables. Once a value assigned to the constant it remains the same during the program(execution) running time. In simple words, contents are just the names or identifiers that are used to call the values which are fixed during the program execution cycle.

MATHEMATICAL EXAMPLE

x+7=3

Hence in the example, 7 and 3 are constant. 

HOW TO DEFINE A PHP CONSTANT

defined() is used in PHP for constant defining. This function takes two values in it. These values are also said as arguments. The first value is declared as the name of constant and the second value is the value, of the first value. Once a constant is defined you can call it by its name which is first value/argument at any time in the program. By default the name of constant is case sensitive in the last there’s example to change your constant in insensitivity.

<?php
//defining constant
define("WEB_SITE", "www.worldviewit.com");
 
//DISPLAYING VALUE STORED IN THE PHP CONSTANT
echo "WELCOME TO:" . WEB_SITE;
?>

 

OUTPUT

WELCOME TO:  www.worldviewit.com

 

In the upper example, we define a constant with the name "WEB_SITE" and store the website name "www.worldviewit.com" in the constant named as "WEB_SITE". Then at the time of displaying/echo, I just write the name of the constant name after “WELCOME TO:” . WEB_SITE and it displays the website name.

IMPORTANT THING TO REMEMBER AT THE TIME OF CONSTANT DECLARATION

At the time of constant declaration, you must have to follow some rules as in the variable declaration. Constant name starts with the letter (small/capital) and underscore is also used in the constant name and then also used number or letter after it. In the constant no need of prefix as in the variable which is $ sign.

NOTE: MOST PROBABLY CONSTANT NAMES ARE WRITTEN IN UPPER ALPHABETS SO THAT THEY CAN BE EASILY REMEMBERABLE & DIFFERENTIATE.

To set your constant as case-insensitive we have the opportunity to use true inside define() braces, by default it is false which means case sensitive syntax (constant name). The format is given below:-

define(constant name,constant value, case – insensitive);

<?php
//defining constant
define("WEB_SITE", "www.worldviewit.com", true);
 
//DISPLAYING VALUE STORED IN THE PHP CONSTANT
echo "WELCOME TO:" . web_site; 
?>

 

Output of CASE INSENSITIVE CONSTANT

WELCOME TO:  www.worldviewit.com

 

In the example, there is a insensitive constant with the name "WEB_SITE" and store the website name "www.worldviewit.com" in the constant named as "WEB_SITE". Then when echo, I just write the name of the constant after “WELCOME TO:” .  web_site and it displays the website name.

By default constant are considered as global which means used anywhere in the script.

2 Comments on “PHP Constant

  1. Hi There!

    We are reaching out to those from the math education space who would be excited to participate in our FREE Virtual Summit coming up on November 13th and 14th, 2021.

    Not only will you love hearing 25+ influencers from the math education space presenting live over Zoom, but I'm sure your audience and colleagues will stand to benefit as well!

    Register to save your seat here: https://zeep.ly/GwPnv

    Will we see you there?

    Kyle & Jon
    admin@makemathmoments.com
    https://zeep.ly/GwPnv

    PS - Consider sharing this amazing free event with others in the education space!

  2. YOU NEED REAL VISITORS FOR: worldviewit.com ?

    We Provide 100% Real Visitors To Your Website.
    With this traffic, you can boost ranking in SERP, SEO, profit from CPM...

    CLAIM YOUR 24 HOURS FREE TEST HERE=> https://zeep.ly/Kp4UB

Leave a Reply

Your email address will not be published. Required fields are marked *

*