Page 1 of 1

concatenating variables

Posted: Thu Oct 02, 2003 10:53 pm
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

Posted: Thu Oct 02, 2003 11:34 pm
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.

hope this is clearer???

Posted: Thu Oct 02, 2003 11:41 pm
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???

Posted: Thu Oct 02, 2003 11:51 pm
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

volka- Thank you very very much

Posted: Fri Oct 03, 2003 12:14 am
by terence
I appreciat it greatly-!!!!