Jump to content

[SOLVED] Variables in Variables


kirk112

Recommended Posts

Hi

 

Thought this was going to be straight forward - (could be having a mental block);

 

From a MySQL query I have:

 

$row['page_title'] which =  "Page Title - $row_jobs['job_title']";

 

if i echo $row['page_title']

 

it prints out 'Page Title - $row_jobs['job_title']'

 

instead of 'Page Title - store manger'

 

What am I missing???

 

Thanks for your help!!!

 

 

Link to comment
Share on other sites

i don't know how liamproduction's reply will actually help you (i myself have no idea what he's trying to say).  if you've stored the variable as that string, you'll need to evaluate it in order to have the variables interpolated as you desire.  from the PHP manual for eval():

 

<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?>

 

The above example will output:

 

This is a $string with my $name in it.

This is a cup with my coffee in it.

 

in this case, $row['page_title'] is the $str value.  you may hit parse errors, since you usually need to encase array values in braces {} in order to have them interpolated properly.

Link to comment
Share on other sites

to do this without using the "dot" operator to concatenate, you can do this:

$row['page_title'] =  "Page Title - {$row_jobs['job_title']}";

The curly braces can be used around variables to show that "this whole thing" is the variable.

$variableName="Hello";
$test[$variableName]="World";
echo "$variableName {$test['Hello']}";
//should output "Hello World"

 

Link to comment
Share on other sites

He said the last bit of his variable was'nt printing so i fixed it

 

$row['page_title'] which =  "Page Title -" . "$row_jobs['job_title']";

 

the word which will toss a parse error, and "$row_jobs['job_title']" will also toss a parse error.

Link to comment
Share on other sites

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.