HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PHPVariablesData TypesOperatorsStringsArraysAssociative ArraysConditions (if / else)Loops (for / foreach / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
if / elseif / elseTernary operator ( ?: )forforeachwhile
🧩 Function
🔄 Type & Conversion
PHP›REFERENCE›Condition & Loop
🔁

Condition & Loop

Decision-making and repetition structures.

CommandDescription
if / elseif / elseExecutes code based on a condition.
Ternary operator ( ?: )Short if/else.
forLoops a specific number of times.
foreachIterates over each element of the array.
whileLoops as long as the condition is true.

if / elseif / else

Try it yourself →

Executes code based on a condition.

Syntax

if (condition) { } elseif (condition) { } else { }

Description

Condition in parentheses, blocks in { }. Use == for equality or === for strict type checking.

Example

<?php
$score = 75;
if ($score >= 50) {
  echo "Passed";
} else {
  echo "Failed";
}

Expected output

Passed

When to use?

Flow branching, checks.

Ternary operator ( ?: )

Try it yourself →

Short if/else.

Syntax

$result = condition ? a : b;

Description

a if condition is true, b if false. ?? is also used for 'default if null'.

Example

<?php
$age = 20;
echo $age >= 18 ? "adult" : "child";

Expected output

adult

When to use?

Short conditional value.

for

Try it yourself →

Loops a specific number of times.

Syntax

for ($i = 0; $i < n; $i++) { }

Description

Counter-based loop with three parts: start, condition, increment.

Example

<?php
for ($i = 1; $i <= 3; $i++) {
  echo $i;
  echo "\n";
}

Expected output

1
2
3

When to use?

Specific repetition, indexed operation.

foreach

Try it yourself →

Iterates over each element of the array.

Syntax

foreach ($arr as $element) { }

Description

Processes array/associative array elements in order. With key: foreach ($d as $k => $v).

Example

<?php
foreach (["red", "purple"] as $color) {
  echo $color;
  echo "\n";
}

Expected output

red
purple

When to use?

Array processing (PHP's most used loop).

while

Try it yourself →

Loops as long as the condition is true.

Syntax

while (condition) { }

Description

Used when the number of repetitions is unknown. Don't forget to increment.

Example

<?php
$s = 0;
while ($s < 3) {
  echo $s;
  $s++;
}

Expected output

012

When to use?

Indefinite repetition.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code