Pass PHP variable by reference, twice

Having a little trouble wrapping my mind around a way to do this. I have a config.php file which declares what certain string variables are. For example, $string1_title = "Title of page";

Now on my index.php i'm trying to get the contents of $string1 by using $string1 = $GET_['string1'];

Now what I want to do either by using a function or just some simple code is append "_title" to the end of $string 1 and then output the contents of the config.php variable.

What I have so far (but obviously doesn't work) is:

$string1 = $GET_['string1'];
$temp_string = &$string1."_title";

So idealy, what I would like is something like:

$temp_string = &($string1."_title"(;

Is there any way to implement this? Just let me know if i'm going about this wrong too, i'm open to anything.
LVL 5
bswinnertonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

john-formbyCommented:
Hi,

You need to include the config.php page in your index.php page.

Something like the example below.

Hope this helps,

John
config.php
==========
<?php
$string1_title = "Title of page";
?>


index.php
=========
<?php
include('config.php');
echo $string1_title;
?>

Open in new window

0
HackneyCabCommented:
Is there a reason why you need to use a reference rather than simply do the following:

$temp_string = $string1."_title";

If so, explain why you need $temp_string to point to the same value (in memory) as $string1.
0
bswinnertonAuthor Commented:
@john-formby, you seem to have forgotton that I am trying to get the $string1 from the browser
          e.g. http://www.mysite.com/index.php?string1=title
and then append the string: "_title" after $string1

@HackneyCab, the reason is because my code actually does a few things after. Perhaps I didn't use the best example above. What I'm actually doing is getting the area in which a blog post is going to display the title. So I'm trying to browse to http://www.mysite.com/index.php?area=Movies. So in config.php I have $Movies_title = "This is a site about movies". Now right after this code that I'm trying to figure out I set the title equal to the variable that is in config.php (which is why it has to pass by reference).
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

john-formbyCommented:
So what do you actually want to do with the value from $string1 = $GET_['string1'];?

This is what I get from your question:

config.php
=======
$string1_title = "Title of page";


index.php
=======
$string1 = $GET_['string1'];

then change $string1 to $string1_title and echo $string1_title from config.php

I am obviously missing something because it doesn't make sense to me at the moment.  It looks like you GET a value and then discard it but that can't be correct.
0
bswinnertonAuthor Commented:
@john-formby What are you confused about? You just explained it perfectly.

Let me see if I can explain it a little better:

In config.php, $movies_title is declared as a string
  $movies_title = "This is the movies page";
==============================================================
You then get area from the URL index.php (http://www.mysite.com/index.php?area=movies):
  $area = $GET_['area'];
  $area = movies
You then append the string "_title" to $area variable:
  $new_area = $area."_title";
  $new_area = movies_title
You then set the title of the page to the $movies_title page which is declared in config.php.

My problem is that if I set the title to $new_area it outputs: movies_title, not the contents of the variable on config.php, and if I set the title to &new_title it thinks i'm referring to the variable that was declared on index.php, not config.php.
0
john-formbyCommented:
That example has made the whole thing click into place :-)

Ok, try this:
page1.php
=========
<a href="page2.php?area=movies">Page 2</a>



config.php
==========
<?php
$movies_title = "Movies";
?>



page2.php
=========
<?php
include('config.php');
echo eval('return $'.$_GET['area'].'_title;');
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
In your config file you have the following:



In your config file:

$movies_title = "This is the movies page";

--------------

Your PHP page then includes the query string ?area=movies.
To refer to the variable called $movies_title simply echo it like this.

echo ${$_GET['area'] . '_title'}; //this will echo the var called $moves_title.

If you query string contains ?area=music then

echo ${$_GET['area'] . '_title'}; //will try to echo a variable called $music_title

Open in new window

0
bswinnertonAuthor Commented:
Both of the above worked for me. But it only works when I do it in header.php. Right now in my header.php I have <title>$current_title</title>. $current_title is defined in the beginning of every page, and I would like to set it to whatever the area variable in the URL says. I can't seem to figure out a way to store either of your solutions into the variable. Is it possible?
0
Chris StanyonWebDevCommented:
How about this:

$current_title = $
{
$_GET
[
'area'
]
 
.
 
'_title'
};

Open in new window

0
Chris StanyonWebDevCommented:
Not sure what happened there.

How about this:



$current_title = ${$_GET['area']  . '_title'};

Open in new window

0
bswinnertonAuthor Commented:
I had to add an apersand before the code, but my final result was:

$current_title = &${$_GET['area'].'_title'};

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.