How can global variables be used to access constants in PHP?
Global variables can be used to access constants in PHP by defining the constants outside of any function or class, making them globally accessible. To access these constants within a function or method, you can use the `global` keyword followed by the constant name to reference the global variable holding the constant value.
define('PI', 3.14);
function calculateCircleArea($radius) {
global $PI;
return $PI * $radius * $radius;
}
echo calculateCircleArea(5); // Output: 78.5
Keywords
Related Questions
- Are there any specific functions or techniques in PHP that can help optimize script execution time?
- How can PHP developers optimize their code to improve readability and maintainability, especially when dealing with complex form structures?
- How can a daemon be implemented to continuously update a variable in the background and make it accessible to clients via AJAX?