-2
<?php

$operation = readline('which operation do you want to use? (+, -, %) ') . PHP_EOL;

if ($operation != '+' || $operation != '-' || $operation != '%' ) {
    echo " '$operation' is not a valid operation"; 
} 

$number1 = readline('First number? ') . PHP_EOL;

if ($number1 != is_numeric($number1)) {
    echo " '$number1' is not a number ";
}

$number2 = readline('Second number? ') . PHP_EOL;



if ($operatie == '+') {
    echo 'Your result is:' . $number1 + $number2;  
}

if ($operatie == '-') {
    echo 'Your result is: ' . $number1 - $number2;    
}

if ($operatie == '%') {
    echo 'Your result is: ' . $number1 % $number2;
}
?>

i expected this to be a working calculator but i don't know how to fix i would really appreciate it if you can fix it for me

Harmen
  • 3
  • 1
  • Welcome to Stack Overflow! In what way is your code not working as expected? Please elaborate on the specific problem you are observing and what debugging you have done. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jan 30 '23 at 12:57
  • It looks like a typo, where does you ```$operatie``` comes from? – Eli-ott Jan 30 '23 at 13:05

2 Answers2

0

I fixed your code, it is broken in many places
You need a while loop for the operator validation and for checking if input is numeric, also you can't use an if statment for this
The last part where you print out the answers, you just can't concat the calculations to the str, it will result in an error
If you have any questions about the code I wrote feel free to ask

<?php

$operator = readline("Which operator would you like to use? (+, -, %): ");

while(!($operator == '+' || $operator == '-' || $operator == '%')) {
    echo "'$operator' is not a valid operation\n";
    $operator = readline("Which operator would you like to use? (+, -, %): ");
}

$number1 = readline('First number: ');

while(!(is_numeric($number1))) {
    echo "'$number1' is not a number\n";
    $number1 = readline('First number: ');
}

$number2 = readline('Second number: ');

while (!(is_numeric($number1))) {
    echo "'$number1' is not a number\n";
    $number2 = readline('Second number: ');
}

if ($operator == '+') {
    echo 'Your result is: ' . strval($number1 + $number2);  
}

if ($operator == '-') {
    echo 'Your result is: ' . strval($number1 - $number2);    
}

if ($operator == '%') {
    echo 'Your result is: ' . strval($number1 % $number2);
}
?>

Maxhu787
  • 18
  • 5
-1

The issue is with your first condition, you have to use && instead of || because like this execution will always fall inside the if statement

wael razouk
  • 88
  • 2
  • 8