PHP

PHP Menu

The for loop repeatedly executes a block of code as long as a certain condition is met. It is used mostly to execute a block of code for certain number of times.

The following is the syntax for the PHP for loop:

for (init counter; test counter; increment counter) {
    code to be executed for each iteration;
}

An example of a for loop that outputs the numbers from 0 to 10:

<?php
for ($i = 0; $i <= 10; $i++) { echo "The number is: $i <br>"; }

Exercise

Using the for loop print the numbers from 5 to 1 (decreasing order).

<?php
$var = 123;
<?php
for ($i = 5; $i >= 1; $i--) { echo $i . "<br>"; }
{ "test_output_contains":[ { "expected":"5", "error_message":"Please read the instructions again." }, { "expected":"4", "error_message":"Please read the instructions again." }, { "expected":"3", "error_message":"Please read the instructions again." }, { "expected":"2", "error_message":"Please read the instructions again." }, { "expected":"1", "error_message":"Please read the instructions again." } ], "success_message":"Good job!", "error_message":"Please read the instructions again." }

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods