By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,654 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,654 IT Pros & Developers. It's quick & easy.

Calling one variable as part of another

P: 3
Hi,

I have a problem, i can't work out how to produce a variable with another variable in it.

e.g.
Expand|Select|Wrap|Line Numbers
  1. $username=$_POST['username'];
  2.  
i want a variable ($content) which will then make a statement like "your username is $username"

It must be a variable as this is loaded into another page dynamically.

How to do i write the $content = '????????

Hope someone can help.

I tried using
Expand|Select|Wrap|Line Numbers
  1. $content ='your username is <?php echo $username ?>';
  2.  
but it doesnt work, i assume that was a crazy thing to try.
May 22 '07 #1
Share this Question
Share on Google+
11 Replies


P: 36
Hi,

This is how you should be writing the code.
[PHP]
<?php
$username="Ankit Mathur";
echo $content = "Your username is ". $username;
?>
[/PHP]

Happy Coding.....
Ankit Mathur


Hi,

I have a problem, i can't work out how to produce a variable with another variable in it.

e.g.
Expand|Select|Wrap|Line Numbers
  1. $username=$_POST['username'];
  2.  
i want a variable ($content) which will then make a statement like "your username is $username"

It must be a variable as this is loaded into another page dynamically.

How to do i write the $content = '????????

Hope someone can help.

I tried using
Expand|Select|Wrap|Line Numbers
  1. $content ='your username is <?php echo $username ?>';
  2.  
but it doesnt work, i assume that was a crazy thing to try.
May 22 '07 #2

Atli
Expert 5K+
P: 5,058
Hi.

This can be done in many ways.

Most basic, like ankitmathur mentioned
Expand|Select|Wrap|Line Numbers
  1. echo "My name is ". $name;
  2.  
You can also put it inside the string. PHP will realize you are refering to a variable and exchange it for it's value.
Be carefull not to user single quote marks
they do not evaluate variable names and print the variable name rather than its value.
Expand|Select|Wrap|Line Numbers
  1. echo "My name is $name";
  2.  
You can also put arrays inside the quote marks if you put {} arround them.
Expand|Select|Wrap|Line Numbers
  1. echo "My name is {$_POST['name']}";
  2.  
And if special cases, if all else fails you could replace some marker with the text you want.
Expand|Select|Wrap|Line Numbers
  1. echo str_replace("!-name-!", $name, "Hi my name is !-name-!");
  2.  
Hope this helps
May 22 '07 #3

ak1dnar
Expert 100+
P: 1,584
I think you are trying to pass the $username variable over the pages.

So create a session variable and assign the $username to the session.
Then later on you can execute that value on a different page. until you close the browser window.
May 22 '07 #4

Atli
Expert 5K+
P: 5,058
I think you are trying to pass the $username variable over the pages.

So create a session variable and assign the $username to the session.
Then later on you can execute that value on a different page. until you close the browser window.
If this is the case, this article could help.
May 22 '07 #5

P: 3
The username was an example, perhaps i should explain my full situation.

I get these values from an input form:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $title=$_POST['trackname'];
  3. $author=$_POST['author'];
  4. $region=$_POST['region'];
  5. $version=$_POST['version'];
  6. $website=$_POST['website'];
  7. $stars=$_POST['stars'];
  8. $details=$_POST['details'];
  9. $ch_rl_mgp=$_POST['ch_rl_mgp'];
  10. $ch_rl_sbk=$_POST['ch_rl_sbk'];
  11. $ch_rl_bsb=$_POST['ch_rl_bsb'];
  12. $ch_rl_ama=$_POST['ch_rl_ama'];
  13. $ch_rl_aus=$_POST['ch_rl_aus'];
  14. $ch_rl_jpn=$_POST['ch_rl_jpn'];
  15. $ch_gp_mgp500=$_POST['ch_gp_mgp500'];
  16. $ch_gp_stonl=$_POST['ch_gp_stonl'];
  17. $date=date("Y-m-d");
  18. $ident=ereg_replace(' ', '_', $title);
  19.  
I use an include function to load a design of a page (to avoid having to code designs all the time)

Expand|Select|Wrap|Line Numbers
  1. include("../design/body/twx_rightpane.php");
  2.  
The twx_rightpane.php contains the following variable:

Expand|Select|Wrap|Line Numbers
  1. <?php echo $mainrightpane ?>
  2.  
Which is the variable in my original php page which needs to have the other variables in it:

Expand|Select|Wrap|Line Numbers
  1. $mainrightpane='echo "Title: $title <br />Author: $author <br />Region: $region <br />Version: $version <br />Website: $website <br />STARS: $stars <br />DETAILS: $details <br />RLCHAMPS:  $ch_rl_mgp $ch_rl_sbk $ch_rl_bsb $ch_rl_ama $ch_rl_aus $ch_rl_jpn <br />GPCHAMPS: $ch_gp_mgp500 $ch_gp_stonl <br />Date: $date <br />IDENT: $ident <br /><br />";';
  2. }
  3.  
However, the method above doesnt work.

Stu
May 22 '07 #6

Atli
Expert 5K+
P: 5,058
The problem is how you create the $mainrightpane string.

Using varibale names in strings created with single quotation marks will not output the variable's value as you are trying to do, it outputs the string exactly as you write it without evaluating any variables.

Consider the following:
Expand|Select|Wrap|Line Numbers
  1.  
  2. $name = "Atli";
  3. echo 'My name is $name';// Outputs: My name is $name
  4. echo "My name is $name";// Outputs: My name is Atli
  5.  
  6. // This is similar to your $mainrightpane variable.
  7. $mrp = 'echo "My name is $name";';
  8. echo $mrp; // Outputs: echo "My name is $name";
  9.  
  10. // I don't reccomend this, but it is possible.
  11. eval($mrp); // Outputs: My name is Atli
  12.  
Your $mainrightpane variable contains PHP code, but because you are creating it with single quote marks and then sending it using the echo function, it is not being evaluated as PHP but rather sent as HTML to the client browser.
May 23 '07 #7

pbmods
Expert 5K+
P: 5,821
Take two of these, and post back in the morning:
http://php.net/string
May 23 '07 #8

ak1dnar
Expert 100+
P: 1,584
Your question is still not clear for me, Anyway if you went through the posts submitted by Atli and pbmods Clearly its explaining the way of echoing String variables.

Let me add something for the thread. As you mentioned your $_POST Strings is coming from a Different page and it will store the values in this page.I'll name it main.php .

main.php

[PHP]<?php
$title='Title Goes Here';
$author=$_POST['author'];
$region=$_POST['region'];
$version=$_POST['version'];
$website=$_POST['website'];
$stars=$_POST['stars'];
$details=$_POST['details'];
$ch_rl_mgp=$_POST['ch_rl_mgp'];
$ch_rl_sbk=$_POST['ch_rl_sbk'];
$ch_rl_bsb=$_POST['ch_rl_bsb'];
$ch_rl_ama=$_POST['ch_rl_ama'];
$ch_rl_aus=$_POST['ch_rl_aus'];
$ch_rl_jpn=$_POST['ch_rl_jpn'];
$ch_gp_mgp500=$_POST['ch_gp_mgp500'];
$ch_gp_stonl=$_POST['ch_gp_stonl'];
$date=date("Y-m-d");
$ident=ereg_replace(' ', '_', $title);

include("twx_rightpane.php");

echo $mainrightpane;
?>
[/PHP]

You have to put this include statement after assigning the vales to the $_POST String. Normally Object oriented languages get execute line by line.
if you are adding this line [PHP]include("twx_rightpane.php");[/PHP]

on the top of the page you wont be able to pass the $_POST strings to the $mainrightpane that resides under twx_rightpane.php.

and for the twx_rightpane.php try to echo the string values like this.

twx_rightpane.php
[PHP]<?php
$mainrightpane='
Title: '.$title.' <br />
Author: '.$author.' <br />
Region: '.$region.' <br />
Version: '.$version.' <br />
Website: '.$website.' <br />
';

?>[/PHP]
May 23 '07 #9

P: 3
Sorted, was the double quotations, thanks.

Stu
May 23 '07 #10

P: 1
it needs to be like
Expand|Select|Wrap|Line Numbers
  1. $content ="your username is $username";
  2.  
Jun 20 '07 #11

eragon
100+
P: 431
put username in a session...
put this before ANY html:
[PHP]<?php
session_start();
$_SESSION['userid'] = $_POST['username'];
?>[/PHP]and in the html you put:
[PHP]<?php
echo ('Your username is: '.$_SESSION['userid'].'. Welcome.');
?>[/PHP]
and this username will stay in the browsers cache untill you call this function:

[PHP]<?php
unset($_SESSION['userid']);
?>[/PHP]
and remember, ANY page that uses this session string make sure you put the

[PHP]<?php
session_start();
?>[/PHP]before the html or you will get a nasty error.

ok? is this useful? it is nice, because you can use the username to verify that the user is logged in. if you want to know how, just ask.
Jun 21 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.