Am I doing something wrong?

$i==3 ? break : $i++ ; 

I do not get the expected result.

  • The ternary operator is an abbreviated if/else entry. What could there be a break ? - Alexey Shimansky
  • Alexei, therefore, I ask what surprises you so much))) It turns out that the ternary operator does not repeat the if / else function completely, as I thought. - Victor Vasiliev
  • Even in the usual if there is no break . Separated from cycles, this in itself is a meaningless construction - Alexey Shimansky
  • What kind of nonsense))) Exit from the cycle can be performed through the form if in php. Especially since I used this example just for the loop, it’s just that the code doesn’t indicate that the users would be more comfortable understanding the situation. - Victor Vasilyev
  • Re-read what I wrote for in-depth awareness ....... and show where you have the cycle in question? - Alexey Shimansky

2 answers 2

The ternary operator uses data of the same type. You cannot use words like break or continue .

Example of use:

 $i += $i > 5 ? 1 : 2; 

Use

 if ($i == 3) break; $i++; 

    break not a value .. in the ternary operator you need to return something

    • That is, in the ternary operator, you can only return variables? - Victor Vasilyev
    • @ Viktor Vasiliev yes. not only variables but expression (expression), which may consist of one variable - Alexey Shimansky