Solved

php variable won't echo out

Posted on 2009-07-04
16
297 Views
Last Modified: 2013-12-13
Info that is posted from a form:
answer1 = 0;

Variable create on the same page;
$loop365 = 1;

Create a variable from the form data:
$answer1 = $_POST['answer1'];

Join two variables and make a new one:
$answerloop365 = '$answer'.$loop365;

Echo the result:
echo $answerloop365;

The echo = "$answer1" instead of "0"

What am I doing wrong?
0
Comment
Question by:bschwarting
  • 4
  • 3
  • 3
  • +3
16 Comments
 
LVL 2

Expert Comment

by:Erbureth
ID: 24777815
Joining command should look like this:
$answerloop365 = $answer1.$loop365;

your version joined the string "$answer" literally, instead of using $answer1 variable
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24777817
echo $$answerloop365;
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24777820
and I am still convinced you are approaching the problem from the wrong end ... and array will be much easier :)
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24777846
erbureth, i want $answerloop365 to = $answer1, that is why I had '$answer'

in this code:
if ( $answerloop365 == '1' ) {

$answerloop365 always = $answer1 instead of 0

angel, i couldn't get that to work either, i tried:
if ( $$answerloop365 == '1' ) {
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24777855
sorry, forgot the { } :
http://php.net/manual/en/language.variables.variable.php

echo ${$answerloop365};

Open in new window

0
 
LVL 2

Expert Comment

by:Erbureth
ID: 24777857
Ok, now I am confused. Why should

echo $answerloop365;

display 0 if you assign it value '$answer'.$loop365, so it is bound to display the string "$answer" followed by value $loop365?

what exactly are you trying to achieve?
0
 
LVL 2

Expert Comment

by:Erbureth
ID: 24777865
i see it now, sorry for misleading you
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24777866
i want $answerloop365 to = $answer1 and $answer1 = 0
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24777936
Run phpinof() as shown below and check to see if register_globals is set to OFF.  You want it set to OFF for security reasons, but the language in the original post made me unsure whether you're doing this stuff in the right order.  

It would be helpful if you can show us the code you are using now.  Thanks, ~Ray
<?php phpinfo(); ?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24777940
Also, be sure you are not confusing $answer and $answer1 - they are different variables.
0
 
LVL 4

Accepted Solution

by:
stevepicks earned 500 total points
ID: 24777975
try this one
<?php
 $answer1 = 0;
//$answer1 = $_POST['answer1'];
//Variable create on the same page;
$loop365 = 1;

//Create a variable from the form data:


//Join two variables and make a new one:
$answerloop365 = ${'answer'.$loop365};
 
//Echo the result:
echo $answerloop365;

 

?>
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24777977
To achieve this:

"i want $answerloop365 to = $answer1 and $answer1 = 0"

Just do this:

$answerloop365 = $answer1 = 0;
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24777988
did you try my suggestion?
0
 
LVL 14

Expert Comment

by:profya
ID: 24779113
It works if you replaced the single quotes with double quotes, since php parser does not attempt to parse strings encapsulated within single quotes:
$answerloop365 = "$answer".$loop365;

gives you the desired result.
0
 
LVL 14

Expert Comment

by:profya
ID: 24779118
Sorry, I meant:
$answerloop365 = "$answer1".$loop365;
0
 
LVL 1

Author Comment

by:bschwarting
ID: 24781969
angel, yes I tried yours and couldn't get it to work.

steve, yours worked great!
$answerloop365 = ${'answer'.$loop365};
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now