Search code examples
phpfunctionshort-circuitingshort-circuit-evaluation

Breaking out of multiple functions (short circuiting) in PHP


I want to return multiple nested functions in PHP. It's possible to break out of multiple loops by adding a number after "break". Eg.

while(1)
  while(1)
    while(1)
      break 3;

Can I do a circuit break while calling a sequence of functions?


Solution

  • Not that I know of, it's also not very healthy of a design, as the parent and grandparent functions in question will never know of the break. You should throw an exception and catch it on the parent, which in turn will throw an exception and catch it on the grandparent etc.