Solved

php variable won't echo out

Posted on 2009-07-04
16
300 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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

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 109

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 109

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

777 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