Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

EDIT: SOLVED


ORIGINAL POST:

I have a foreach loop which creates variables in the form of:

    foreach ($fieldsarray as $field)
    {
        ${$field} = $globalrow[$field];
    }

This creates variables using array elements, like $firstname, $lastname, etc.

Now, since the values of these variables are fetched from the DB, I cannot use the variable names again in the same foreach. What I would like to do is to use this in the loop:

if(!empty($$field)) 
  {
    $r.$field='<tr><td width="50%">'.${$field}._label.': </td>
    <td width="50%">'.${$field}.'</td></tr>';
  }

Basically, this would generate table rows with data from DB, but only if the value of the variable is not empty.

My problem is: Since I have declared above that e.g. $firstname = 'John' - then the second part of the code does not work anymore, because I assume PHP does not transform the above snippet into

if(!empty($firstname))

but rather into

if(!empty(John))

How can I "force" PHP to again use the variable NAME rather than its value in this case?

Thank you for your help.

EDIT: OK, cheezeburgers suggestions worked and I think now it's working as intended. Thanks to all of you for your help.

EDIT 2: The table rows are output correctly on the screen, e.g.:

First Name: John
Last Name : Smith

but for some weird reason in the email confirmation the output is doubled:

First Name: John
Last Name : Smith
First Name: John
Last Name : Smith

In both cases I simply echo them with:

<?php echo $allrows ?>

where $allrows is in the foreach loop.

(I will post this part as a new question since this seems to be a new problem now)

share|improve this question
3  
Urgh, that gives me a headache, variable variables are not easy to read. What are you actually trying to achieve? Im positive there is a much better way –  user574632 Apr 24 at 11:17
1  
Why do you need the variable variables? Can't you use the array for table generation? (hint: you can) –  kapa Apr 24 at 11:17
1  
Why don't you just use if (!empty($globalrow[$field]))? –  Barmar Apr 24 at 11:17
    
But I think it should work as you wrote it. PHP does transform empty($$field) into empty($firstname) when $field = 'firstname'. –  Barmar Apr 24 at 11:18
    
Why on earth do you want to do this? You already have a variable (the array) with all the info you need. This makes no sense to me –  PeeHaa Apr 24 at 11:29
show 3 more comments

2 Answers

up vote 1 down vote accepted

PHP will work it out fine, just that you have missed the quote around _label and probably that's being read as a constant.

Try this:

$$field='<tr><td width="50%">'.${$field}.'_label: </td>
<td width="50%">'.${$field}.'</td></tr>'; 

which gives the output as:

John_label: John

Demo

share|improve this answer
    
Exactly, but it should not be John_label: John -> I need it to produce $firstname_label: John - because I am declaring the variable $firstname_label in other part of code as $firstname_label = 'First Name'. Do you see my problem now? –  Al-Iskander Al-Albani Apr 24 at 11:28
    
@Al-IskanderAl-Albani You can do that by simply changing ${$field} to $field. –  I Can Has Kittenz Apr 24 at 11:30
    
OK changed that, but its still not outputting anything, because as I said, I think the problem is here: if(!empty($$field)) –  Al-Iskander Al-Albani Apr 24 at 11:35
    
@Al-IskanderAl-Albani Can you paste the values of $field and $$field? Echo them right before the if. –  I Can Has Kittenz Apr 24 at 11:37
    
i can echo them, and they output correctly as "firstname" and "John", but the Problem remains with the label thingy. I can either have "firstname_label" or "John_label" - neither will produce anything of course, because the variable is named above as $firstname_label. I can mix it with the string and visually produce "$firstname_label" - but thats just it, it does not get interpreted, its simply output as it is. –  Al-Iskander Al-Albani Apr 24 at 11:49
show 1 more comment

I would do that differently altogether, but to answer your question, try this:

extract($globalrow);

It creates variables in the current scope for every entry in the array going by the name of the respective key. So as long as there are no numeric keys, this should work instead of ${$field} = $globalrow[$field];. Then just refer to the variables by actual name.

http://php.net/extract

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.