concatenating variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
terence
Forum Newbie
Posts: 21
Joined: Fri Jun 06, 2003 11:31 pm
Location: new york city

concatenating variables

Post by terence »

I am trying to include variables from a seperate php page like this:
<?
$title1=new;
$title2=old;
?>

then on my main page I have this within a while loop
<?
require (theFileAbove.php);
fileRender++;
echo "<html>$title$fileRender</html>";
?>

essentially the file above that include is echoed within the html through the concatenated new variable- how in theory it works?
I tried .- the period concatenator and all it does is print the period.
never had a result like this?

any insight is helpful!!
Terence
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

I'm not sure if I got exacly what you mean. Please take a look to the following code and it's output:

Code: Select all

<?
$title1 = "new";
$title2 = "old";
?>

<?
//Above is the included file:
//require ("theFileAbove.php");

//Now lets set the loop:
$fileRender = 0;
while ($fileRender < 5) {
	echo "<b>$title1$title2$fileRender</b>\n";
	$fileRender++;
}
?>
output:

Code: Select all

&lt;b&gt;newold0&lt;/b&gt;
&lt;b&gt;newold1&lt;/b&gt;
&lt;b&gt;newold2&lt;/b&gt;
&lt;b&gt;newold3&lt;/b&gt;
&lt;b&gt;newold4&lt;/b&gt;
I hope this illustrative example helps you.

Regards,
Scorphus.
terence
Forum Newbie
Posts: 21
Joined: Fri Jun 06, 2003 11:31 pm
Location: new york city

hope this is clearer???

Post by terence »

In your echo you are manually inserting the number.
My goal is to have the fileRender loop concatneates itself on the end of the $title variable.

In return this creates a variable named $title1

then the$title1 variable when echoed prints "new" to the browser.

I do this all the time in actionscript.
creating variables on the fly as the script executes.

hope that makes it clearer???
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

might be easier to use an array

Code: Select all

<?php
$titles = array(
		'new',
		'old',
		'even older',
		'prehistoric'
	);

foreach($titles as $title)
	echo '<p>', $title, '</p>';
?>
but if you insist on having a couple of string-variables you might try

Code: Select all

<?php
$title1 = 'new';
$title2 = 'old';
$title3	= 'even older';
$title4	= 'prehistoric';
	
$fileRender = 1;
while(isset(${'title'.$fileRender}))
	echo '<p>', ${'title'.$fileRender++}, '</p>';
?>
either way you shouldn't rely on knowing how many strings there are and I suggest using the array, keeps things together a bit better.

see also: http://www.php.net/manual/en/language.v ... riable.php
terence
Forum Newbie
Posts: 21
Joined: Fri Jun 06, 2003 11:31 pm
Location: new york city

volka- Thank you very very much

Post by terence »

I appreciat it greatly-!!!!
Post Reply