PHPackages                             loganlinn/constantsarray - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. loganlinn/constantsarray

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

loganlinn/constantsarray
========================

A simple array-like PHP class for using constants in strings

28PHP

Since Oct 1Pushed 13y ago1 watchersCompare

[ Source](https://github.com/loganlinn/ConstantsArray)[ Packagist](https://packagist.org/packages/loganlinn/constantsarray)[ RSS](/packages/loganlinn-constantsarray/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

ConstantsArray
==============

[](#constantsarray)

A simple array-like PHP class for using constants in strings
------------------------------------------------------------

[](#a-simple-array-like-php-class-for-using-constants-in-strings)

In PHP, if you want to use a constant in a string, you have a few options.

You could use the concatenation operator:

```
define('COLOR_RED',    0);
define('COLOR_GREEN',  1);
define('COLOR_BLUE',   2);

echo "SELECT * FROM shirts WHERE color IN (".COLOR_RED.",".COLOR_GREEN.",".COLOR_BLUE.")";
```

or use good ol' `sprintf()`

```
define('COLOR_RED',    0);
define('COLOR_GREEN',  1);
define('COLOR_BLUE',   2);

echo sprintf(
	'SELECT * FROM shirts WHERE color IN (%d, %d, %d)',
	COLOR_RED,
	COLOR_GREEN,
	COLOR_BLUE
)
SQL;
```

or you even assign the constants to a variable:

```
define('COLOR_RED',    0);
define('COLOR_GREEN',  1);
define('COLOR_BLUE',   2);

$COLOR_RED   = COLOR_RED;
$COLOR_GREEN = COLOR_GREEN;
$COLOR_BLUE  = COLOR_BLUE;

echo
