User Tools

Site Tools


programmieren:grundlagen:kontrollstrukturen

Control Structures

Control structures in programming are like the structures that let you define how code should operate. Its basically the most important thing of programming languages.

For example the control structure if defines if the given case is true it will execute a certain code within it, if its not true it does nothing.

// This case is true since 2+2 does infact equal 4
if ((2+2) === 4) {
  echo 'This will be echoed';
}

The whole list of PHP Control Structures is found on the official PHP documentation

goto

A less common control structure in PHP that I just found out about is goto.

What it does is pretty self explanatory. The code flow, after hitting a goto tag will navigate to the point defined in goto by case sensitive text with a colon afterwards.

Like so:

goto after;
 
echo 'Wont be echoed';
 
after:
 
echo 'Will be echoed';

I feel like theres a use for it only very rarely, like if you would like to skip a certain code part in a certain condition, if that somehow isn't possible by returning early, or any other means.

I also think it's more useful in procedural programming than Object Oriented.

programmieren/grundlagen/kontrollstrukturen.txt · Last modified: by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki