Jump to content

define varibale


Destramic

Recommended Posts

The brackets {} are only used within a double quoted string (or hredoc method) to have a variable interpreted. One of the following methods would work (I'd go with the first)

echo $var._VAR;

echo "{$var}"._VAR;

 

EDIT: Scratch that. I misread what you were trying to do. You are defining a constant and then trying to access that constant dynamically. I don't know of a way to do that. You could instead define the TEST_VAR as an array

$config = array();
$config['TEST_VAR'] = 'hello';
$var = "TEST";
echo $config["{$var}_VAR"];

Link to comment
https://forums.phpfreaks.com/topic/224131-define-varibale/#findComment-1158122
Share on other sites

OK, a quick search shows a solution using constants

define ('TEST_VAR', 'hello');
$var = "TEST";
echo constant("{$var}_VAR");
//Output: hello

 

thanks for that. good to know. i was stumped.

 

Yeah I had no idea either. But, it was right there in the manual for constants! Who would have thought to look there?

Link to comment
https://forums.phpfreaks.com/topic/224131-define-varibale/#findComment-1158143
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.