Loops and If#
Example code#
<?php
echo "hello php";
print "Hello Amigos";
print("http://localhost/wp/page1");
print("\n");
$output = 0;
$counter = 0;
print("For Loop\n");
for ($counter = 0; $counter <= 20; $counter++) {
echo("for: ".$counter."\n");
}
$counter = 0;
print("\nWhile Loop\n");
while ($counter < 10) {
echo("while: ".$counter."\n");
$counter += 1;
}
$num = 12;
if ($num < 10) {
echo "This was less than ten";
} else if ($num < 15) {
echo "This was less than 15";
} else {
echo "This was greater than 15";
}
?>
Purpose: Demonstrate basic output, looping structures, and conditional statements.
Breakdown:
Output Statements:
echo "hello php";: Prints the string “hello php” to the output.print "Hello Amigos";: Prints the string “Hello Amigos” to the output.print("http://localhost/wp/page1");: Prints the URL “http://localhost/wp/page1” to the output.print("\n");: Prints a newline character to the output.
Variable Initialization:
$output = 0;: Initializes a variable named$outputwith the value 0.$counter = 0;: Initializes a variable named$counterwith the value 0.
For Loop:
for ($counter = 0; $counter <= 20; $counter++) { ... }: Iterates from$counterequal to 0 up to 20 (inclusive), incrementing$counterby 1 in each iteration. Inside the loop, it prints the current value of$counter.
While Loop:
while ($counter < 10) { ... }: Continues to execute as long as$counteris less than 10. Inside the loop, it prints the current value of$counterand increments$counterby 1.
Conditional Statements:
if ($num < 10) { ... } else if ($num < 15) { ... } else { ... }: Checks the value of$numand executes the appropriate code block based on the comparison.
Summary: The code demonstrates the following PHP concepts:
Output using
echoandprint.Variable declaration and assignment.
Looping structures:
forandwhile.Conditional statements:
if,else if, andelse.
The code effectively illustrates how to use these concepts to perform basic operations and control the flow of execution in a PHP script.