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
https://forums.phpfreaks.com/topic/65256-solved-variables-in-variables/
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.

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"

 

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.

Archived

This topic is now archived and is closed to further replies.

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