Peterrr
Peterrr

Reputation: 83

Plussing numbers to a variable in PHP

I have a rather simple question that I believe I can find a simple solution on, on this forum. My code goes here:

<?php
    $tekst = $_POST['tekst'];
    $billeder = $_POST['billeder'];
    $kørende = $_POST['kørende'];
    $cms = $_POST['cms'];
    $funktioner = $_POST['funktioner'];
    $select = $_POST['select'];
    $pris = 0;

    if (isset($_POST['submit'])) {
        if ($select >= 1) {
            $pris + 100;
        }
        if ($select >= 2) {
            $pris + 100;
        }
        echo $pris;
    }
    ?>

So if the user chooses "1" on a dropdown checkbox menu, the variable "$pris" will be 100. If the user chooses "2" on a dropdown checkbox menu, the variable "$pris" will be 200. Since it goes through both of them. However, the "$pris" returns nothing right now due to the reason that it hasn't been set correctly. But are there a nice way doing this that I am not aware of? In such case, please let me know.

My new updated code:

    if (isset($_POST['submit'])) {
        if ($select >= 1) {
            $pris = $pris + 100;
        }
        if ($select >= 2) {
            $pris = $pris + 100;
        }
        echo $pris;
    }
    ?>

However, I am wondering what you mean by using "+=". Can you explain?

Update:

I just got answered everything, thanks a lot, I will now proceed my work. :-)

Upvotes: 0

Views: 79

Answers (2)

codingbiz
codingbiz

Reputation: 26386

You might want to move your echo outside the if, may be that is why nothing is printed out.

$pris = 0;

if (isset($_POST['submit'])) {
    if ($select == 1) {
        $pris += 100;
    }
    else if ($select == 2) {
        $pris += 100;
    }

    //echo here will output nothing here if $_POST["submit"] is not set
}

echo $pris;  //this should output 0 if any condition above was not met

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

$pris + 100 means nothing if you don't assign it somewhere. Use += or $pris = $pris + 100;

<?php
    $tekst = $_POST['tekst'];
    $billeder = $_POST['billeder'];
    $kørende = $_POST['kørende'];
    $cms = $_POST['cms'];
    $funktioner = $_POST['funktioner'];
    $select = $_POST['select'];
    $pris = 0;

    if (isset($_POST['submit'])) {
        if ($select == 1) {
            $pris += 100;
        }
        if ($select == 2) {
            $pris += 200;
        }
        echo $pris;
    }
    ?>

Upvotes: 6

Related Questions