PHP Assignment Part 2
PHP Assignment Part 2
PHP Assignment Part 2
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 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 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 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
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
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
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
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
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 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 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());
}
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 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