Destramic Posted January 11, 2011 Share Posted January 11, 2011 im having a problem getting this code to work...i know it may not make sence why i would do this but it is for a larger script...is there any reason why it wont echo please? define ('TEST_VAR', 'hello'); $var = "TEST"; echo {$var}_VAR; Quote Link to comment https://forums.phpfreaks.com/topic/224131-define-varibale/ Share on other sites More sharing options...
Psycho Posted January 11, 2011 Share Posted January 11, 2011 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"]; Quote Link to comment https://forums.phpfreaks.com/topic/224131-define-varibale/#findComment-1158122 Share on other sites More sharing options...
BlueSkyIS Posted January 11, 2011 Share Posted January 11, 2011 actually, you can use the brackets to create a dynamically named variable $var = 'hello'; ${$var} = 'world'; echo $hello; output: world but i can't get this to work properly to dynamically define a constant. Quote Link to comment https://forums.phpfreaks.com/topic/224131-define-varibale/#findComment-1158124 Share on other sites More sharing options...
Psycho Posted January 11, 2011 Share Posted January 11, 2011 OK, a quick search shows a solution using constants define ('TEST_VAR', 'hello'); $var = "TEST"; echo constant("{$var}_VAR"); //Output: hello Quote Link to comment https://forums.phpfreaks.com/topic/224131-define-varibale/#findComment-1158129 Share on other sites More sharing options...
Destramic Posted January 12, 2011 Author Share Posted January 12, 2011 thanks guys...wouldnt know what i'd do without you Quote Link to comment https://forums.phpfreaks.com/topic/224131-define-varibale/#findComment-1158136 Share on other sites More sharing options...
BlueSkyIS Posted January 12, 2011 Share Posted January 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224131-define-varibale/#findComment-1158139 Share on other sites More sharing options...
Psycho Posted January 12, 2011 Share Posted January 12, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/224131-define-varibale/#findComment-1158143 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.