8

I have a config file that I include early in my program and set this

define('BASE_SLUG','/shop');

I include another file later with these lines

echo BASE_SLUG;
if (defined(BASE_SLUG)) {
  echo ' - yes';
} else {
  echo ' - no';
}

And my output is

/shop - no

how is this possible? BASE_SLUG has the value of /shop and I can echo it, but one line later it says it is not defined

1
  • if (defined(BASE_SLUG)) = if (defined('/shop')) which is not defined. Commented Jul 18, 2014 at 4:20

1 Answer 1

24

Here is the function prototype for defined

bool defined ( string $name )

You can see that it expects a string value for a constant name. Yours isn't a valid string.

if (defined(BASE_SLUG)) {

Should have the name of constant inside quotes, like :

if (defined('BASE_SLUG')) {

Read this note from PHP Manual

<?php
/* Note the use of quotes, this is important.  This example is checking
 * if the string 'TEST' is the name of a constant named TEST */
if (defined('TEST')) {
    echo TEST;
}
?> 

Source

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.