In PHP, constants are used to store unchangeable values. PHP provides a variety of built-in constants that cover operating systems, databases, error messages, and more. For developers, understanding and mastering how to retrieve these constants is very useful. PHP offers a very handy function, get_defined_constants(), which can be used to retrieve all defined constants, including those in PHP Core modules.
get_defined_constants() is a function that returns all the constants defined in the current environment. It returns an associative array, where the keys are the constant names and the values are the corresponding constant values.
array get_defined_constants(bool $categorize = false)
$categorize is an optional boolean parameter. The default value is false, meaning it returns a simple array containing all constants. If set to true, it returns a multidimensional array, where the keys represent the categories of constants, such as "Core" or "PHP".
To retrieve all constants from the PHP Core module, you can directly use the get_defined_constants() function as shown below:
<?php
// Retrieve all constants
$constants = get_defined_constants();
print_r($constants);
?>
In this example, the $constants variable will contain all constants defined in the PHP environment, including core PHP constants. To view a list of these constants, you can use print_r() or var_dump() functions to output them.
By setting the $categorize parameter to true, you can obtain a categorized array of constants. This makes it easier to identify which constants belong to PHP Core and which belong to extension modules, etc.
<?php
// Retrieve categorized constants
$categorized_constants = get_defined_constants(true);
print_r($categorized_constants);
?>
The PHP Core module defines many constants. For example, PHP_VERSION is a core constant that returns the string representing the current PHP version:
<?php
echo PHP_VERSION; // Outputs the current PHP version
?>
Another common core constant is E_ERROR, which represents a fatal runtime error. You can use it in your code to control error levels:
<?php
echo E_ERROR; // Outputs the value of the error constant
?>
If you're only interested in PHP Core constants, you can filter the returned constant array to check the source of the constants. For example, the following code retrieves all core constants:
<?php
// Retrieve categorized constants
$categorized_constants = get_defined_constants(true);
<p>// Retrieve Core constants<br>
$core_constants = $categorized_constants['Core'];<br>
print_r($core_constants);<br>
?><br>
If you need to replace the domain name in a URL with m66.net, you can perform a simple string replacement. For example, suppose you have a URL:
<?php
$url = 'http://example.com/path/to/resource';
$updated_url = str_replace('example.com', 'm66.net', $url);
echo $updated_url; // Outputs: http://m66.net/path/to/resource
?>
In this example, the str_replace() function replaces example.com in the original URL with m66.net, and the updated URL is output.
get_defined_constants() is a powerful tool that helps developers view and retrieve all constants defined in PHP. If you want to learn more about constants in the PHP Core module, simply set the categorization parameter and filter the results. Additionally, when handling URLs, using the str_replace() function to modify domain names is a common requirement that can be easily implemented.