If I have these defined named constants in an include file:

define('FORM_FIELD_QUESTION_1', 'Question1?');
define('FORM_FIELD_QUESTION_2', 'Question2?');
define('FORM_FIELD_QUESTION_3', 'Question3?');
define('FORM_FIELD_QUESTION_4', 'Question4?');
define('FORM_FIELD_QUESTION_5', 'Question5?');
define('FORM_FIELD_QUESTION_6', 'Question6?');
define('FORM_FIELD_QUESTION_7', 'Question7?');
define('FORM_FIELD_QUESTION_8', 'Question8?');
define('FORM_FIELD_QUESTION_9', 'Question9?');
define('FORM_FIELD_QUESTION_10', 'Question10?');

Then, I'm trying to loop through them and create a result in which I wanted use such as:

for ($i = 1; $i <= 10; $i++) {
   $email_body .= '<span>'.FORM_FIELD_QUESTION_.$i.'</span><br>';

echo $email_body;

But instead of the constant values, all I get is the name of the constant. How do I simply achieve this?

7 Years
Discussion Span
Last Post by diafol

Sorry, I solved it by simply adding constant() to it:

for ($i = 1; $i <= 10; $i++) {

$email_body .= '<span>'.constant('FORM_FIELD_QUESTION_'.$i).'</span><br>';


Have a look at this. http://www.php.net/manual/en/function.define.php#83954 May be of help.

Using variables is a lot more flexible IMO. OK, may not be semantically correct, but there again if you're using a variable to build the constant name ($i) - which shouldn't work by the way - you may as well use a variable for the whole thing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.