0% found this document useful (0 votes)
17 views3 pages

WEBAPPS - Practice Exercise 7 - PHP Repetition Statement

The document discusses a PHP program that uses while, for, and do-while loops to output numbers from 1 to a number entered by the user. The program is run and the output for different test cases like empty input, non-numeric input, and valid numeric input are shown.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

WEBAPPS - Practice Exercise 7 - PHP Repetition Statement

The document discusses a PHP program that uses while, for, and do-while loops to output numbers from 1 to a number entered by the user. The program is run and the output for different test cases like empty input, non-numeric input, and valid numeric input are shown.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

WEBAPPS – PHP REPETITION STATEMENT

PRACTICE EXERCISE #7
Week 6 Date
Section Score

Instruction: Type the following codes in any text editor that you going to use like Notepad++.
Understand and analyze the codes. Run the program and understand the output of the program.

SAMPLE PROGRAM USING WHILE, FOR AND DO..WHILE


LOOP
1: <?php
2: //This program will print 1 to the number inputted in the textbox using
3: //for, while and do..while loop
4: //FILENAME: test5.php
5:
6: ini_set('display_errors',0);
7: error_reporting( E_ALL & ~ E_NOTICE);
8:
9: $num = $_POST['number'];
10: ?>
11: <!DOCTYPE html>
12: <html>
13: <head>
14: <title>test5.php</title>
15: </head>
16: <body>
17: <form action="test5.php" method="POST">
18: Enter a number : <input type="text" name="number" value="<?php echo
$num;?>">
19: <input type="submit" name="submit" value="submit">
20: <button type="button" name="clear"
onclick="location.href='test5.php'">Clear</button>
21: </form>
22: <?php
23: if (isset($_POST['submit']))
24: {
25: if (!empty($num))
26: {
27: if (is_numeric($num) && $num>=1)

Prepared by: Prof. EMILY F. SICAT Page 1


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP REPETITION STATEMENT
PRACTICE EXERCISE #7
28: {
29: echo "Output using <em>for</em> loop<br>";
30: for($ctr=1; $ctr <= $num; ++$ctr)
31: echo $ctr." ";
32:
33: echo "<br><br>";
34: echo "Output using <em>while</em> loop<br>";
35: $ctr=1;
36: while($ctr <= $num)
37: {
38: echo $ctr." ";
39: ++$ctr;
40: }
41:
42: echo "<br><br>";
43: echo "Output using <em>do..while</em> loop<br>";
44: $ctr=1;
45: do{
46: echo $ctr." ";
47: ++$ctr;
48: }while($ctr <= $num);
49: }
50: else
51: echo "Please type number a number and the number should be higher
than 0.";
52: }
53: else
54: echo "Empty values is not allowed...";
55: }
56: ?>
57: </body>
58: </html>

Prepared by: Prof. EMILY F. SICAT Page 2


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP REPETITION STATEMENT
PRACTICE EXERCISE #7
TEST5.PHP – SAMPLE OUTPUT
Ouput upon running the file: Output with empty value:

Output with non-numeric input Output with valid numeric input

Prepared by: Prof. EMILY F. SICAT Page 3


Faculty, College of Computing and Information Sciences

You might also like