Solved

php variable won't echo out

Posted on 2009-07-04
16
298 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RDBMS and No sql database 4 47
PHP warning 4 30
How to keep line breaks when outputting data from database 4 20
hasing a url 16 8
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

864 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

16 Experts available now in Live!

Get 1:1 Help Now