Control structures in PHP

Control structures in PHP

For control structures, PHP has the control sentences typical of high level languages

Variable statement

Variables in PHP are not assigned a predefined type

In PHP the type of variables depends on the value they contain at all times

Therefore it performs an automatic conversion of types

PHP recognizes the following types of values:

  • Numbers

    integers and real

  • Boolean values

    true and false

  • Strings
  • Arrays

    data type that associates values with keys (such as an ordered map)

  • Iterables

    pseudotype introduced in PHP 7.1

    Accepts any array or object that implements the interface Traversable

  • Resources

    reference to an external resource

  • The value null
  • Objects

    Created by the programmer or pre-defined by the language

Since there are no types of a priori variables, we do not have to specify the type of variable when declaring it

The variable declaration is made by prepending the reserved word $ to the variable name

It is possible to assign the value when we declare

The if sentence

The if sentence has the form:

The parentheses associated that define the condition are not optional

If the condition is true, instruction 1 will be executed; otherwise it is executed if there is instructution 2

The use of the sentence else is optional, so brackets have been used in the definition

If omitted, the instruction block will only be considered when the condition is true

A block of instructions is a set of instructions bounded by curly brackets

Keys after sentencing if are not required

If omitted and the condition was true, the following instruction will be executed

Otherwise, the subsequent instruction will be executed, independent of the condition state

Thus, the omission of keys after the sentence if it will allow us to write everything on a single line finished in ;

The elseif sentence

We can also use sentences if anities by sentencing elseif

Sentences elseif work just like a sentence if

But they will only be executed in case the condition of the sentence if was false

The switch sentence

Makes is select a group of sentence among several possible

It is an alternative to the use of sentences elseif anities

Its syntax is:

The expression in parentheses of the switch must be integer or string

Your result will be compared with the different values of the case

If it matches one of them, the following sentence will be moved to the case with that value and consecutive instructions will continue to be executed until a sentence is found break or reach the switch lock keys

In case the result of the expression does not match any value, execution will be passed to the next statement sentence on the label default, if there were, and it will continue as a case

The values in the case can be a constant expression

There can be two case with the same value

Unlike other languages, the sentence continue applies to switch and acts in a similar way to break

If you have a switch within a loop and want to continue to the next iteration of the outer cycle, it will be used continue 2

You can use a semicolon instead of a colon after a case

Consecutive instructions will continue to be executed normally until a sentence is found break or reach the switch lock keys

The match statement

It was added in PHP 8.0 version

Makes is select a group of sentence among several possible

It is an alternative to the use of sentences elseif nested and is similar to the statement switch

Similar to a sentence switch, a match expression has a subject expression that is compared against multiple alternatives

Unlike switch, will be evaluated at a value very similar to that of ternary expressions

But using an identity check (===) instead of a weak equality check (==)

Return a value

Subsequent values ​​are no longer processed, as is done in statements switch

It is not possible to execute blocks of code in each condition, as is done in statements switch

The expression used in the match must be complete, if it is not handled by any case the exception will be thrown UnhandledMatchError

Within the same case it can contain several expressions separated by a comma, it is similar to a OR logical

A special case is the pattern default, which allows matching anything that has not been previously matched

If several default patterns are used, the exception will be thrown E_FATAL_ERROR

Its syntax is:

Let's see how the example about beers that we saw with would look switch, now with match:

The while sentence

The while sentence has the form

The parentheses are not optional

If the condition is met, the sentence or instruction block is executed and the process is repeated until the condition is no longer met

The do sentence

The do sentence has the form

It's very similar to the sentence while, except that the condition goes after the instruction or block of instructions

So they are executed at least once even if the condition is false

The for sentence

In the case of such a sentece, in PHP we can distinguish two variants:

  • The loop for "classic"
  • The foreach loop

The loop for "classic"

This loop has a syntax very similar to that of C / C+

In this syntax:

Initialization creates the counter variable and gives it an initial value

Condition must be fulfilled for the loop to run

Depends on the variable index

Expression updates the value of the variable index

The equivalent of this while expression is:

The foreach loop

This loop has a syntax very similar to the for-each Java

Itera una variable $var sobre todas las propiedades de un objeto $obj que se le pasa

Así para cada valor de $var se ejecutaran las sentencias del bucle

Therefore, the loop will have as many iterations as the object's properties, and in each iteration the variable will have the value of the corresponding object's property with that iteration

Its syntax is:

The break sentence

The break sentence can be placed within a loop or anities loops

When you execute the break sentence exits the loop more internal

To all effects, the break sentence acts as a jump to the sentence following the loop in which it runs

The continue sentence

The continue sentence does not leave the loop but causes the next iteration to run

In the loop while the execution of the continue causes the program flow to jump to the condition

In the for loop the continue execution causes the increment expression to run, and then continue normally with the condition

That is, running the continue prevents the rest of the loop body from running

If you have a switch within a loop and you want to continue to the next iteration of the outer cycle, it will be used continue 2