Katushai
Katushai

Reputation: 1520

Defining PHP constants in header file doesnt work?

I tried to define constants in my config.php file. Its primary use is just configuration, as I'm sure you guessed. But it isn't working. here's my config.php file:

<?php
    define("KPEREQUEST", "0", true);
    define("KPERESPONSE", "1", true);

    define("KPEGET", 0, true);
    define("KPEPOST", 1, true);
?>

and here's the result when I include config.php and print the define kPAPost:

KPEPOST: ""

but here's the result if i define the constants in index.php AND i print them from index.php:

KPEPOST: "1"

** EDIT **

heres my index.php file:

<?php
    $title = "API Documents";
    include 'header.php';
    include "config.inc.php";

    echo "KPEPOST: \"" . KPEPOST . "\""; 
?>

<?php include 'footer.php'; ?>

I've seen this done a million times in wordpress, magento, etc. I have no idea why this isn't working and I've done a ton of research to figure it out but I'm at a dead end now. Any help would be appreciated. Why can't I define a constant in a config file and use the define in files that include the config file?

Upvotes: 2

Views: 704

Answers (2)

Katushai
Katushai

Reputation: 1520

another possible solution is to use ./ in front of the filename during the include. i discovered it inadvertently.

define("kPEPost", "1");

and the include:

include("./header");
include("./includes/config.inc.php");

Upvotes: 0

paulsm4
paulsm4

Reputation: 121809

It sounds like you just don't have config.php (or that particular copy of config.php) in your include path.

Suggestion

  1. Define a root index.php for your entire web app;
  2. Do this:

    define('BASE_DIR', realpath(__FILE__));
    
  3. Then you can do this:

    require_once BASE_DIR.'config.php';
    

Further reading

Upvotes: 1

Related Questions