Gen. partnerAlgotech

Conditions and branching

No more linear programs! The most basic principle of any program is "what happens when....". A condition can be written as a logical statement, which may be valid (the condition is satisfied) or invalid (then it is not executed or its exact opposite is executed). Both are easy to define.

|
September 7, 2019

Warning: This article was written many years ago and some information may be outdated or incorrect. Please bear this in mind when reading.

No more linear programs! The most basic principle of any program is "what happens when....". A condition can be written as a logical statement, which may be valid (the condition is satisfied) or invalid (then it is not executed or its exact opposite is executed). Both are easy to define.

General notation

In general, a condition can be written as a logical statement. The condition may or may not be satisfied. It is a good idea to count both options as possible. If there are multiple alternatives, it is called a nested condition.

Example:

if (operation value value) {
// This will execute if the condition is true
} else {
// This will execute if the condition does not hold
}

We don't always have to define both options (sometimes it's completely unnecessary). In fact, we can define the situation if only the condition holds. This is done as follows:

if (operation value value) {
// This is executed if the condition is true
}

Logical operators

Operator Meaning
== Equals
=== Equals and has the same data type (anything can be compared to anything, but the condition is only satisfied if it is a value of the same data type (e.g. number, text, ...))
!= Does not equal
<= Equal to or greater than
>= Equal to or less than
< Greater
> Less

Real Example

$a = 5;
$b = 3;
if ($a === $b) {
// block to be printed if $a equals $b
} else {
// block that is printed if $a does NOT equal $b
}
```
Nested conditions
--------------------------
Unfortunately, the output is only `true` (valid) and `false` (invalid). So if we want to consider multiple possibilities, we have to nest multiple conditions inside each other. This is called a **nested condition**. It is nested because one of the solutions to the condition is just another condition.
```php
$a = 5; // left pocket
$b = 3; // right pocket
$kapsa = true; // do I have a pocket?
if ($kapsa === true) {
if ($a > $b) {
echo 'There's more in the left pocket';
} else {
echo 'There is more in the right pocket';
}
} else {
echo 'You have no pocket';
}
```
Loading comments...

Stay in the loop

Subscribe to our newsletter and get the latest cybersecurity news delivered straight to your inbox.

Your data is safe. You can unsubscribe from the newsletter at any time.