Assignment / Lab Sheet / Project / Case Study No. 2
Assignment / Lab Sheet / Project / Case Study No. 2
Semester: Fourth
1.
a. With an example, explain the usage of htmlspecialchars() function.
➢ The htmlspecialchars() function converts special characters into HTML entities. It is
the in-built function of PHP, which converts all predefined characters to the HTML
entities. The predefined characters are:
For example:
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); // Does not convert any quotes
?>
b. Write PHP code to show differences between local and global variables.
➢ PHP code to show the differences between local and global variables are given below:
Local variable:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
OUTPUT:
Global variable:
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
OUTPUT:
2. For the figure given below:
a. Write required HTML and PHP code for the following figure:
<!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">
<title>Document</title>
</head>
<body>
<hr>
<div>
</tr>
<tr>
<td>Marks obtained</td>
<td> <input type="text"
Name="Subject1" value="Subject1">
<td>
<td> <input type="text"
Name="Subject2" value="Subject2">
<td>
<td> <input type="text"
Name="Subject3" value="Subject3">
<td>
</tr>
<tr>
<td> </td>
<td> <input type="submit"
name="submit"> </td>
</tr>
</table>
</form>
</div>
<hr>
</body>
</html>
b. For the fig .1 write required PHP code to collect form data and insert it into
database table, with following structure :
<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "rakshya_database";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
die('Could not Connect MySql Server:' .mysql_error());
}
?>
PHP code for inserting the data:
<?php
include_once 'database.php';
// $name=$Subject1=Subject2=Subject3="";
if(isset($_POST['submit']))
$name = $_POST['name'];
$Subject1= $_POST['Subject1'];
$Subject2= $_POST['Subject2'];
$Subject3 = $_POST['Subject3'];
VALUES ('$name','$Subject1','$Subject2','$Subject3')";
if (mysqli_query($conn, $sql)) {
} else {
mysqli_close($conn);
?>
Output:
c. Fetch the result from the table in fig. 2 and display the result as shown in figure
below: NOTE: use function to calculate average marks.
<html>
<body>
<?php
$conn=mysqli_connect("localhost","root","","rakshya_database");
if(mysqli_num_rows($result)>0)
{
?>
<?php
$i=0;
while($row=mysqli_fetch_array($result))
{
$i=$i+1;
?>
<tr>
<td>
<?php echo $row["name"]; ?>
</td>
<td>
<?php
$a=$row["Subject1"];
$b=$row["Subject2"];
$c=$row["Subject3"];
$avg=($a+$b+$c)/3;
echo $avg;
?>
</td>
</tr>
<?php } ?>
</table>
<?php } ?>
</fieldset>
</form>
</body>
</html>
Output: