Welcome to WebmasterWorld Guest from 54.226.147.190

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP code should be simple, but I'm stumped

...why isn't this working?

     

Storyman

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

10+ Year Member



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.

sidewinder

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

10+ Year Member



<?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 "

baze22

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

10+ Year Member



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

baze

lZakl

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

10+ Year Member



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!

Code Sentinel

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

10+ Year Member



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.

Storyman

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

10+ Year Member



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.

uncle_bob

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

10+ Year Member



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.

Storyman

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

10+ Year Member



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.

ergophobe

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

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



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;

baze22

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

10+ Year Member



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

Storyman

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

10+ Year Member



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.

baze22

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

10+ Year Member



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

baze

Storyman

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

10+ Year Member



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

baze22

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

10+ Year Member



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]

Storyman

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

10+ Year Member



baze22,

Thanks for the info.

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

You never know what is around the corner.

DaButcher

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

10+ Year Member



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.

Storyman

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

10+ Year Member



DaButcher,

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

ergophobe

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

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



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

Storyman

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

10+ Year Member



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.

ergophobe

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

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



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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month