PHP Assignment Part 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

ASSIGNMENT-1

QUESTION: Create a Webpage that displays your Name, Phoneand Email,


your academic details, your technical skills and other information.
Ans:-
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body style = "background-color:lightblue;background-image: linear-
gradient(lightblue,green);">
<h1 align = 'center'>Suman Basu </h1>
<p align = 'center'>sumanbasu1997@gmail.com <br>+91-8984137497</p>
<p><b><font size = 5px>Academic Details</font size></b></p>
<table>
<tr>
<th>Institute/College Name</th>
<th>Course</th>
<th>Year</th>
<th>Peercentage/CGPA</th>
</tr>
<tr>
<td>Institute of Technical Education and Research</td>
<td>B.Tech CSIT</td>
<td>2015-2019</td>
<td>7.68 CGPA(Upto 6th Semester)</td>
</tr>
<tr>
<td>Xavier International </td>
<td>XIIth CBSE</td>
<td>2015</td>
<td>72.6%</td>
</tr>
<tr>
<td>St.Josephs School</td>
<td>Xth ICSE</td>
<td>2013</td>
<td>66.8% </td>
</tr>
</table>
<p><b><font size = 5px>Technical Skills</font size></b></p>
<ul>
<li>Core JAVA</li>
<li>Advanced JAVA</li>
<li>C</li>
<li>C++ </li>
<li>MATLAB</li>
<p><b><font size = 5px>Academic Projects</font size></b></p>
<ul>
<li>women safety app </li>
<li>Library Management System </li>
<li>Grocerry Management System</li>
</ul>
</body>
ASSIGNMENT-2

QUESTION 1:Create a PHP webpage and print hello message.


Ans:-
<?php
echo "Hello World";
?>
Output:-
Hello World
QUESTION 2:Create a PHP webpage and print your name, registration no,
branch, section in four lines.
Ans:-
<?php
echo "My Name is Suman Basu.<br>";
echo "My registration number is 1541017045.<br>";
echo "My branch is Computer Science and Information Technology.<br>";
echo "My Section is CSIT-B.<br>";
?>
Output:-
My Name is Suman Basu.
My registration number is 1541017045.
My branch is Computer Science and Information Technology.
My Section is CSIT-B.

QUESTION 3:Create a PHP program to add two numbers and display the
result with message.
Ans:-
<?php
$num1 = 42;
$num2 = 56;
$sum = $num1+$num2;
echo "The sum of $num1 and $num2 is $sum.<br>";
?>
Output:-
The sum of 42 and 56 is 98.

QUESTION 4:Create a PHP program to add, subtract multiply and divide


two numbers and display the result with message.
Ans:-
<?php
$num1 = 42;
$num2 = 7;
$sum = $num1 + $num2;
$diff = $num1 - $num2;
$mul = $num1 * $num2;
$div = $num1 / $num2;
echo "The sum of $num1 and $num2 is $sum.<br>";
echo "The difference of $num1 and $num2 is $diff.<br>";
echo "The multiplication of $num1 and $num2 is $mul.<br>";
echo "The division of $num1 and $num2 is $div.<br>";
?>
Output:-
The sum of 42 and 7 is 49.
The difference of 42 and 7 is 35.
The multiplication of 42 and 7 is 294.
The division of 42 and 7 is 6.

QUESTION 5:Create a PHP program to check whether a given number is


even or odd.
Ans:-
<?php
$x=21;
$temp=($x % 2==0)? "EVEN":"ODD";
echo $temp;
?>
Output:-
ODD

QUESTION 6: Create a PHP program to find the greatest number among


three numbers.
Ans:-
<?php
$a=20;
$b=22;
$c=112;
$max=($a>$b)?(($a>$c)?$a:$c):(($b>$c)?$b:$c);
echo $max;
?>
Output:-
112

QUESTION 7:Create a PHP program to display date in different formats


Ans:-
<?php
echo date("j-F-Y")."<br>";
echo date("F j, Y, g:i a")."<br>";
echo date("m.d.y")."<br>";
echo date("j, n, Y")."<br>";
echo date('\i\t \i\s \t\h\e jS \d\a\y.')."<br>";
echo date("D M j G:i:s T Y")."<br>";
echo date("H:i:s")."<br>";
?>
Output:-
7-April-2019
April 7, 2019, 10:49 pm
04.07.19
7, 4, 2019
it is the 7th day.
Sun Apr 7 22:49:29 CEST 2019
22:49:29

QUESTION 8:Create a PHP program to display date along with message


"todays date is..".
Ans:-
<?php
echo "Today’s date is ".date("Y/M/D")."</br>"
?>
Output:-
Today’s date is 2019/Apr/Sun

QUESTION 9:Write a PHP program to swap two numbers.


Ans:-
<?php
$a=23;
$b=90;
echo "a is equal to ".$a." b is equal to ".$b;
echo "<br>"."AFTER SWAPPPING";
$a=$a+$b;
$b=$a-$b;
$a=$a-$b;
echo "<br>"."a is equal to ".$a." b is equal to ".$b;
?>
Output:-
a is equal to 23 b is equal to 90
AFTER SWAPPPING
a is equal to 90 b is equal to 23

QUESTION 10: Write a PHP program to compare two numbers and display
the message of equality or bigger or smaller.
Ans:-
<?php
$x=99;
$y=99;
if($x==$y)
echo $x." Equal to ".$y;
else if($x>$y)
echo $x." bigger than ".$y;
else
echo $x." lesser than ".$y;
?>
Output:-
99 Equal to 99
ASSIGNMENT-3

QUESTION 1:Write a PHP program to print the odd numbers upto 99 using
while statement
Ans:-
<?php
$i=1;
while($i<=99)
{
echo $i." ";
$i+=2;
}
?>
Output:-
1 3 … 99

QUESTION 2:Write a PHP program to print the prime numbers upto 99


using while statement
Ans:-
<?php
$i=2;
function checkprime($n)
{
$count=2;
while($count<$n)
{
if($n%$count==0)
{
return 1;
}
$count++;
}
return -1;
}
while($i<=99)
{
if(checkprime($i)==-1)
echo $i." ";
$i++;
}
?>
Output:-
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

QUESTION 3:Write a PHP program to print the Fibonacci series 1,


1,2,3,5..up to 100 using do while.
Ans:-
<?php
$a=1;
$b=1;
do
{
if($b<=99)
echo $a." ".$b." ";
else
echo $a." ";
$a=$a+$b;
$b=$a+$b;
}while($a<99);
?>
Output:-
1 1 2 3 5 8 13 21 34 55 89
QUESTION 4:Write a PHP program to know the datatype of a given value
using switch case.
Ans:-
<?php
$x="Apple";
switch(gettype($x))
{
case "double": echo "DOUBLE";
break;
case "integer": echo "INTEGER";
break;
case "float": echo "FLOAT";
break;
case "string": echo "STRING";
break;
default: echo "hi";
break;
}
?>
Output:-
STRING

QUESTION 5:Write a php program to find the area of a circle of radius 5cm
using constants
Ans:-
<?php
define("PI", 3.14);
$r=5;
$area=PI*$r*$r;
echo $area;
?>
Output:-
78.5

QUESTION 6:Create an indexed array of 10 elements(Student name) and


search whether a given name exist in the array.
Ans:-
<?php
$i=$flag=0;
$arr=array("suman","ashrita","gayatri","malayaka","abhishek","monika","avinash","sur
bhi","aditya","ankur");
while($i<count($arr))
{
if($arr[$i++]=="malayaka ")
$flag=1;;
}
if($flag)
echo "ELEMENT FOUND";
else
echo "ELEMENT NOT FOUND";
?>
Output:-
ELEMENT FOUND

QUESTION 7:Create an array of number elements and sort it.


Ans:-
<?php
$arr = array(4, 6, 2, 22, 11, 1);
$arrlength = count($arr);
for($x = 0; $x < $arrlength; $x++)
{
for($y = 1; $y < $arrlength-$x; $y++)
{
if($arr[$y]<$arr[$y-1])
{
$temp=$arr[$y];
$arr[$y]=$arr[$y-1];
$arr[$y-1]=$temp;
}
}
}
for($x = 0; $x < $arrlength; $x++) {
echo $arr[$x]." ";
}
?>
Output:-
1 2 4 6 11 22

QUESTION 8: Write a function to calculate the factorial of a given number


(6).
Ans:-
<?php
function fact($n)
{
$fact=1;
while($n>0)
{
$fact=$fact*$n--;
}
return $fact;
}
echo fact(6);
?>
Output:-
720
QUESTION 9:Write a PHP program to reverse a string without using
strrev().
Ans:-
<?php
$string="Suman Basu ";
$strlen = strlen($string);
for($x=$strlen-1; $x>=0; $x--)
{
echo $string[$x];
}
?>
Output:-
usab namus
QUESTION 10:Write a PHPscript to that checks whether a given string is a
palindrome or not? (madam)
Ans:-
<?php
$string="madam";
if(strcmp($string,strrev($string))==0)
echo "PALINDROME";
else
echo "NOT PALINDROME";
?>
Output:-
PALINDROME

QUESTION 11:Create a php script using a for loop to add all the integers
between 0 and 50 and display the total.
Ans:-
<?php
$sum=0;
for($i=0; $i<=50; $i++)
{
$sum+=$i;
}
echo $sum;
?>
Output:-
1275

QUESTION 12:Create a script to construct the following pattern, using


nested for loop.
*
**
***
****
*****

Ans:-
<?php
for($i=1; $i<=5; $i++)
{
for($j=1; $j<=$i; $j++)
{
echo "* ";
}
echo "<br>";
}
?>
Output:-
*
**
***
****
*****

QUESTION 13:Write a php program which will count the "r" characters in
the text "irregularities"
Ans:-
<?php
$count=0;
$string = "irregularities";
for($x=0; $x<strlen($string); $x++)
if($string[$x]=="r")
$count++;
echo $count;
?>
Output:-
3

QUESTION 14:Display all the leap years from 2004 to 2050.


Ans:-
<?php
$year = 2004;
for($year = 2004; $year <= 2050; $year++)
{
if( ($year % 4 == 0) and ($year % 100 != 0) or ($year % 400 == 0) )
echo "$year<br>";
}
?>
Output:-
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

QUESTION 15:Sum of digits of a number 21345.


Ans:-
<?php
$sum=0;
$num = 21345;
while($num>0)
{
$sum+=$num%10;
$num/=10;
}
echo $sum;
?>
Output:-
15
ASSIGNMENT-4
QUESTION 1:Write a PHP program using function to check for armstrong
number, perfect number and palindrome number.
Ans:-
<?php
functionarmstrongCheck($number){
$sum = 0;
$x = $number;
while($x != 0)
{
$rem = $x % 10;
$sum = $sum + $rem*$rem*$rem;
$x = $x / 10;
}
if ($number == $sum)
return 1;

return 0;
}
functionperfectCheck($number){
$sum = 0;
for ($i = 1; $i < $number; $i++)
{
if ($number % $i == 0)
{
$sum = $sum + $i;
}
}
return $sum == $number;
}
functionpalindromeCheck($number){
$temp = $number;
$new = 0;
while (floor($temp)) {
$d = $temp % 10;
$new = $new * 10 + $d;
$temp = $temp/10;
}
if ($new == $number){
return 1;
}
else{
return 0;
}
}

$number = 121;
$arm = armstrongCheck($number);
if ($arm == 1)
echo "ARMSTRONG NUMBER. <br>";
else
echo "NOT ARMSTRONG NUMBER. <br>";
if (perfectCheck($number))
echo " PERFECT NUMBER. <br>";
else
echo "NOT PERFECT NUMBER. <br>";
if (palindromeCheck($number)){
echo "Palindrome";
}
else {
echo "Not a Palindrome";
}
?>
Output:-
NOT ARMSTRONG NUMBER.
Not Perfect Number.
Palindrome

QUESTION 2:Declare a global array (100, 105, 102, 106, 104, 101, and 103)
and display using function all the elements in a sorted way.
Ans:-
<?php
global $arr;
function display($arr)
{
ort($arr);
$l = count($arr);
for($i = 0; $i < $l; $i++)
{
echo $arr[$i];
echo "<br>";
}
}
$arr = array(100, 105, 102, 106, 104, 101, 103);
display($arr);
?>
Output:-
100
101
102
103
104
105
106

QUESTION 3:Write a program and remove duplicate values from an array


using function.
Ans:-
<?php
functiondup_removal($arr)
{
$result = array_unique($arr);
print_r($result);
}
$arr = array(1,1,1,2,2,3,3,3,56,76,87);
dup_removal($arr);
?>
Output:-
Array ( [0] => 1 [3] => 2 [5] => 3 [8] => 56 [9] => 76 [10] => 87 )

QUESTION 4:Write the PHP script to display


Ram 9.5

Mohan 8.0

Hari 8.5

Harish 9.0

Ans:-
<?php
$arr = array("Ram"=>"9.5", "Mohan"=>"8.0", "Hari"=>"8.5", "Harish"=>"9.0");
foreach($arr as $i=>$value)
{
echo "".$i, "\t".$value;
echo "<br>";
}
?>
Output:-
Ram 9.5
Mohan 8.0
Hari 8.5
Harish 9.0

QUESTION 5:Using function write a PHP program to count the number of


prime numbers upto 100 and display them.
Ans:-
<?php

for ( $i=1;$i<=$number;$i++)
{
if (($number%$i)==0)
{
$flag++;
}
}
if ($flag == 2)
{
$count++;
}
$number=$number+1;
}
echo $count;
}
$number = 2 ;
displayPrime($number);
?>
Output:-
25

QUESTION 6:Write a PHP program to check whether a given mail id is valid


or not.
Ans:-
<?php
$email = "phpa4q6@gmail.com";
if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-
z]{2,3})$^",$email))
echo "Invalid email";
else
echo "Valid Email";
?>
Output:-
Valid Email

QUESTION 7:Write a PHP program to count the number of movie in each


category as given below.
Ans:-
<?php
$category = array("Dhamaal"=>"Comedy", "BhagamBhag"=>"Comedy",
"Singham"=>"Action", "Sholay"=>"Action", "Padmavati"=>"Epic", "Jab
TakHainJaan"=>"Romance", "Hera Pheri"=>"Comedy");
$a = 0;
$c = 0;
$e = 0;
$r = 0;
foreach($category as $i=>$value)
{
echo "".$i, "\t".$value;
echo "<br>";
if($value == "Action")
$a++;
else if($value == "Comedy")
$c++;
else if($value == "Epic")
$e++;
else
$r++;
}
echo "Action Movies are ".$a;
echo "<br>Comedy Movies are ".$c;
echo "<br>Epic Movies are ".$e;
echo "<br>Romance Movies are ".$r;
?>

Output:-
Dhamaal Comedy
BhagamBhag Comedy
Singham Action
Sholay Action
Padmavati Epic
Jab TakHainJaan Romance
Hera Pheri Comedy
Action Movies are 2
Comedy Movies are 3
Epic Movies are 1
Romance Movies are 1
QUESTION 8:Write a program to display the multiplication table of 9
multiplied by 0 to 10 using for loop.
Ans:-
<?php
$number = 9;
$upto = 10;
for($i = 0; $i <= $upto; $i++)
{
echo "$number * $i = ".$number*$i.".<br>";
}
?>
Output:-
9 * 0 = 0.
9 * 1 = 9.
9 * 2 = 18.
9 * 3 = 27.
9 * 4 = 36.
9 * 5 = 45.
9 * 6 = 54.
9 * 7 = 63.
9 * 8 = 72.
9 * 9 = 81.
9 * 10 = 90.
QUESTION 9:Write a program to generate a random number between 1 to 9
and display it in words using switch case.
Ans:-
<?php
$r = rand(1,9);
switch($r)
{
case 1:
echo "One";
break;
case 2:
echo "Two";
break;
case 3:
echo "Three";
break;
case 4:
echo "Four";
break;
case 5:
echo "Five";
break;
case 6:
echo "Six";
break;
case 7:
echo "Seven";
break;
case 8:
echo "Eight";
break;
case 9:
echo "Nine";
break;
default:
echo "Out of Bounds";
}
?>
Output:-
Six
QUESTION 10:Design a registration form to accept Student name,
registration number, branch, semester and email id with submit button and
display it.

Ans:-
<html>
<head></head>
<body>
<?php
// define variables and set to empty values
$name = $email = $regno = $branch = $sem =
"";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$regno = test_input($_POST["regno"]);
$branch = test_input($_POST["branch"]);
$sem = test_input($_POST["sem"]);
}
functiontest_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;}
?>
<h2>Student Registration Form</h2>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<br><br>
Registration Number: <input type="text" name="regno">
<br><br>
Branch: <input type="text" name="branch">
<br><br>
Semester: <input type="text" name="sem">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $regno;
echo "<br>";
echo $branch;
echo "<br>";
echo $sem;
echo "<br>";
echo $email;
?>
</body>
</html>
Output:-
Student Registration Form
Name:

Registration Number:

Branch:

Semester:

E-mail:

Your Input:
Suman Basu
1541017045
CSIT
8th
sumanbasu1997@gmail.com
ASSIGNMENT-5
QUESTION 1:Calculate the difference between two dates, i.e calculate your
age.
Ans:-
<?php
$date1 = date("Y-m-d");
$date2 = date_create("1996-09-27");
$diff = date_diff($date2, $date1);
echo "The Difference between two dates is : ".$diff;
?>
QUESTION 2:Write a simple PHP class which displays any string of 4
lines.Ans:-
<?php
class Demo
{
var $name;
function Demo($name)
{
$this->name=$name;
}
function show()
{
echo nl2br($this ->name);
}
}
$obj=new Demo("This is\nPHP\nAssgn 5\nQuestion 2.");
$obj->show();
?>
Output:-
This is
PHP
Assgn 5
Question 2.

QUESTION 3:Write a PHP class calculates the factorial of an integer.


Ans:-
<?php
class factorial
{
protected $n;
public function __construct($n)
{
$this->n = $n;
}
public function result()
{
$factorial = 1;
for ($i = 1; $i <= $this->n; $i++)
{
$factorial *= $i;
}
return $factorial;
}
}
$newfactorial = new factorial(5);
echo $newfactorial->result();
?>
Output:-
120

QUESTION 4:Write a PHP class that sorts an ordered integer array with the
help of sort() function.
Ans:-
<?php
classarray_sort
{
protected $_arr;
public function __construct(array $arr)
{
$this->_arr = $arr;
}
public function alhsort()
{
$sorted = $this->_arr;
sort($sorted);
return $sorted;
}
}
$sortarray = new array_sort(array(11, -2, 4, 35, 0, 8, -9));
print_r($sortarray->alhsort())."\n";
?>
Output:-
Array ( [0] => -9 [1] => -2 [2] => 0 [3] => 4 [4] => 8 [5] => 11 [6] => 35 )

QUESTION 5:Write a PHP Calculator class which will accept two values as
arguments, then add them, subtract them, multiply them together, or divide
them on request.
Ans:-
<?php
classMyCalculator {
private $_fval, $_sval;
public function __construct( $fval, $sval )
{
$this->_fval = $fval;
$this->_sval = $sval;
}
public function add() {
return $this->_fval + $this->_sval;
}
public function subtract() {
return $this->_fval - $this->_sval;
}
public function multiply() {
return $this->_fval * $this->_sval;
}
public function divide() {
return $this->_fval / $this->_sval;
}
}
$mycalc = new MyCalculator(12, 6);
echo $mycalc-> add()."<br>";
echo $mycalc-> multiply()."<br>";
echo $mycalc-> subtract()."<br>";
echo $mycalc-> divide()."<br>";
?>
Output:-
18
72
6
2

QUESTION 6:WAP of PHP to define a class of shape and inherit from this
class to find area of rectangle, circle and triangle.
Ans:-
<?php
class Shape
{
const pi = 3.1415;
}
class Circle extends Shape
{
public $rad;
public function __construct( $rad )
{
$this->rad = $rad;
}
public function area()
{
return Shape::pi*$this->rad*$this->rad;
}
}
class Rectangle extends Shape
{
public $x;
public $y;
public function __construct( $x, $y )
{
$this->x = $x;
$this->y = $y;
}
public function area()
{
return $this->x*$this->y;
}
}
class Triangle extends Shape
{
public $b;
public $h;
public function __construct( $b, $h )
{
$this->b = $b;
$this->h = $h;
}
public function area()
{
return 0.5*$this->b*$this->h;
}
}
$c = new Circle(2);
echo "Area of Circle is: ".$c->area()."<br>";
$r = new Rectangle(4,5);
echo "Area of Rectangle is: ".$r->area()."<br>";
$t = new Triangle(10,5);
echo "Area of Triangle is: ".$t->area()."<br>";
?>
Output:-
Area of Circle is: 12.566
Area of Rectangle is: 20
Area of Triangle is: 25

QUESTION 7:WAP to define an abstract class vehicle with some members


and methods. Declare one abstract method to determine no of wheels and
implement in child class.
Ans:-
<?php
abstract class Vehicle
{
abstract public function getWheels();
public function printWheels()
{
echo "No. of wheels are : ".$this->getWheels();
}
}
class Car extends Vehicle
{
public function getWheels()
{
return 4;
}
}
$c = new Car();
$c->printWheels();
?>
Output:-
No. of wheels are : 4

QUESTION 8:WAP to define any constant in class and show it.


Ans:-
<?php
class Cons
{
const PHP = "Assignment 6";
}
echo "The Assignment Number is " . Cons::PHP;
?>
Output:-
The Assignment Number is Assignment 6

QUESTION 9:WAP to implement polymorphism with method overriding for


any message displaying.
Ans:-
<?php
classpclass
{
public $p_val;
public function __construct( $p_val )
{
$this->p_val = $p_val;
}
functionshowval()
{
echo "PClass, We are in ".$this->p_val." class.";
}
}
classcclass extends pclass
{
public $c_val;
public function __construct( $c_val )
{
$this->c_val = $c_val;
}
functionshowval()
{
echo "CClass, We are in ".$this->c_val." class.";
}
}
$cobj = new cclass("child");
$cobj->showval();
?>
Output:-
CClass, We are in child class.
QUESTION 10:WAP to define two interfaces and implement the methods in
derived class.
Ans:-
<?php
interface Car
{
public function setModel($name);
}
interface Vehicle
{
public function setWheels($wheel);
}
classMiniCar implements Car, Vehicle
{
private $model;
private $hasWheels;
public function setModel($name)
{
$this->model = $name;
echo $this->model."<br>";
}
public function setWheels($wheel)
{
$this->hasWheels = $wheel;
echo $this->hasWheels." wheels<br>";
}
}
$obj = new MiniCar();
$obj->setModel("Nano");
$obj->setWheels(4);
?>
Output:-
Nano
4 wheels

QUESTION 11:Write a program to declare an object as string.


Ans:-
<?php
classStr
{
public function __toString()
{
return __CLASS__;
}
}
echostrval(new Str());
?>
Output:-
Str

QUESTION 12:Define a variable static in the class and display its value.
Ans:-
<?php
class Demo
{
static $val = 100;
}
echo "The static variable value is :".Demo::$val;
?>
Output:-
The static variable value is :100

QUESTION 13:Create one class file and then invoke it in another file using
include_once() and __autoload().
Ans:-
myclass.php:-
<?php
classMyClass
{
public function __construct()
{
echo "Done Successfully By Me.";
}
}
?>

index.php:-
<?php
function __autoload($class_name)
{
$filename = "./".$class_name.".php";
include_once($filename);
}
$obj = new MyClass();
?>
Output:-
Done Successfully By Me.
QUESTION 14:Declare the class as final and define two methods. derive the
class and create an object. Show the message.
Ans:-
<?php
final class Base
{
public function show()
{
echo "Base Class Called.";
}
}
class Child extends Base
{
public function show()
{
echo "Child Class Called.";
}
}
$c = new Child();
$c->show();
?>
Error: - Compile Time Error since final class cannot be inherited.
QUESTION 15:Create any method in a class as final and try to override it.
Ans:-
<?php
class Base
{
final public function show()
{
echo "Base Class Called.";
}
}
class Child extends Base
{
public function show()
{
echo "Child Class Called.";
}
}
$c = new Child();
$c->show();
?>
Error: - Compile Time Error since final method cannot be overridden.

ASSIGNMENT-6
QUESTION 1:WAP to create a database named Student.
Ans:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "CREATE DATABASE Student";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Output:-
Database created successfully

QUESTION 2:WAP to create a table stdinfo in Student with id, name, and
branch as fields.
Ans:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "CREATE TABLE stdinfo (


id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
branch VARCHAR(30) NOT NULL
)";
if (mysqli_query($conn, $sql)) {
echo "Table stdinfo created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Output:-
Table stdinfo created successfully

QUESTION 3:WAP to insert one record into stdinfo.

Ans:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO stdinfo (id, name, branch)
VALUES (1541017045, 'Suman', 'CSIT')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Output:-
New record created successfully

QUESTION 4:WAP to modify the name with a new value.


Ans:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "ALTER TABLE stdinfo Change name snameVARCHAR(30)";
if (mysqli_query($conn, $sql)) {
echo "Column Name changed successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Output:-
Column Name changed successfully

QUESTION 5:WAP to display in a table the values.


Ans:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, sname, branch FROM stdinfo";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_assoc($result);
printf ("%s %s %s\n",$row["id"],$row["sname"],$row["branch"]);
mysqli_free_result($result);
mysqli_close($conn);
?>
Output:-
1541017068 Suman CSIT

QUESTION 6:WAP to drop the table.


Ans:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Student";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "DROP TABLE stdinfo";
if (mysqli_query($conn, $sql)) {
echo "Table Dropped successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Output:-
Table Dropped successfully
Screenshot of the Database:-

QUESTION 7:WAP to create text file and write and update it using PHP
program.
Ans:-
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
newfile.txt:-
Mickey Mouse
Minnie Mouse

You might also like