nogginj Posted December 17, 2008 Share Posted December 17, 2008 howdy i love constants because i can set up a bunch of them in one file, and so makes editing my code to fit other circumstances later wonderful. however, i have one question. is there a way to define a constant with variable included in it? example: i have constants.php and in there i have things like define('MESSAGE', 'howdy hows it goin'). in another file, mailer.php, i have a function that uses this constant, for example, $email = MESSAGE would set $email equal to 'howdy hows it goin'. great, but, say for instance i want the constant to change based on another variable in mailer.php, ie $recipient. the desired behavior would be for $email to then become 'howdy '.$recipient.' hows it goin'. if i define the constant as such, define('MESSAGE', 'howdy '.$recipient.' hows it goin'), it evaluates $recipient from within constants.php. how can i define something similar to a constant that is not evaluated until it is placed within another file? this way i can easily switch my message from 'howdy'.$recipient.'hows it goin' to something else like 'bonjour'.$recipient.'comment ca va'. does that make sense? i have considered using includes but then i have a bunch of include files and that is not necessarily desirable. thanks Quote Link to comment https://forums.phpfreaks.com/topic/137399-solved-constants-w-variable-inside/ Share on other sites More sharing options...
.josh Posted December 17, 2008 Share Posted December 17, 2008 maybe I'm misunderstanding the goal here, so maybe this won't help, but if you assign something to a variable using single quotes, it will be interpreted literally. So for instance: $x = 'one'; $string1 = "the number $x"; $string2 = 'the number $x'; echo $string1; // output: the number one echo $string2; // output: the number $x You can then use eval on $string2 later on to get it to actually interpret $x as whatever. Quote Link to comment https://forums.phpfreaks.com/topic/137399-solved-constants-w-variable-inside/#findComment-717946 Share on other sites More sharing options...
Mark Baker Posted December 17, 2008 Share Posted December 17, 2008 Surely if the constant changes, then it isn't a constant and you should be using a variable. However, define your constant as a string using sprintf values: define('MESSAGE','howdy %s hows it goin'); Then, when you come to send your message, use: $email = sprintf(MESSAGE,$recipient); Quote Link to comment https://forums.phpfreaks.com/topic/137399-solved-constants-w-variable-inside/#findComment-717947 Share on other sites More sharing options...
nogginj Posted December 17, 2008 Author Share Posted December 17, 2008 howdy brilliant. thank you mark. i understand i was no longer talkign about a constant, per se, but i wanted to have it considered as such by someone else reading my code. if we may go further into this sprintf solution, what if i were to change the order of multiple variables? ie i have define('MESSAGE','howdy %s, %s says hi) and then i have in the mailer $email = sprintf(MESSAGE,$recipient,$sender) is there anyway to arrange it so, without changing the code in mailer.php ( $email = sprintf(MESSAGE,$recipient,$sender) ), i could change the message to something like: define('MESSAGE','%s has said hello to you, %s') looking through the manual, i see things like 1$s and 2$s...is this what im after? im trying it now..just wanted to ask in case im wrong. EDIT: i was sort of right...but it takes a % sign to make it work. %1$s is the 1st arg and %2$s is the 2nd arg. Thank you again for the pointer to sprintf. Crayon, ive not noticed that difference in regular and double quotes, but that is totally possible. thank you for the suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/137399-solved-constants-w-variable-inside/#findComment-717968 Share on other sites More sharing options...
.josh Posted December 17, 2008 Share Posted December 17, 2008 once you define a constant, it cannot be changed (or undefined and redefined). Quote Link to comment https://forums.phpfreaks.com/topic/137399-solved-constants-w-variable-inside/#findComment-717975 Share on other sites More sharing options...
nogginj Posted December 17, 2008 Author Share Posted December 17, 2008 thanks again for the info, how can i mark this topic as solved or is that something not up to me? -j EDIT: found the little lurking button. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/137399-solved-constants-w-variable-inside/#findComment-717983 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.