homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

PHP code should be simple, but I'm stumped
...why isn't this working?

 1:17 am on Dec 4, 2004 (gmt 0)

This should be simple, but I can't get it to work.

$section1= "Test Page";
$i = 1;
echo '$section'.'$i'; // This is the line with the problem code

I've tried everything I can think of without positive results.



 1:23 am on Dec 4, 2004 (gmt 0)

$section1= "Test Page";
$i = 1;
echo "$section$i";

or echo $section.$i;
or echo "$section" . "$i";

It is your use of ' instead of " that would be the problem.
You also might want a space at the end of $section1 eg "Test Page "


 1:24 am on Dec 4, 2004 (gmt 0)

you need to use double quotes (") instead of single quotes in your echo statement. Single quotes print exactly what between them.



 1:26 am on Dec 4, 2004 (gmt 0)

If that's just part of the code, I don't really understand what you are trying to do, but does this help?

$section1= "Test Page";
$i = 1;
echo "$section1 $i"; // This is the line with the problem code

wow, before I could even finish writing, 2 replies... that was FAST!

Code Sentinel

 2:07 am on Dec 4, 2004 (gmt 0)

echo '$section'.'$i'; // This is the line with the problem code


single quoted tell php not to parsed/substitute the variables, when you put the variable $section between single quotes PHP will look at it as a string "$section" instead of the variable $section or its contents "Test Page".

echo $section.$i; should work (Test Page1) or
echo $section.' '.$i; (Test Page 1) the space is in quotes.

of course you could also just use double quotes like others have mentioned.. I hate how that messes up code coloring in dreamweaver though.


 2:40 am on Dec 4, 2004 (gmt 0)

Thanks people, but none of these work...

echo $section.$i; RESULT: 1
echo "$section" . "$i"; RESULT: 1
echo "$section1 $i"; works only because of $section1
echo '$section'.'$i'; RESULT: $section$i
echo $section.' '.$i; RESULT: 1

This really should be simple, but... it apparently isn't.


 3:29 am on Dec 4, 2004 (gmt 0)

You are mixing up your variable names, $section1 and $section

$section1= "Test Page";

and then

echo '$section'.'$i';

Try setting error_reporting = E_ALL in your php.ini, and it would have told you
Notice: Undefined variable: section in D:\Inetpub\test\test.php on line 4

It makes debugging much much easier.


 3:56 am on Dec 4, 2004 (gmt 0)


You'd think this would work...
echo '$section'.'$i';

...but the result is till '1'.

(Regarding $section1/$section: it was suggested among the suggestions and as per my note for the result so duly noted.)

Although the answer hasn't appeared it is good to know that I'm not alone on this.


 5:10 am on Dec 4, 2004 (gmt 0)

Took me a while to figure out what you want to do. If I understand right, what you want is this:

$a = "section" . $i;
$$a = $section . $i;

echo $section1;


 5:38 am on Dec 4, 2004 (gmt 0)

Or is this what you're trying to do?

$section1 = 'Test page';
$i = 1;
$varname_root = 'section';
$varname = $varname_root . $i;
echo $$varname;

Looking at ergophobe's post made me go back an look at your original post. I missunderstood what you wanted for output.



 5:41 am on Dec 4, 2004 (gmt 0)


You're still using: echo $section1

What I'm trying to do is to assign variables like this:

$section1= "Test Page One";
$section2= "Test Page Two";
$section3= "Test Page Three";
and so on...

Then assign variable: $i=1;

Now echo each $sectionN using the assigned variable $i.

So the code stays the same and only the value of $i changes.

Everything I've tried has failed and what should work doesn't. The suggestions given so far don't work either.

It seems like it should be a simple code that should work out of the box, but simply doesn't.

I keep looking at the code and try to figure out what we are all missing.


 5:43 am on Dec 4, 2004 (gmt 0)

I think my post #10 does what you are looking for. (must have posted while you were replying)



 6:02 am on Dec 4, 2004 (gmt 0)


Absolutely right on both accounts.

The double $$ makes all the difference: echo $$varname;

When I first saw $$ it looked like a typo. Can you tell me a little more about why it works and/or were I can read more aobut it?

Thanks everyone for helping and a special kudo to baze22


 6:23 am on Dec 4, 2004 (gmt 0)

Variable variables. Info at:



[edited by: coopster at 11:39 pm (utc) on Dec. 4, 2004]
[edit reason] hooked up url [/edit]


 7:13 am on Dec 4, 2004 (gmt 0)


Thanks for the info.

I've never heard of Variable Variables until now. Cool stuff.

You never know what is around the corner.


 11:55 am on Dec 4, 2004 (gmt 0)

You can also use arrays:

$section[0] = "this is the first section";
$section[1] = "this is the second section";
$section[2] = "this is the last section";

for($i = 0; $i < count($section); $i++) {
echo $section[$i];

ps. I did not test this code.


 6:26 pm on Dec 4, 2004 (gmt 0)


Thanks for that suggestion. It makes my approach look rather clumsy.


 9:10 pm on Dec 4, 2004 (gmt 0)

Well, I knew it had to be a variable variable solution (that's right, the $$ was not a typo), but I think it took everyone a while to figure out what you wanted to do and what were the givens and what he unknowns.

Defintely use arrays if it's possible. Sometimes though, variable variables let you do things that arrays will not.

So is it all sorted now?



 2:17 am on Dec 5, 2004 (gmt 0)


Truth is that when you're on the steep slope of the learning curve you aren't always sure of what exactly you're after because there are so many ways to do the same thing.

Everyone's help (and patience) is greatly appreciated.


 3:50 am on Dec 6, 2004 (gmt 0)

So will you be able to work it out with arrays?

BTW, it doesn't matter where you are on the slope. Setting up the problem is usually the hard part. I think that's why the discussion form works so well. I think we get a lot of threads where it goes back and forth a lot until the question is defined sufficiently and then the answer pops out in a few lines. I always htink that's pretty cool.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved