What is the best way to define a global constant in PHP which is available in all files?
Asked Answered
L

8

31

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?

Lotson answered 21/5, 2012 at 19:30 Comment(0)
A
35

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');
Airplane answered 21/5, 2012 at 19:33 Comment(1)
I find this short approach more readable: defined('CONST_NAME') || define('CONST_NAME', 'value');Vani
H
12

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");
Hydroponics answered 21/5, 2012 at 19:34 Comment(0)
W
4

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)

Whitehurst answered 30/8, 2016 at 5:28 Comment(0)
R
1

check this

<?php
//Global scope variables
$a = 1;
$b = 2;

 function Sum()
 {
global $a, $b;

$b = $a + $b;
 } 

Sum();
echo $b;
?>
Rothrock answered 21/5, 2012 at 19:42 Comment(0)
R
1

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");
Rolanderolando answered 15/4, 2016 at 17:47 Comment(0)
S
1

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;
Stark answered 23/4, 2017 at 19:52 Comment(0)
S
0

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.

Sarajane answered 21/5, 2012 at 19:38 Comment(0)
H
0
defined("FOO") ?? define("FOO", "BAR");

Try this

Howenstein answered 8/1 at 3:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.