How to use echo output as a new variable?

Discussion in 'PHP' started by ingilizdili, Oct 25, 2010.

  1. #1
    Hello;
    Is it possible to pass the output of echo to a new variable?
    What I mean is something like that:

    $a = '1';

    $b = (echo $a);
     
    ingilizdili, Oct 25, 2010 IP
  2. _:codefan:_

    _:codefan:_ Active Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #2
    no becouse echo() doesn't return any value.

    You can do something like this:

    
    $foo = 'foo';
    
    echo $bar = $foo; //foo
    
    echo $bar; //foo
    
    
    PHP:
     
    _:codefan:_, Oct 25, 2010 IP
  3. max2010

    max2010 Greenhorn

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    no, anyway I don't get the reason to do that, the output of "echo $a" is the same as "echo $b" if you assign $a value to $b like this $b=$a
    if you need the var dump, do this:
    $b=var_dump($a);
     
    max2010, Oct 25, 2010 IP
  4. max2010

    max2010 Greenhorn

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    if you need some return values, check print and printf instead
     
    max2010, Oct 25, 2010 IP
  5. Amator

    Amator Well-Known Member

    Messages:
    1,424
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    165
    #5
    This is not possible.
    What you can do is
    $b = $a;
    echo $b;
    HTML:
     
    Amator, Oct 25, 2010 IP
  6. reli4nt

    reli4nt Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you explain why you wanted to do it, we may be able to offer a solution.
     
    reli4nt, Oct 26, 2010 IP
  7. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The problem is;
    User X answers a question on a particular grammar subject, let's say pronouns and if the answer is correct, the column value corresponding to the correct answers will increase by one and vice versa. Each subject has its unique code, pro for pronouns, adj for adjectives etc. The column name for correct answers on pronouns is pro_c; and for incorrect answers is pro_i. The script is able to determine the subject using GET or POST method. Therefore, I leave it as a variable, $subject. What I want is to be able to update the columns in accordance with the answers. For correct answers, the column $subject_c must increase by one and for incorrect ones the column $subject_i will increase as due.
    Now, the question is how to create the variables $subject_c and $subject_i.
     
    ingilizdili, Oct 26, 2010 IP
  8. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i think that _:codefan:_ answered your question already.

    first you put it in another variable and then you echo it.
     
    w47w47, Oct 26, 2010 IP
  9. reli4nt

    reli4nt Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You would create $subject_c and $subject_i at the point where your script determines if the answer is correct or incorrect. This I assume happens when the user hits submit. Then you return these values on the resulting page.

    It almost sounds like you are trying to update the columns as the user answers the questions in real time. PHP requires a call to the server (submitting the page or calling a new page) so JavaScript would be your answer there.
     
    reli4nt, Oct 26, 2010 IP
  10. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thank you very much. I managed to solve the problem. It was much simpler than I thought. Here it is;
    $subject = $_POST['subject'];
    $c = "_c";
    $i = "_i";
    $e = "$subject{$c}"; //gives me $subject_c, the cell for correct answers
    $f = "$subject{$i}"; //gives $subject_i, the cell for incorrect answers

    Then I use the variables $e or $f while updating the cells $subject_c or $subject_i.
     
    ingilizdili, Oct 26, 2010 IP