lordvader Posted March 30, 2008 Share Posted March 30, 2008 Say: define('NAME-1', 'Larry'); define('NAME-2', 'Curly'); define('NAME-3', 'Moe'); How could I append a number to a portion of a string and then use that new thing as a constant and not as a string? Like: take the number 1, append it to the back of NAME- and then now php knows that I want to use constant NAME-1, and returns 'Larry', instead of returning string "NAME-1". TIA Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 30, 2008 Share Posted March 30, 2008 hmmm - I hope you know what constants are and what they are for... The reason being is that you cant reference constants liek variables... define('NAME-'.$i, 'String'); will work but I don't see how its going to be useful as you cant then do anything like echo NAME-.$i; So ultimately this process is pointless. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 If you are setting them dynamically, they are variables, not constants. Use an array. <?php $names = array (1=>'Larry', 'Curly', 'Moe'); $x = 1; echo $names[$x]; // Larry Quote Link to comment Share on other sites More sharing options...
lordvader Posted March 30, 2008 Author Share Posted March 30, 2008 I meant like this: function stooge($num) $temp = 'NAME-' . $num echo $temp --- stooge(1); echoes Larry. (But of course my example func will just echo the phrase NAME-1 ) I want to avoid using an if else tree or a switch, like func stooge($num) if ($num == 1) echo NAME-1 if ($num == 2) echo NAME-2 or func stooge($num) switch ($num) case 1: echo NAME-1 case 2: echo NAME-2 I don't want to change the constant's name after the fact. TIA Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 30, 2008 Share Posted March 30, 2008 like I said - you can ONLY reference constants by their full assigned 'name' you CAN NOT concatenate a string that represents the name of that constant (maybe you can with eval but thats just VERY poor programming in my book). These are NOT constants so don't bother using ing them - just use variables... Quote Link to comment Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 You can, but I don't see the point <?php define("NAME-1", 'Curly'); define('NAME-2', 'Larry'); define('NAME-3', 'Moe'); $stooge = 1; $var = 'NAME-'. $stooge; echo constant($var); //Curly ?> Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 30, 2008 Share Posted March 30, 2008 face egg on NEVER used constant(); only constants I have are configuartion settings for my apps - I know what they are called so never had any call for to reference them dynamically - you learn somethinge new everyday... even if you doubt you'll ever use it Quote Link to comment Share on other sites More sharing options...
Barand Posted March 30, 2008 Share Posted March 30, 2008 Don't worry, I agree with you. lordvader should be using variables. If you are appending numbers to names like that, it should be an array IMO Quote Link to comment Share on other sites More sharing options...
lordvader Posted March 30, 2008 Author Share Posted March 30, 2008 YES! Works perfect! FYI the reason is to compare the processes of script writing; for my project things have been tedious so I wanted to try it this way to see if things become a bit more efficient. Thanks everybody! Quote Link to comment 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.