11. Write a PHP program to create a form that contains two textboxes that accept numbers and a submit button. When submit button is clicked, the sum of two numbers will be displayed.

<!doctype html>
<html>
	<head>
        <title>Year : 2018 | 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 program to print the following List in an array and display them as follows:
a. Bachelor in Information Management
b. Bachelor in Business Administration
c. Bachelor in Business Studies

<!doctype html>
<html>
	<head>
        <title>Year : 2018 | Q : 12</title>
    </head>    
    <body>
		<?php
			$arr = array('Bachelor in Information Management','Bachelor in Business Administration','Bachelor in Business Studies');
			echo "<ol type = 'a'>";
			foreach($arr as $data)
				echo "<li>".$data."</li>";
			echo "</ol>";
		?> 
    </body>
</html>

13. Write a PHP program to find the difference between two dates.

<html>
	<head>
        <title>Year : 2018 | Q : 13</title>
    </head>    
    <body>
		<form method = 'post'>
        	Date 1 : <input type = 'text' name = 'd1' required><br>
			Date 2 : <input type = 'text' name = 'd2' required><br>
			<input type = 'submit' value = 'Calculate difference' name = 'diff'>
        </form>
        <?php
			if(isset($_POST['diff']))
			{
				$d1 = strtotime($_POST['d1']);
				$d2 = strtotime($_POST['d2']);
				$diff = abs($d1-$d2);
				echo "The difference between the given dates is of about ".floor($diff/(60*60*24))." days.";
			}
		?> 
    </body>
</html>

14. Write a PHP program to read name and address from a database, and display them.

<html>
    <head>
        <title>Year : 2018 | Q : 14</title>
    </head>
    <body>
 		<?php
			/*
				Database name: 'test'
				Table name: 'temp'
				Column names: 'name', 'address'
			*/
			$C = mysqli_connect('localhost','root','','test') or die("Error connecting with the database");	
			$code = "SELECT name, address FROM temp";
			$R = mysqli_query($C,$code) or die("Error in code");
			echo "<table border = '1' cellspacing= '0' cellspacing = '15'>";
			echo "<tr> <th>Name</th> <th>Address</th> </tr>";
			while($res = mysqli_fetch_array($R))
				echo "<tr align = 'center'> <td>".$res[0]."</td> <td>".$res[1]."</td> </tr>";		
			echo "</table>";
		?>
    </body>
</html>

15. Write a PHP program to copy content of one text file to another.

<html>
    <head>
        <title>Year : 2018 | Q : 15</title>
    </head>
    <body>
		<?php
			//Copying contents of 'txt1.txt' to 'txt2.txt'.
			//copy function: copy('txt1.txt','txt2.txt');
			$f1 = fopen('txt1.txt','r');
			$f2 = fopen('txt2.txt','w');
			while(!feof($f1))
			{
				if(!fwrite($f2,fgets($f1)))
					die("Error copying");
			}
			echo "Copied";
			fclose($f1);
			fclose($f2);
		?>    
    </body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *