Constants Types in PHP

A simple value can be referred to by its name, which is known as a constant. During the course of the script\’s execution, a constant value will not undergo any changes. Case sensitivity is turned on for a constant by default. Constant identifiers are usually written in uppercase because this is the practise. The first character of a constant name can be a letter or an underscore, and it is then followed by any combination of letters, digits, and underscores. When a constant has been defined, it cannot subsequently be altered or undefined under any circumstances.

To define a constant, you will need to use the define() function, and to retrieve the value of a constant, you will only need to supply its name. Using define() will allow you to define a constant. In contrast to variables, it is not necessary to preface a constant with a dollar sign. If you want to retrieve the name of the constant in a dynamic manner, you may also use the function constant() to read a constant\’s value if you want to do so.

This function will, just as its name suggests, return the value that has been set for the constant.

When you want to obtain the value of a constant but you do not know its name, for example because it is stored in a variable or returned by a function, you can leverage this to your advantage.

constant() example

<?php
define(\”MINSIZE\”, 50);

echo MINSIZE;
echo constant(\”MINSIZE\”); // same thing as the previous line
?>

Constants are only able to store scalar data, which includes boolean, integer, floating point, and string values.

The main Differences between constants and variables are as follows:

Before writing the value of a constant, you do not have to put the dollar sign ($), but before writing the value of a variable, you must use the dollar sign.

It is not possible to define a constant by simply assigning a value to it; rather, the define() function is required in order to do so.

Without respect to the rules that govern the scope of variables, constants can be defined and retrieved anywhere.

After they have been established as constants, they cannot be redefined or undefined again.

 

Valid and Invalid names for constants

// Valid constant names
define(\”PERSON\”, \”first value\”);
define(\”MEN\”, \”second value\”);
define(\”WOMEN_3\”, \”third value\”);
define(\”__OTHER__\”, \”third value\”);

// Invalid constant names
define(\”2PERSON\”, \”second value\”);

PHP Magic constants

Any script that PHP is used to run receives access to PHP\’s extensive library of predefined constants.

There are five magical constants, each of which takes on a different form depending on the context in which it is utilised. For instance, the value of the __LINE__ variable changes depending on the line in your script where it is utilised. These special constants do not differentiate between upper and lower case and are as follows:

The following are examples of a few \”magical\” PHP constants:

  1. __LINE__
    The line number that is currently being used in the file.
  2. __FILE__
    The complete directory path as well as the filename of the file. If this expression is used inside of an include, the name of the file that is being included is returned. Since version 4 of PHP, the __FILE__ variable has consistently been set to hold an absolute path, although in earlier versions of the language it occasionally held a relative path.
  3. __FUNCTION__
    The name of the function. (Added inversion 4 of PHP ) Beginning with PHP version 5, this constant returns the function name exactly as it was declared (case-sensitive). In PHP 4, the value of this variable is never capitalised.
  4. __CLASS__
    The name of the class. (New in version 4 of PHP) This constant, available since PHP 5, returns the class name exactly as it was declared (case-sensitive). In PHP 4, the value of this variable is never capitalised.
  5. __METHOD__
    The name of the class method. (New in version 5.0.0 of PHP) The name of the method is given back exactly how it was declared (case-sensitive).
Related Posts
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

Hypertext Preprocessor (PHP) is a programming language that lets web developers make dynamic content that works with databases. PHP is Read more

Introduction of PHP

PHP started out as a small open source project that grew as more and more people found out how useful Read more

Syntax Overview of PHP

This chapter will show you some of PHP\'s very basic syntax, which is very important for building a strong PHP Read more

Environment Setup in PHP

To develop and run PHP on your computer, you need to instal three important parts. Web server PHP can almost Read more

Variable Types in PHP

Using a variable is the main way to store information in the middle of a PHP program. Here are the Read more

Scroll to Top