What is the best way to define a constant that is global, i.e., available in all PHP files?
UPDATE: What are the differences between constants and class constants? Can they be used interchangeably?
What is the best way to define a constant that is global, i.e., available in all PHP files?
UPDATE: What are the differences between constants and class constants? Can they be used interchangeably?
Define your constant in your top .php file, that will be included in all the other scripts. It may be your front controller, your config file, or a file created for this single purpose.
!defined('MY_CONST') && define('MY_CONST', 'hello');
do the following:
define("CONSTANT_NAME","value");
if you want to access this constant from other php files, you have to include it:
include_once("thefilewiththeconstant.php");
When you want to use global constants across PHP files, I feel a good practice is to use Object Oriented principles and define these in relevant classes. Using the spl_autoload_register() function built into PHP, you can define all your later classes to extend the related "globals class" without worrying about including files manually everywhere. All the later child classes of that globals class will always have access to the related global variables. (I say related because it's good design to have global constants in a class be related in some way, rather than everything grouped together with no rhyme or reason)
check this
<?php
//Global scope variables
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
A better way is to check if the constant is assigned to anything and if it's not assigned to anything then assign it, else assign it to NULL and then assign it to your constant
defined("FOO") ? null : define("FOO", "BAR");
It depends. In your situation I would go for define()
because it has a more compact syntax in usage. But define()
can only hold scalar values in PHP < 7.0 In case you need i.e. an associative array you have no other choice then to go for $GLOBALS
or use PHP >=7.0.
// Storing a single value works fine with define
define( 'ROOT_DIR', dirname(dirname(__FILE__)) . '/' );
// But not for complex data types like this array
$USERPIC_PARAMS = array(
"user_root" => "images/users",
"padding_length" => 8,
"split_length" => 4,
"hash_length" => 12,
"hide_leftover" => false
);
// Then you need $GLOBALS
$GLOBALS['USERPIC_PARAMS'] = $USERPIC_PARAMS;
// Or in PHP >=7.0
define( 'USERPIC_PARAMS', $USERPIC_PARAMS );
// output your define
echo ROOT_DIR;
// output your $GLOBALS var
echo $GLOBALS['USERPIC_PARAMS'];
// output in PHP >=7.0 your constant
echo USERPIC_PARAMS;
Whichever file you create your constant(s) in, you will have to require/include it in the files you will be using them.
Otherwise you can use the $_SESSION global but that requires session initialization by session_start()
at the beginning of your code in each of those files.
© 2022 - 2024 — McMap. All rights reserved.
defined('CONST_NAME') || define('CONST_NAME', 'value');
– Vani