Chapter 30 More with Loop Control Structures

30.1 Simple Exercises with Loop Control Structures

Exercise 30.1-1 Counting the Numbers According to Which is Greater

Write a PHP script that prompts the user to enter 10 pairs of numbers and then counts and displays the number of times that the first user-provided number was greater than the second one and the number of times that the second one was greater than the first one.

Solution

The PHP script is as follows. It uses variable $countA to count the number of times that the first user-provided number was greater than the second one and variable $countB to count the number of times that the second one was greater than the first one.

 file_30.1-1
<?php
$countA = 0;
$countB = 0;
 
for ($i = 1; $i ...

Get PHP and Algorithmic Thinking for the Complete Beginner now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.