11. Write a program in PHP to add three numbers.

<!doctype html>
<html>
	<head>
        <title>Year : 2015 | Q : 11</title>
    </head>    
    <body>
        <?php
			$sum = 0;
			if(isset($_POST['add']))
				$sum = $_POST['n1']+$_POST['n2']+$_POST['n3'];	
		?>
        <form method = 'post'>
        	Number 1: <input type = 'number' name = 'n1' value = '0'><br>
            Number 2: <input type = 'number' name = 'n2' value = '0'><br>
            Number 3: <input type = 'number' name = 'n3' value = '0'><br>
			<input type = 'submit' name = 'add' value = 'Add'>
            <input type = 'number' name = 'res' value = '<?php echo $sum;?>' disabled> 
        </form>
    </body>
</html>

12. Write a program to display the first and last character of a string.

<!doctype html>
<html>
	<head>
        <title>Year : 2015 | Q : 12</title>
    </head>    
    <body>
        <?php
			$str = 'My name is something';
			echo "Given string: '".$str."'.<br>";
			echo "First character of the string is : '".substr($str,0,1)."', and the last character of the string is: '".substr($str,strlen($str)-1)."'.";	
		?>
    </body>
</html>

13. Create two integer arrays of size 5 each with data in them. Then display sum of all the numbers of both arrays.

<!doctype html>
<html>
	<head>
        <title>Year : 2015 | Q : 13</title>
    </head>    
    <body>
        <?php
			function sumarr($arr)
			{
				$sum = 0;
				foreach($arr as $data)
					$sum+=$data;
				return $sum;	
			}
			function disarr($arr)
			{
				foreach($arr as $data)
					echo " $data ";	
			}
			$a1 = array(5,10,15,20,25);
			$a2 = array(1,2,3,4,5);
			echo "<br>First array: ";
			disarr($a1);
			echo "<br>Sum of the first array: ".sumarr($a1);
			echo "<br>Second array: ";
			disarr($a2);
			echo "<br>Sum of the second array: ".sumarr($a2);	
		?>
    </body>
</html>

14. Write a program in PHP to store “coding in PHP” in a file named “coding.txt”.

<!doctype html>
<html>
	<head>
        <title>Year : 2015 | Q : 14</title>
    </head>    
    <body>
        <?php
			$str = 'coding in PHP';
			$f = fopen('coding.txt','w');			
			if(fwrite($f,$str))
				echo "Successfully wrote 'coding in PHP' in the file named 'coding.txt'.";
			else
				echo "Error in writing 'coding in PHP' in the file named 'coding.txt'.";
			fclose($f);
		?>
    </body>
</html>

15. Make the following table using PHP.

NameAgeGender
R2M
A20F
<!doctype html>
<html>
	<head>
        <title>Year : 2015 | Q : 15</title>
    	<style>
			td{
				width: 75px;
			}
        </style>
    </head>    
    <body>
        <?php
			$arr = array(array('R',21,'M'),array('A',20,'F'));
			echo "<table border = '1' cellspacing = '0' cellpadding = '10'>";
			echo "<tr align = 'left'> <td>Name</td> <td>Age</td> <td>Gender</td> </tr>";
			foreach($arr as $data)
			{
				echo "<tr align = 'left'>";
				echo "<td>".$data[0]."</td>";//Name
				echo "<td>".$data[1]."</td>";//Age
				echo "<td>".$data[2]."</td>";//Gender
				echo "</tr>";			
			}
		?>
    </body>
</html>

Leave a Reply

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