#3 Control structures, what they are and types

In the third installment of the php course I am going to address the control structures, I will answer the questions: What are they? How are they used? What types exist? As always I will add theory concepts and examples explained step by step for its correct understanding.


  • What is a control structure in programming?
  • Control flow in php
  • php control structures
    • The block of statements of the control structures
    • Types of php control structures
  • Extra: summary concepts control structures

What is a control structure in programming?

Understanding what a control structure is involves understanding the control flow of programming, that is, how our code is executed, in what order and priority. You must also understand the concept of instruction or statement.

Control flow in php

He control flow is the order of execution of the lines of code. The order is always the same, sequential, that is, the same as we read the code interpreter reads by people (in the case of php our Apache server). Only this established sequential order can be modified using control structures, concept that I will explain in the following sections.

Imagine reading the typical book of the manual of any device or of the tutorial type, as is customary in reading you will read from left to right and from top to bottom. This is how the code interpreter (the software that executes the code) reads it. This reading order would be the control flow.

php control structures

Continuing with the previous example, now suppose that several images appear with the text of Figure 1, Figure 2, etc. throughout the text. You will continue reading from top to bottom and from left to right until you find a note from the author that says: see Figure 1, see Figure 2. You will search the content for the image indicated and you will have stopped reading the established order of your control flow . This modification in the reading that they have made you do, in programming, is carried out by the control structures.

In php (and in programming in general) the equivalent of “see figure 1” for the code interpreter is a special statement, called a control structure, which usually encloses a portion of the script (several lines) in braces. An example:

$var1 = 6;
if ( $var1 < 5 ) {
echo ‘The number ‘ . $var1 . ‘ is less than 5’;
echo ‘End script’;

control flow establishes that the code in the example should be executed sequentially, that is, sentence by sentence (line by line). First assigning the value 6 to the variable $var1, after reading the if statement and then showing the text “The number 6 is less than 5”. However, the if statementwhich is a control structure, tells the interpreter that only the threw out (enclosed in braces) if the value of the variable $var1 is less than 5. As in the example 6 (the value of $var1) is greater than 5, only the message would be displayed on the screen “End script”.

Notice that the control structure does not end in the character ; but in a key that opens.

The block of statements of the control structures

As I mentioned before, understanding the control structures in programming happens to understand what is a statement block:

  • A statement block (or compound statement) is a number of simple PHP statements (like an echo) surrounded by braces.
  • The sentence blocks define in many occasions (we will see it in future classes) the scope of variables.
  • The blocks can be nested without limit, that is, some blocks can contain others, controlling each other. In the previous php example you could program inside the if another if, and so on.

Types of php control structures

There are three types of control structures:

  • structures conditionals: They allow one or more blocks of statements to be executed if one or more conditions are met.
  • structures iterative or loops: Also known as loops, these structures allow a block of statements to be repeated a finite number of times.
  • functions either procedures: These control structures allow us to name a block of statements with a name of our choice, thus being able to call the execution of this code between braces by this name.

Extra: summary concepts control structures

  • A statement or instruction is a basic programming command that is delimited by a general rule with a semicolon.
  • Control flow is the order of execution of the sentences in programming.
  • A control structure is a special statement that allows to modify the programming control flow.
  • A statement block is always defined in curly braces, one that opens and one that closes. Thus, the execution of these block instructions will always depend on the control structure “owner” of the keys.
  • The blocks of statements can be nested (program one inside the other) without limit.
  • Conditionals, loops, or functions are control structures.
  • Some control structures such as if, they allow to be used without the curly braces, in these cases they act on the sentence immediately after.

Leave a Reply