1

I am working on a PHP script which I was cleaning up after the code was working. To make code more readable and easy to fix for future, I tried to move all the regularly used variables to constants.

One such variable value was "http://api.io/url/" which I moved to define and assigned to URL_PREFIX

define("URL_PREFIX" , "http://api.io/url/");
define("FILE_PREFIX" , "http://api.io/files/");

This script worked with the changes. However it started failing suddenly, and on debugging I realized, the place where I am calling URL_PREFIX was assigning the value as "URL_PREFIX" instead of "http://api.io/url/"

$apiUrl = URL_PREFIX . "" . $scope;   **[FAILS]**
$apiUrl = "http://api.io/url/" . "" . $scope; **[WORKS]**

Am I missing something up here ?

13
  • Where do you define the constants and where do you use them? Make sure its either in the same file or include the definition file in all other scripts that you want to use the constants in. Commented Feb 21, 2015 at 9:54
  • 1
    PHP will convert URL_PREFIX to the string 'URL_PREFIX' if it's not defined. So that's what's going on. We'd need to know the code path in between your define and the place it's now not defined to know what's going wrong, but a missing require or something would normally be the problem. Commented Feb 21, 2015 at 9:54
  • make sure define() was called before that Commented Feb 21, 2015 at 9:55
  • 1
    What happens if you echo URL_PREFIX just after the define? Does it work? What about if you do it just before the $apiURL = ...? Does it still work? If the answer to these two questions is "yes" and "no" respectively, your problem must be in the code you're not showing us. Commented Feb 21, 2015 at 9:56
  • 1
    @CᴴᵁᴮᴮʸNᴵᴺᴶᴬ meanwhile i admit.. it was a way silly mistake :( Commented Feb 21, 2015 at 10:03

2 Answers 2

1

Make sure you place any defines at the top of your PHP file, basically make sure you define them before you request them.

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

3 Comments

to clarify: at the top of EVERY PHP file that uses that defined constant
Yes unless this file is used as an include.
(an include is included in the file)
1

Set your defines within the very first lines of code (outside of any if's and so on), to make sure they will execute no matter what.

2 Comments

Thanks... Manuel .. wish i had a way to select your answer as well. Upvoted it though :)
It only matters that we could help you. :)

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.