CodeMonkey
CodeMonkey

Reputation: 2285

PHP define not setting variable value

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 ?

Upvotes: 1

Views: 86

Answers (2)

DannyTheDev
DannyTheDev

Reputation: 4173

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

Upvotes: 1

Manuel Arwed Schmidt
Manuel Arwed Schmidt

Reputation: 3586

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.

Upvotes: 1

Related Questions