[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

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.
0
bswinnerton
Asked:
bswinnerton
  • 4
  • 3
  • 3
  • +1
2 Solutions
 
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
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
 
Chris StanyonCommented:
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 StanyonCommented:
How about this:

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

Open in new window

0
 
Chris StanyonCommented:
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now