php variable won't echo out

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?
LVL 1
bschwartingAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
stevepicksConnect With a Mentor Commented:
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
 
ErburethCommented:
Joining command should look like this:
$answerloop365 = $answer1.$loop365;

your version joined the string "$answer" literally, instead of using $answer1 variable
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
echo $$answerloop365;
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
and I am still convinced you are approaching the problem from the wrong end ... and array will be much easier :)
0
 
bschwartingAuthor Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry, forgot the { } :
http://php.net/manual/en/language.variables.variable.php

echo ${$answerloop365};

Open in new window

0
 
ErburethCommented:
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
 
ErburethCommented:
i see it now, sorry for misleading you
0
 
bschwartingAuthor Commented:
i want $answerloop365 to = $answer1 and $answer1 = 0
0
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
Also, be sure you are not confusing $answer and $answer1 - they are different variables.
0
 
Ray PaseurCommented:
To achieve this:

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

Just do this:

$answerloop365 = $answer1 = 0;
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
did you try my suggestion?
0
 
profyaCommented:
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
 
profyaCommented:
Sorry, I meant:
$answerloop365 = "$answer1".$loop365;
0
 
bschwartingAuthor Commented:
angel, yes I tried yours and couldn't get it to work.

steve, yours worked great!
$answerloop365 = ${'answer'.$loop365};
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.