Solved

php variable won't echo out

Posted on 2009-07-04
16
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 143

Expert Comment

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

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 143

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
 
LVL 110

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 110

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 110

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 143

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 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