Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
constant
communitycreator

What is the get_defined_constants method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The get_defined_constants method is used to get the:

  • User-defined constants.
  • Pre-defined constants.
  • Constants created by extensions.

Syntax

get_defined_constants(bool $categorize = false): array
  • The categorize parameter can be used to display in which category the constant is defined.
  • The categorize parameter value is false by default.
  • When this is set as true, we will get a multidimensional array with category as key and constants as values.

Code

<?php
define("TEN", 10);
define("HUNDRED", 100);
echo "Getting user deinfed constants \n";
print_r(get_defined_constants(true)['user']);
echo "\n Getting all constants without category \n";
print_r(get_defined_constants());
echo "\n Getting all constants with category \n";
print_r(get_defined_constants(true));
?>

Explanation

In the code above:

  • We have defined the two constants, TEN and HUNDRED.

  • We have called the get_defined_constants method with categorize argument as true, and accessed the user-defined constants with the key user.

get_defined_constants(true)['user']
  • We then printed all the constants without a category by setting the categorize argument to false.

  • We then printed all the constants with a category by setting the categorize argument to true.

RELATED TAGS

php
constant
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring