0

I Want to make a calculator. I made the code but problem is when some number divide by zero it gives an Exception,so it is unable to generate the correct result, is there a fix for this? see code

<?php
$x = $_POST['x'];
$y = $_POST['y'];
$tafel = 10;
$antwoord = '' ;
{
    if($y = 0)
        return 'Voer voor y een ander getal in';
}

switch ($_POST['type']) {
    case 'plus':
        $antwoord = $x + $y;
        break;
    case 'keer':
        $antwoord = $x * $y;
        break;
    case 'wortel':
        $antwoord = sqrt($x);
        break;
    case 'min':
        $antwoord = $x - $y;
        break;
    case 'deel':
        $antwoord = $x / $y;

        break;
    case 'kwadraat':
        $antwoord = pow($x, 2);
        break;
    case 'macht':
        $antwoord = pow($x, $y);
        break;
    case 'tafel':
        for($i = $x; $i <= 10; $i++){
        echo $x * $i.'<br>';
        }
        break;
    default:
        # code...
        break;
}
echo '<h1 id="answer"> Antwoord:</h1><br>';
echo $antwoord;
            ?>
Khurram Sharif
  • 504
  • 1
  • 4
  • 20

3 Answers3

3

The following statement will always be true as you're actually assigning the value within your if statement, instead of comparing it.

if($y = 0)

to

if(0 == $y)
Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • 1
    Good catch. Thus why I always use something like `if(0 == $y)`... as it will throw an error if I mistakenly assign the value instead of comparing it. ^^ – Jon Jan 17 '15 at 18:27
  • 1
    Nice! Didn't even notice that one. There is a coding style to avoid these kinds of programming errors. It's called yoda-style, in which you turn around the comparison. if(0 = $y) { } This way the language will fail because you can't assign something to zero. – Dennis Jan 17 '15 at 18:28
  • Those are both excellent points! I will adjust this to reflect that. – Ohgodwhy Jan 17 '15 at 18:29
  • Just realized... you still want to perform the other operations if `0==$y`, so that error checking should be done if the case is actually for division instead of blanketing it before all operations. – Jon Jan 17 '15 at 18:40
2

Just change this:

(If this code is not in a function)

return 'Voer voor y een ander getal in';

to this:

die('Voer voor y een ander getal in');

So that the script stops! And you have to make a comparison like this:

if($y == 0)
    //^^ See here 2x '='

For more information about comparison operator see the manual: http://php.net/manual/en/language.operators.comparison.php

Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

The following code work fine for you

= means assign

== means comparison

in your situation your need is to compare in if condition

<?php
    $x = $_POST['x'];
    $y = $_POST['y'];
    $tafel = 10;
    $antwoord = '' ;
    {
        if($y == 0)
            return 'Voer voor y een ander getal in';
    }

    switch ($_POST['type']) {
        case 'plus':
            $antwoord = $x + $y;
            break;
        case 'keer':
            $antwoord = $x * $y;
            break;
        case 'wortel':
            $antwoord = sqrt($x);
            break;
        case 'min':
            $antwoord = $x - $y;
            break;
        case 'deel':
            $antwoord = $x / $y;

            break;
        case 'kwadraat':
            $antwoord = pow($x, 2);
            break;
        case 'macht':
            $antwoord = pow($x, $y);
            break;
        case 'tafel':
            for($i = $x; $i <= 10; $i++){
            echo $x * $i.'<br>';
            }
            break;
        default:
            # code...
            break;
    }
    echo '<h1 id="answer"> Antwoord:</h1><br>';
    echo $antwoord;
                ?>
Khurram Sharif
  • 504
  • 1
  • 4
  • 20