0

I am having an issue with my variables value after the IF function. I am looking to change the value of the variable depending on the outcome value of $answer1 on the sender page. However, when I print $answer1 after the IF process, the value has not changed. Before IF it is 5, after the IF process, the Print still produces a 5.

Please can someone advise me how I can increment/decrement using the IF function and affect the value of $answer1 afterwards.

Thank you.

<?php
    $finalvalue=5;

    $answer1=$_GET['answer1'];

    if ($answer1=="1"){$finalvalue+2;}
    elseif ($answer1=="2"){$finalvalue+1;}
    elseif ($answer1=="3"){$finalvalue-1;}
    elseif ($answer1=="4"){$finalvalue-2;};
    print "$finalvalue"
?>
1
  • are you looking for somthing like $finalvalue=$finalvalue+2; and so on? Commented Nov 21, 2015 at 15:08

2 Answers 2

1

You need to increment on the variable. Have a look here and try the following:

$finalvalue = 5;

$answer1 = $_GET['answer1'];

if ($answer1 == "1") {
    $finalvalue += 2;
} elseif ($answer1 == "2") {
    $finalvalue++;
} elseif ($answer1 == "3") {
    $finalvalue--;
} elseif ($answer1 == "4") {
    $finalvalue -= 2;
}
echo $finalvalue;
Sign up to request clarification or add additional context in comments.

Comments

0

You are not properly updating the $finalvalue. It should be as follows -

<?php
$finalvalue=5;

$answer1=$_GET['answer1'];

if ($answer1=="1"){ $finalvalue = $finalvalue+2;}
elseif ($answer1=="2"){ $finalvalue =  $finalvalue+1;}
elseif ($answer1=="3"){$finalvalue =  $finalvalue-1;}
elseif ($answer1=="4"){$finalvalue =  $finalvalue-2;};
echo $finalvalue;
?>

5 Comments

Thank you very much, this has worked, I did try something similar to this earlier however I used the ++ function and I am not sure I coded it correctly!
Thank you very much for your help
Thank you for your help earlier Chandan, I have encountered one issue with it though, on application, the last three increments of the final "else if" function have not been affecting the value or $finalvalue, is there something I should be adding to the end of my string for them to take effect? or should I be using pre incrementation as opposed to post incrementation? Please see below. thanks.
$answer5=$_GET['answer5']; if ($answer5=="17"){ $finalvalue = $finalvalue+2;} elseif ($answer1=="18"){ $finalvalue = $finalvalue+1;} elseif ($answer1=="19"){$finalvalue = $finalvalue-1;} elseif ($answer1=="20"){$finalvalue = $finalvalue-2;}; print "$finalvalue";
this should work. Can you confirm by putting echo statements in those elseif block if it is getting called ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.