Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,442 answers

573 users

How to create an enumeration of constants with and without explicit values in PHP

1 Answer

0 votes
/* 
   Title: Enumeration of Constants in PHP
   Example with and without explicit values 
*/

// -------------------------------
// Enum WITHOUT explicit values
// PHP automatically assigns no numeric values,
// but you can still use them as named constants.
// -------------------------------
enum Color {
    case Red;     // ordinal-like position 0
    case Green;   // 1
    case Blue;    // 2
}

// -------------------------------
// Enum WITH explicit values
// Backed enums allow assigning string or int values.
// -------------------------------
enum Status: int {
    case OK = 1;
    case Warning = 5;
    case Error = 6;
    case Critical = 10;
}

echo "Enum without explicit values:\n";
echo "Red = " . Color::Red->name . "\n";
echo "Green = " . Color::Green->name . "\n";
echo "Blue = " . Color::Blue->name . "\n";

echo "\nEnum with explicit values:\n";
echo "OK = " . Status::OK->value . "\n";
echo "Warning = " . Status::Warning->value . "\n";
echo "Error = " . Status::Error->value . "\n";
echo "Critical = " . Status::Critical->value . "\n";


/* 
run:

Enum without explicit values:
Red = Red
Green = Green
Blue = Blue

Enum with explicit values:
OK = 1
Warning = 5
Error = 6
Critical = 10

*/

 



answered Apr 25 by avibootz

Related questions

...