09.09.2024
Home / Safety / Basic PHP syntax. PHP: Alternative syntax for control constructs PHP syntax is similar to language syntax

Basic PHP syntax. PHP: Alternative syntax for control constructs PHP syntax is similar to language syntax

Allows you to organize the execution of code fragments based on conditions.

Syntax:

If (expression) statement

Can have an unlimited degree of nesting within other IFs.

If($a > $b) print "$a is greater than $b";

else

Extends the capabilities of IF to handle variants of an expression when it is FALSE.

The ELSE expression is executed only if IF is FALSE.

If($a>$b) ( echo "a is greater than b"; ) else ( echo "a is not greater than b"; )

elseif

Is a combination of IF and ELSE. Allows you to execute an expression if the IF value is FALSE, but unlike ELSE it will be executed if the ELSEIF expression is TRUE.

If ($a > $b) ( echo "a is greater than b"; ) elseif ($a == $b) ( echo "a is equal to b"; ) else ( echo "a is less than b"; )

if...endif

One of the possible options for grouping operators with the IF operator.

Useful when embedding large blocks of HTML code inside an IF statement.

If ($a == 1): echo "a is 1"; elseif ($a == 2): echo "a is 2"; else: echo "a is not equal to 1 and 2"; endif; A=5

The HTML code block A=5 will be visible if the condition $a==5 is met

while

The simplest type of loop in PHP. Forces PHP to execute nested statements as long as the condition is TRUE. If the condition is FALSE from the very beginning, then the loop will not be executed even once.

Syntax:

WHILE(condition) expressions

You can group multiple statements inside curly braces, or use an alternative syntax: WHILE(condition)expressions... ENDWHILE;