-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
64 lines (58 loc) · 1.92 KB
/
index.php
File metadata and controls
64 lines (58 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.error {color: #FF0000;}
</style>
<title>Array Generator</title>
</head>
<body>
<?php
// define variables and set to empty values
$sizeError = "";
$arraySize = 0;
$generated = false;
// do the following when the button is clicked
if(isset($_POST['btnGenerate'])){
// set input from form to variable
$arraySize = $_POST['array_size'];
// check whether input variable is empty or not numeric
if(is_numeric($arraySize) == false || empty($arraySize)){
$sizeError = "Invalid array size";
}else{
// define the array of random numbers based on the given size
$arrayOfInt = array();
for($i = 0; $i < $arraySize; $i++){
$arrayOfInt[$i] = mt_rand(10, 100);
}
$generated = true;
}
}
?>
<form method="POST">
<table>
<tr>
<td><label for="array_size">Enter size of array:</label></td>
<td>
<input type="text" name="array_size">
<span class="error">* <?php echo $sizeError; ?></span>
</td>
</tr>
<tr>
<td><button type="submit" name="btnGenerate">Generate</button></td>
</tr>
</table>
</form>
<?php
if($generated == true){
// display array elements separated by commas
$arrayElements = implode(', ', $arrayOfInt);
echo "<br><br>Generated Array = [" . $arrayElements . "]";
echo "<br><br> Size of generated array is ". $arraySize. "<br>";
}
?>
</body>
</html>