Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/137399-solved-constants-w-variable-inside/
Share on other sites

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.

 

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);

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.