Welcome to WebmasterWorld Guest from 54.196.232.162

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP code should be simple, but I'm stumped

...why isn't this working?

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

Preferred Member

10+ Year Member

joined:Jan 4, 2004
posts:456
votes: 0


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

<?php
$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)

Junior Member

10+ Year Member

joined:Feb 27, 2003
posts:49
votes: 0


<?php
$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)

Junior Member

10+ Year Member

joined:Nov 26, 2004
posts:153
votes: 0


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

baze

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

Preferred Member

10+ Year Member

joined:Dec 3, 2004
posts:445
votes: 0


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


<?php
$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!

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

Junior Member

10+ Year Member

joined:Nov 19, 2003
posts:85
votes: 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)

Preferred Member

10+ Year Member

joined:Jan 4, 2004
posts:456
votes: 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)

Full Member

10+ Year Member

joined:Dec 11, 2003
posts:241
votes: 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)

Preferred Member

10+ Year Member

joined:Jan 4, 2004
posts:456
votes: 0


UncleBob,

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)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8137
votes: 102


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)

Junior Member

10+ Year Member

joined:Nov 26, 2004
posts:153
votes: 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.

baze

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

Preferred Member

10+ Year Member

joined:Jan 4, 2004
posts:456
votes: 0


ergophobe,

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)

Junior Member

10+ Year Member

joined:Nov 26, 2004
posts:153
votes: 0


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

baze

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

Preferred Member

10+ Year Member

joined:Jan 4, 2004
posts:456
votes: 0


baze22,

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)

Junior Member

10+ Year Member

joined:Nov 26, 2004
posts:153
votes: 0


Variable variables. Info at:

[php.net...]

baze

[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)

Preferred Member

10+ Year Member

joined:Jan 4, 2004
posts:456
votes: 0


baze22,

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)

Junior Member

10+ Year Member

joined:Nov 2, 2004
posts:56
votes: 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)

Preferred Member

10+ Year Member

joined:Jan 4, 2004
posts:456
votes: 0


DaButcher,

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

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

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8137
votes: 102


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?

Tom

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

Preferred Member

10+ Year Member

joined:Jan 4, 2004
posts:456
votes: 0


ergophobe,

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)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8137
votes: 102


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.

Tom