There is for example the code:

$a = 3; $b = 1; $op = "-"; switch ($op) { case '+': echo $a + $b ; case '-': echo $a - $b ; case '*': echo $a * $b ; case '/': echo $a / $b; case '%': echo $a % $b; break; } 

I tried different options - with quotes, without, </br> , /n... I can not understand how to make a line break after each echo in case ?

  • four
    Why do after each? Why can't I do one after the switch? - Ipatiev
  • Well, in general, put a logical operation in parentheses and construct with a string, for example echo ($a + $b).'</br>' ; but nevertheless Ipatyev is right: why this hemorrhage, if you can put a transfer after the switch - Alexey Shimansky
  • What is /n ? Maybe you are looking for "\n" ? --- must be in double quotes if \n in single quotes will not work. - Arnial 1:01
  • and how to catch the error of division by zero if $ b = 0; - Beginner
  • @Sven if($b === 0) echo "Это число 0" . In general, read about try catch . You can also write case '/': if($b !== 0) echo $a / $b; else echo "Деление на 0"; case '/': if($b !== 0) echo $a / $b; else echo "Деление на 0"; - MaximPro

2 answers 2

 switch ($op) { case '+': $res=$a + $b ; case '-': $res=$a - $b ; case '*': $res= $a * $b ; case '/': $res=$a / $b; case '%': $res=$a % $b; break; } echo $res.PHP_EOL; 
  • and how to catch the error of division by zero if $ b = 0; - Beginner
  • <code> case '/': $ res = $ a / $ b; </ code> - abrdabr
  • replace with case '/': if (b! = 0) {$ res = $ a / $ b;} else {$ res = 'ERROR';} break; - abrdabr
  • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky

Or

 echo '<br />'; 

or

 echo "\n"; 

or

 echo PHP_EOL; 

(note the double quotes in the last example)

And by the way, break needs to be added after each condition

  • and if I want all my operators to complete with line breaks only - Beginner
  • @Sven did not understand what you want? - Anton Shchyrov
  • You wrote: And by the way, break needs to be added after each condition. And I am writing that I need to get all cases out. - Beginner
  • @Sven well put it as I told you in the comment echo ($a + $b).PHP_EOL; for all kaysof - Alexey Shimansky
  • @Sven Some wild desire. Well, if you want, then yes, break not needed - Anton Shchyrov