Constants in PHP

Constants in PHP are defined with the function define(), which requires that we place two elements separated by a comma: the name of the constant and its value.

Constants in PHP | Learn PHP | Constants in PHP are defined with the function define(), which requires that we place two elements separated by a comma: the name of the constant and its value

Constants in PHP: few data that we will not change. | A variable that we are not going to modify

Sometimes, we will need that the value that we store in a variable does not change even if we make an error.

That is, we need the stored value to remain identical, constant, until the PHP interpreter finishes processing our page.

In those cases, instead of a variable we would use a constant. Unlike variables, it is impossible to define a value through an assignment operator (the equal sign), which makes it easier for us to change its value "even by mistake" during its entire useful life, since they will always store the same value.

The constants in PHP are defined with the function define (), which requires that we place two elements separated by a comma: the name of the constant and its value.

Let's see an example of its syntax:

<?php
define("PI", 3.1415926);
define("BR", "<br />");
define("LIBRO", "PHP 6");
print(PI);
print(BR);
print(LIBRO);
?>

Very important: at the time of showing Constants in PHP (with echo or print), the constants in PHP do not go in quotes (they are not a text string to be printed literally) nor do they carry a sign of weights ahead (they are not a variable) .

In addition, when defining them, their name is usually written totally in CAPITAL LETTERS in order to differentiate them at a glance from the variables and from the literal texts. Recall how important it is to facilitate the reading of our code.

We will use Constants in PHP very often in our projects, to store connection data based on data, users and passwords, and any other information that we need to store without risk of being modified elsewhere in our code.

Includes in Constants in PHP

It’s a very good idea to define all the PHP Constants of a Web site in a separate file, and then include that file (using include or require) in all the pages that need to use those PHP Constants. It is a technique widely used in multilingual sites, which define PHP constants for texts and use a file to define those texts in each language.

Let's see an example of this technique: imagine a website with a Spanish and English version. There will be two files that will store Constants in PHP, one of the texts in Spanish and another with texts in English. For example, within the languages folder we will create a file called english.php that will contain only this:

<?php
define('NAVBAR_TITLE', 'My Account');
define('HEADING_TITLE', 'My Account Information');
define('OVERVIEW_TITLE', 'Overview');
define('OVERVIEW_SHOW_ALL_ORDERS', '(show all orders)');
define(OVERVIEW_PREVIOUS_ORDERS', 'Previous Orders');
?>

And, within another file called spanish.php we will place the translation into Spanish of those same Constants in PHP:

<?php
define('NAVBAR_TITLE', 'Mi cuenta');
define('HEADING_TITLE', 'Datos de mi cuenta');
define('OVERVIEW_TITLE', 'Resumen');
define('OVERVIEW_SHOW_ALL_ORDERS', '(Ver todos mis pedidos)');
define(OVERVIEW_PREVIOUS_ORDERS', 'Pedidos anteriores');
?>

In this way, by indicating in each page the language that will be displayed (typically with a session variable), we can include the text file corresponding to the language chosen by the user.

For example, if we prepare a page called account.php that must show those texts in Spanish, your code will be similar to this:

<!DOCTYPE html>
<html>
<?php include ('lenguajes/castellano.php'); ?>
<head>
<title><?php print (NAV_TITLE); ?></title>
</head>
<body>
<h1><?php print (HEADIN_TITLE); ?></h1>
<h2><?php print (OVERVIEW_TITLE); ?></h2>
<ul>
<li><a href="show_all_orders.php"><?php print (OVERVIEW_SHOW_ALL_ORDERS); ?></a></li>
<li><a href="show_previous_orders.php"><?php print (OVERVIEW_PREVIOUS_ORDERS); ?></a></li>
</ul>
</body>
</html>

Note that we have not included PHP constants within blocks of text delimited with single or double quotes, as we do when we want to assign a value to a variable, since this would not work. Nor would it work to place them inside an echo or print along with a text wrapped in quotation marks.

Let's see why:

<!DOCTYPE html>
<html>
<head>
<title>PHP constants</title>
</head>
<body>
<h1><?php print ("Welcome to HEADING_TITLE"); ?></h1>
<!-- It will not be replaced by its value, the text HEADING_TITLE will be written as is -->
<h2><?php print ('Hello OVERVIEW_TITLE'); ?></h2>
<!-- Neither will it be replaced by its value, the text OVERVIEW_TITLE will be written as is -->
<?php
$doubleQuotes="Text in double quotes, can not contain PHP constants as HEADING_TITLE because they will not be replaced by their value";
#singlequotes='Text in single quotes, can not contain PHP constants as HEADING_TITLE because they will not be replaced by their value';
?>
<!-- Constants in PHP will not be replaced by their value -->
</body>
</html>

The reason is that, within those text blocks, since the name of the constant is not identified with any special sign (like the $ sign used to indicate that something is a variable), the PHP interpreter has no way of detect that it is a constant and not a common word that simply has to write letter by letter. To remedy this limitation, we will use the technique called concatenation.

For example:

$concatenated="Text concatenated with a constant '.HEADING_TITLE.' that now will be replaced by its value";

So whenever we insert Constants in PHP, we must concatenate them.

Did you like it or was it useful?

Help us share it in Social Networks

Professor at the University of Guadalajara

Hugo Delgado Desarrollador y Diseñador Web en Puerto Vallarta

Professional in Web Development and SEO Positioning for more than 10 continuous years.
We have more than 200 certificates and recognitions in the Academic and Professional trajectory, including diploma certificates certified by Google.

IT ALSO DESERVES TO PAY TO VISIT:

Not finding what you need?

Use our internal search to discover more information
Sponsored content:
 

Leave your Comment

SPONSOR

Your business can also appear here. More information