mandelbug
mandelbug

Reputation: 1798

How to add values in php?

I thought I had this syntax right, but apparently I don't... here's what I have.

    /* Price Calculation */
    if ($stage="1") $price + 1000 = $price;
    if ($stage="2") $price + 2500 = $price;
    if ($stage="3") $price + 5000 = $price;
    if ($chrome=on) $price + 100 = $price;
    if ($bov=on) $price + 200 = $price;
    if ($controller=on) $price + 500 = $price;
    if ($filter=on) $price + 50 = $price;
    print "<br /><u>Total: </u>";
    echo $price;

The variables are already declared correctly, so I know it isn't that. The php has a problem with the ='s. What did I do wrong?

Upvotes: 0

Views: 108

Answers (7)

calebds
calebds

Reputation: 26228

There are a couple of problems:

(1) You are trying to assign the value of $price into $price + number, which is impossible. Try:

$price = $price + number;

(2) Use the == operator for checking equality, like:

if ($stage == "1")

A single = is the assignment operator.

Upvotes: 0

Shahrukh
Shahrukh

Reputation: 1536

if ($stage=="1") $price += 1000;
if ($stage=="2") $price += 2500;
if ($stage=="3") $price += 5000;

if ($chrome=="on") $price += 100;  // if your want to use "on" instead of TRUE, use it as a string
if ($bov=="on") $price += 200;
if ($controller=="on") $price += 500;
if ($filter=="on") $price += 50;
echo "<br /><u>Total: </u>";
echo $price;

Upvotes: 0

ghoti
ghoti

Reputation: 46846

In addition to using == for comparison (since = is for assignment), consider adding using the += operator to add values to variables. Also, while it works, many folks consider it bad practice to skip your curly braces.

  if ($stage=="1") {
    $price += 1000;
  }
  ...

Upvotes: 0

saturngod
saturngod

Reputation: 24967

you should use == in condition

 if ($stage="1")

should be

 if ($stage=="1")

and adding should be

$price = $price + 1000;

Your code should be

<?php

 /* Price Calculation */
    if ($stage=="1") {
        $price = $price + 1000;
    }
    if ($stage=="2") {
        $price = $price + 2500;
    }
    if ($stage=="3") {
        $price = $price + 5000;
    } 
    if ($chrome==true) {
        $price = $price + 100;
    }
    if ($bov==true) {
        $price = $price + 200;
    }
    if ($controller==true) {
        $price = $price + 500;
    }
    if ($filter==true) {
        $price = $price + 50;
    }
    echo "<br /><u>Total: </u>";
    echo $price;

?>

I don't know why you are using on. I think, it's a Boolean condition.

Upvotes: 3

Sabari
Sabari

Reputation: 6335

You should change your code to:

if ($stage=="1") 
   $price = $price + 1000;
if ($stage=="2") 
    $price = $price + 2500;
if ($stage=="3") 
    $price = $price + 5000;
if ($chrome==on) 
   $price = $price + 100;
if ($bov==on) 
   $price = $price + 200;
if ($controller==on) 
   $price = $price + 500;
if ($filter==on) 
   $price = $price + 50;

print "<br /><u>Total: </u>";
echo $price;

Hope this helps :)

Upvotes: 0

Logan Serman
Logan Serman

Reputation: 29880

    if ($stage=="1") $price = $price + 1000;
    if ($stage=="2") $price = $price + 2500;
    if ($stage=="3") $price = $price + 5000;
    if ($chrome==on) $price = $price + 100;
    if ($bov==on) $price = $price + 200;
    if ($controller==on) $price = $price + 500;
    if ($filter==on) $price = $price + 50;
    print "<br /><u>Total: </u>";
    echo $price;

Upvotes: 1

skytreader
skytreader

Reputation: 11707

Shouldn't it be $price = $price + 100; or $price += 100?

Also, you are mistaking your assignment for equality testing. = never meant equality in php---it means assignment (think of it $price = $price + 100 doesn't make sense it it is equality). To test for equality, use == (check your conditions).

Upvotes: 0

Related Questions