PHP || old paper solutions 2015

4th semester

 

year 2015

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

<body>
<form action="" method="post">
<label>First Number</label>
<input type="number" name="first_num" /> <br />
<label>Second Number</label>
<input type="number" name="second_num" /> <br />
<label>Third Number</label>
<input type="number" name="third_num" />
<input type="submit" name="" value="Submit" />
</form>
</body>
<?php
if($_POST){
$first_num = $_POST['first_num'];
$second_num = $_POST['second_num'];
$third_num = $_POST['third_num'];
$sum = $first_num + $second_num + $third_num;
echo 'Sum: '.$sum;
}
?>




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

<body>
<form action="" method="post">
<label>Enter a string</label>
<input type="text" name="text" />
<input type="submit" name="" value="submit" />
</form>
</body>
<?php
if($_POST){
$string = trim($_POST['text']);
$length = strlen($string);
for($i = ($length - 1); $i >= 0; $i--){
if($i == ($length-1)){
echo 'Last character is: '.$string[$i];
}
if($i == 0){
echo '<br />First character is: '.$string[$i];
}
}
}
?>

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

<?php
$first_array = array(2, 4, 6, 8, 10);
$second_array = array(3, 5, 7, 9, 11);
$result_array = array();
for($i = 0; $i < sizeof($first_array); $i++){
$result_array[$i] = $first_array[$i] + $second_array[$i];
}
echo "<pre>";
print_r($result_array);
echo 'Sum of two arrays is: '.array_sum($result_array);
?>

 





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

<?php
$text = 'coding in PHP';
$myfile = fopen('coding.txt', 'w') or die('Cannot open file');
fwrite($myfile, $text);
fclose($myfile);
echo 'Content written to the file';
?>

 
16.

<table border="1" style="border-collapse: collapse; width: 250px">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr>
<th>R</th>
<th>21</th>
<th>M</th>
</tr>
<tr>
<th>A</th>
<th>20</th>
<th>F</th>
</tr>
</tbody>
</table><input id="6251.5200704804351" class="cdbx-try-code-web cdbx-btn-main" style="background-color: #ffffff; margin-bottom: 0; color: #008b8b; border: 1px solid rgba(231, 231, 230, 1); border-radius: 10px; font-size: 13px; height: 30px; min-width: 110px; max-width: 220px; padding: 4px; font-weight: normal; outline: none; display: none; float: right;" type="button" value="Run HTML" data-lang="html" data-code="1534084070025.1526" data-mce-style="background-color: #ffffff; margin-bottom: 0; color: #008b8b; border: 1px solid rgba(231, 231, 230, 1); border-radius: 10px; font-size: 13px; height: 30px; min-width: 110px; max-width: 220px; padding: 4px; font-weight: normal; outline: none; display: none; float: right;">

Leave a Reply

Your email address will not be published.