TU Solution- BIM- 2019 (Makeup)
11. Write a PHP program to create a form that contains two textboxes which accepts numbers and a submit button. When submit button is clicked, sum of two numbers will be displayed.
<!doctype html> <html> <head> <title>Year : 2019 (Makeup) | Q : 11</title> </head> <body> <?php if(isset($_POST['add'])) { $n1 = $_POST['n1']; $n2 = $_POST['n2']; if(is_numeric($n1)&&is_numeric($n2)) die("Sum is: ".($n1+$n2)); else echo "Please enter numbers"; } ?> <form method = 'post'> Number: <input type = 'text' name = 'n1'><br> Number: <input type = 'text' name = 'n2'><br> <input type = 'submit' value = 'Add' name = 'add'> </form> </body> </html>
12. Write a PHP function, which accepts an array as an argument, and prints the first five elements of that array.
<!doctype html> <html> <head> <title>Year : 2019 (Makeup) | Q : 12</title> </head> <body> <?php function prtarr($arr) { $i = 0; echo "<br><br>First 5 elements of the array:<br>"; foreach($arr as $data) { if($i==5) break; echo " $data "; $i++; } } $a1 = array(1,2,3,300,44,99,9999,96,2000,444); $a2 = array('a'=>'abc','b'=>'abc'); prtarr($a1); prtarr($a2); ?> </body> </html>
13. Write a PHP program to print the following table. (Use array to store data)
Roll No | Name | Gender |
1 | Ram Thapa | M |
2 | Aruna Thapa | F |
<!doctype html> <html> <head> <title>Year : 2019 (Makeup) | Q : 13</title> </head> <body> <?php $info = array(array(1,'Ram Thapa','M'),array(2,'Aruna Thapa','F')); echo "<table border = '1' cellpadding = '15' cellspacing = '0'>"; echo "<tr> <th>Roll No</th> <th>Name</th> <th>Gender</th> </tr>"; foreach($info as $row) { echo "<tr align = 'center'>"; echo "<td>".$row[0]."</td>"; //Roll No. echo "<td>".$row[1]."</td>"; //Name echo "<td>".$row[2]."</td>"; //Gender echo "</tr>"; } echo "</table>"; ?> </body> </html>
14. Write a program to check if the textbox has a content or not, and if a checkbox is selected or not.
<!doctype html> <html> <head> <title>Year : 2019 (Makeup) | Q : 14</title> </head> <body> <form method = 'post'> Input : <input type = 'text' name = 'inp'><br> Select : <input type = 'checkbox' name = 'sel' value = 'sel'><br> <input type = 'submit' name = 'chk' value = 'Check'> </form> <?php if(isset($_POST['chk'])) { $I = $_POST['inp']; $S = ''; if(isset($_POST['sel'])) $S = $_POST['sel']; if(!($I==''||$S=='')) echo "Textbox is not empty, and the checkbox is selected."; else{ if($I=='') echo "Text box is empty.<br>"; if($S=='') echo "Checkbox is not selected."; } } ?> </body> </html>
15. Write a program to check if the textbox has a content or not, and if a checkbox is selected or not.
<!doctype html> <html> <head> <title>Year : 2019 (Makeup) | Q : 14</title> </head> <body> <form method = 'post'> Input : <input type = 'text' name = 'inp'><br> Select : <input type = 'checkbox' name = 'sel' value = 'sel'><br> <input type = 'submit' name = 'chk' value = 'Check'> </form> <?php if(isset($_POST['chk'])) { $I = $_POST['inp']; $S = ''; if(isset($_POST['sel'])) $S = $_POST['sel']; if(!($I==''||$S=='')) echo "Textbox is not empty, and the checkbox is selected."; else{ if($I=='') echo "Text box is empty.<br>"; if($S=='') echo "Checkbox is not selected."; } } ?> </body> </html>
16. Write a PHP program to read one line at a time from file and print them.
<!doctype html> <html> <head> <title>Year : 2019 (Makeup) | Q : 16</title> </head> <body> <?php echo "<h3>Contents of the file ('Dummy.txt'):</h3>"; $F = fopen('dummy.txt','r'); while(!feof($F)) echo fgets($F).'<br>'; fclose($F); ?> </body> </html>