Monthly Archives: January 2016

Convert a constant value to its string name in PHP

I ran into a case where I had to get the string representation of a constant based only on its value, so a little digging around and some experimentation produced this: const MY_CONSTANT = 1; // get constant name by value $class = new ReflectionClass(‘MyClassName’); $constants = array_flip($class->getConstants()); echo $constants[1]; // output is “MY_CONSTANT” If […]