• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » PHP » PHP Constants Types
Next →
← Prev

PHP Constants Types

By Dinesh Thakur

In programming, PHP allows you to define constants, it is often necessary to define data structures whose value must not change during program execution. These data structures are typically what is known constants. The most famous mathematical constants is for instance the PI number whose value is approximately 3.1415926535898. Note that PHP natively integrates the M_PI constant. This can then be used for any calculation of circumference.

The constants declare by the define () statement.
So,
define (“Country“, “India”);
define (“Code”,91 );
creates two constants, and Country Code, which can be used very naturally;
echo Country.” “.Code;

For example. Begin by studying how constant is declared in a PHP program.

We’ll be covering the following topics in this tutorial:

  • Creating and playing constants
  • The native function define ()
  • Declaration of a Constant
  • Declaring a new constant
  • Redefinition of a constant
  • The predefined constants

Creating and playing constants

<?php
   define ("PI",3.1415926535, TRUE);
   echo "The PI constant is ", PI, "<br />";
   echo "The PI is constant ", pi, "<br />";
   // Verification of existence
   if (defined ("PI")) echo "The PI constant is already defined", "<br />";
   if (defined ("pi")) echo "The constant pi is already defined", "<br />";
   // set sensitive, verification of the existence and use
   if (define ("site", "http://.ecomputernotes.com", FALSE))
      {
           echo "<a href=\"",site, "\"> Link to my site </ a>";
      }
?>

The native function define ()

The constant declaration is prepared using the native function define () which takes three parameters, the first two are required. Here are the details of the official documentation of the define () function:

Prototype define () function

boolean define (string $name, mixed $value, [boolean $case_insensitive])

• The first parameter of the function is a string that defines the name of the constant. By convention, all constants must be written in capital letters.
• The second is the value that is assigned to that constant. This may be a string, an integer, a real or even a Boolean.
• The third argument is optional. This is a boolean indicating whether the interpreter should be concerned with the case or not constant.

We advise never to complete this third argument to impose a rigorous writing code. The first two obligatory enough. Turning to the statement of our first constant.

Declaration of a Constant

As everyone knows (or almost), water boils at a temperature of 100 ° C (in theory). We will declare a first constant BOILING_WATER_TEMPERATURE name and contains a numerical value information 100.

Declaration of a Constant

<?php
       // Declare the constant
      define ('BOILING_WATER_TEMPERATURE', 100);
      // Display the value
      echo 'Water boils at ' , BOILING_WATER_TEMPERATURE , ' °C ';
?>

If we test this script, we see that the string “Water boils at 100°C” is displayed correctly on the screen, which proves that the constant has been declared and initialized to the correct value .

Notes:

[1] In the statement, if the value is a number, it is registered as the function.
[2] To read the value of a constant, simply call him by name.

From a chemical point of view, water is also a molecule consisting of two hydrogen atoms which are associated with an oxygen atom. Its chemical formula is thus written H2O. We will therefore create a new constant name FORMULA_WATER H2O representing a value chain of characters.

Declaring a new constant

<?php
     // Declare the constant
     define ('FORMULA_WATER', 'H2O');
     // Display the value
    echo 'Chemical formula\'s of water : ', FORMULA_WATER;
?>

Note: the statement, if the value is a string, you must surround it with single or double quotes.

Redefinition of a constant

The value of a constant can not be redefined! Syntax errors will be returned in case of constant redefinition attempt or assigning a new value. The following two scripts have these two cases respectively.

Errors generated in the event of redefinition of a constant

•[Generated error] Notice: Constant BOILING_WATER_TEMPERATURE already defined in /Applications/MAMP/htdocs/PHP-Code/constant.php on line 6
•[Generated error] Parse error: syntax error, unexpected '=' in /Applications/MAMP/htdocs/PHP-Code/xml.php on line 6

Note the = sign for assigning a value to a variable, not a constant.

The predefined constants

It exists in many PHP predefined constants, which can include used in functions as parameters for defining options. We can not mention them all as they are numerous, but we will define them As our needs.
The table defines some useful constants to know. If, out of curiosity, you want view all existing constants, you can write the following code:

<?php
   print_r (get_defined_constants ());
?>

You get an impressive list, including many of the values are integers.

Some predefined constants

PHP_VERSIONPHP version installed on server
PHP_OSName of the server operating system
DEFAULT_INCLUDE_PATHPath to default file
__FILE__Name of running file
__LINE__Line number running

You’ll also like:

  1. How many Types of Comments are there in PHP
  2. PHP Variable Types
  3. PHP Operator Types
  4. Constants – Type of Constants in C++
  5. What are Constants
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

PHP Tutorials

PHP Tutorials

  • PHP - Home
  • PHP - Features
  • PHP - Magic Methods
  • PHP - Imagefilter
  • PHP - Arrays Numeric
  • PHP - Sessions
  • PHP - Forms Processing
  • PHP - clone()
  • PHP - Cookies
  • PHP - Variable Types
  • PHP - First program
  • PHP - call()
  • PHP - Iterator interface
  • PHP - Imports files
  • PHP - Exception Handling
  • PHP - set() and get()
  • PHP - Install MAMP
  • PHP - Functions
  • PHP - Constants Types
  • PHP - Comments Types
  • PHP - OOP's
  • PHP - OOps Use
  • PHP - PHP Code & Redirect 301
  • PHP - Control structures
  • PHP - Abstract Classes
  • PHP - Control structures
  • PHP - Classes
  • PHP - MySQL NULL values
  • PHP - Methods Visibility
  • PHP - Operator Types
  • PHP - Short tags Not use
  • PHP - Object and class
  • PHP - Secure Passwords

Other Links

  • PHP - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW