We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

php variable won't echo out

bschwarting
bschwarting asked
on
Medium Priority
322 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?
Comment
Watch Question

Commented:
Joining command should look like this:
$answerloop365 = $answer1.$loop365;

your version joined the string "$answer" literally, instead of using $answer1 variable
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
echo $$answerloop365;
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
and I am still convinced you are approaching the problem from the wrong end ... and array will be much easier :)

Author

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' ) {
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
sorry, forgot the { } :
http://php.net/manual/en/language.variables.variable.php

echo ${$answerloop365};

Open in new window

Commented:
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?

Commented:
i see it now, sorry for misleading you

Author

Commented:
i want $answerloop365 to = $answer1 and $answer1 = 0
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Also, be sure you are not confusing $answer and $answer1 - they are different variables.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014

Commented:
To achieve this:

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

Just do this:

$answerloop365 = $answer1 = 0;
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
did you try my suggestion?

Commented:
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.

Commented:
Sorry, I meant:
$answerloop365 = "$answer1".$loop365;

Author

Commented:
angel, yes I tried yours and couldn't get it to work.

steve, yours worked great!
$answerloop365 = ${'answer'.$loop365};
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.