0% found this document useful (0 votes)
15 views15 pages

Practical Questions Answers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

Practical Questions Answers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1.Write a PHP script to accept website URL.

If data entered by the user is valid then display it on

the page otherwise display the appropriate message. (use filter_var)

2.VWritea script to keep track of a number of times the web page has been accessed using
the Session.

3. Consider the following entities and their relationships

Movie(m_no, m_name, releaseyear,actor name)

Display all movies and released year acted by any actor or actress in tabular format

4.Write a PHP script to check mobile number entered by the user is valid or not and display
an appropriate message.

5.Write a PHP Script to create a class Book (Book_id, Book_name, Publication, Book_price)
and Author (aid,auth name) is child class Write a member function to accept and display
Book and author details.

6.Write PHP program to perform the following operations on Indexed Array:

a) Check the array element is positive or negative


b) Calculate the sum of array elements

7.Write a PHP Script to accept two numbers from user which will perform the Addition,
Subtraction, Multiplication, and Division of two numbers as per the choice. (Use Switch
Case and radio button )

8.Write a PHP Script to define interface and calculate the area of Circle, Square, and
Rectangle display result
9.Write a PHP script to accept customer name and the list of product and quantity on the
first page. On the next page display the name of the customer, name of the products, rate
of the product, quantity, and total price in table format.

10.Write a PHP script to accept string from user and check is it palindrome or not?use self
processing form and string function

11.Write a PHP Script to create a class Fruit that contains constructor to accept data
members as Name, Color and Price. Write a member function to accept and display details
of Fruit.

12.Write a PHP script to convert temperature Fahrenheit to celsious use sticky forms to
accept temperature

1. PHP Script to Validate a Website URL using


£ilter var()

<?php
If ($ SERVER["REQUEST METHOD" ] == "POST") {

= $
POST['url'1;
$url

If (filter var ($url,


FILTER VALIDATE URL)) {

Echo Valid URI: $url;


{
}else
Echo "Invalid URL!";
}

?>
<form method="post">
Enter website URL: <input type="text"
name="url">
<input type="submit" value="Submit">
</form>

2. PHP Script to Track Number of Times the Web


Page has been Accessed Using a Session

<?php
Session start () ;

If (isset ($ SESSION [ 'visit count'])) {

$ SESSION [ 'visit count' ]++;


} {
else
$
SESSION 'visit count| = 1;
}

Echo This page has been accessed


.
$ SESSION [
'visit count' ] times,";
?>

3. PHP Script to Display Movies and Release


Year

<?php
= [
$movies
['m no' => 1, 'm name' => Inception'
'releaseyear' => 2010, 'actor' => Leonardo
DiCaprio'],
[ 'm no => 2, 'm name' => The Matrix',
*releaseyear' => 1999, 'actor' => Keanu
ReevesJ,
[ 'm no => 3, 'm name => Titanic',
'releaseyear' => 1997, actor! => Leonardo
DiCaprio']

Echo "<table border=1>";


Echo "<tr><th>Movie Name</th><th>Release
Year</th><th>Actor</th></tr>";
Foreach (movies as movie) {

Echo
"<trs<td>{$movie m name'] }</td><td>{$movie['rel
[
easeyear'l}</td><td>{ Smovie [ 'actor' l }</td></tr>"

Echo "</table>";
?>

4. PHP Script to Validate Mobile Number

<?php
If ($ SERVER[ "REQUEST METHOD" ] "POST") {

= POST [ 'mobile'];
mobile $
If (preg match ('/^[6-9] [0-9] {9}$/',
) {
mobile)
Echo "Valid Mobile Number";
}
else {
Echo "Invalid Mobile Number" ;
}

?>

<form method="post">
Enter Mobile Number: <input type="text"
name="mobile">
<input type="submit" value="Submit">
</form>

5. PHP Script for Book and Author Class

<?php
{
Class Book
Public book id, $book name, $publication,
$book price;

Function Construct ($book id, $book name,


{
$publication, $book price)
$this->book id = book id;
$this->book name = $bocok name;
$this->publication = $publication;

$this->book price $book price;


Function display Details () {
Echo "Book ID: $this->book id <br> Book
Name: $this->book name <br> Publication: $this
>publication <br> Book Price: $this->book price
<br>";
}

{
Class Author extends Book
Public $author id, $author name;

Function Construct ($book id, $book name,


$publication, $book price, $author id,
$author name)
Parent:: Construct id,
($book
$book name, $publication, $book price) ;
$this->author id = $author id;
$this->author name = $author name;
}

Function displayAuthorDetails () {

$this->displayDetails () ;
Echo "Author ID: $this->author id <br>
Author Name: $this->author name <br>";
}

,
$book = new Author (1, PHP Basics', Pearson'
500, 101, 'John Doe'):
$book->displayAuthorDetails ();
?>

6. PHP Script to Perform Operations on Indexed


Array

<?php
$array = [10, -20, 30, -40, 50]:

// Check if elements are positive or negative


Foreach ($array as $element) {

If ($element >= 0) {
Echo "$element is Positive<br>";
}
else {
Echo "$element is Negative<br>";
}

// Calculate the sum of array elements


$sum= array sum($array) ;

Echo Sum of Array Elements: $sum";


?>

7. PHP Script for Arithmetic Operations with


Switch Case
<?php
If ($ SERVER["REQUEST METHOD"] == "POST") {

$numl = § POST [ 'num 1' ];


$num2 = $ POST | 'num2 ]:
$operation = $ POST | 'operation];

{
Switch ($operation)
Case "add":
+ ;
Echo "Result: ($num1 num2)
Break;
Case "subtract":
Echo "Result: ($num1 $num2);
Break;
Case multiply":
*
Echo "Result: ($numl $num2);
Break;
Case divide":
;
Echo "Result: ($numl / $num2)
Break;

?>

<form method="post">
Enter First Number: <input type="number"
name="num1"><br>
Enter Second Number: <input type="number"
name="num2 "><br
<input type="radio" name="operation"
value="add">Addition
<input type="radio" name="operation"
value="subtract">Subtraction
<input type-" radio" name-"operation"
value="multiply">Multiplication
<input type="radio" name="operation"
value="divide">Division<br
<input type="submit" value="Submit">
</form>

8. PHP Script to Define Interface and Calculate


the Area of Circle, Square, and Rectangle

<?php
interface Shape {

public function area ();


}

{
class Circle implements Shape
private $radius;

public function construct ($radius) {

$this->radius = $radius;
}

public function area () {

return pi() * $this->radius * $this


>radius;
}
class Square implements Shape {

private $side;

public function construct ($side) {

$this->side = $side;

public function area ()


return $this->side * $this->side;

{
class Rectangle implements Shape
private $length, $width;

public function construct ($length, $width)

$this->length = $length;
$this->width = $width;
}

public function area (){


returrn $this->length * $this->width;
}

$circle = new Circle (5);


echo "Area of Circle: ". $circle->area ()
"<br>";
$square
=
new Square (4) ;
echo "Area of Square: $square->area()
"<br>";

$rectangle = new Rectangle (4, 6);


echo "Area of Rectangle: $rectangle->area ()
"<br>";
?>

9. PHP Script to Accept Customer Name and


Product List and Display Details in Table

<?php
if ($ SERVER["REQUEST METHOD"] == "POST") {

$Customer name = $ POST [' customer name'];


$products = $ POST('product'] ;

$quantities = $ POST ['quantity '];


=
$rates [100, 200, 150]; /Example rates
for products

echo "Customer Name: $customer name<br>";


echo "<table
border=1'><tr><th>Product</th><th>Quantity</th>
<th>Rate</th><th>Total Price</th></trs";

$total = 0;

for ($i = 0; $i < count ($products) ; $i++) {


$product = $products [$i];
$quantity = $quantities ($i];
$rate = $rates [$i];
$price = $quantity * $rate;
$total += $price;

echo
"<tr><td>$product</td><td>$quantity</td><td>$rat
e</td><td>$price</td></tr>";
}

echo "<tr>td
colspan=13'>Total</td><td>$total</td></tr</tabl
e>";
}

?>

<form method="post">
Customer Name: <input type="text"
name="customer name"><br>
Product 1: <input type="text"
name"product ]"><br>
Quantity 1: <input type="number"
name=" quantity [] "><br>
Product 2: <input type="text"
name="product [] "><br>
Quantity 2: <input type="number"
name=" quantity [] "><br>
<input type=" submit" value="Submit">
</form>
10. PHP Script to Check if a String is
Palindrome (Self Processing Form)

<?php
if ($ SERVER["REQUEST METHOD" ] "POST") {

$string = $ POST [ 'string']i

// Remove spaces and convert to lowercase


$processedstring = strtolower (str replace ("
$string) );

if ($processedString
strrev ($processedstring) ) {

echo "$string is a Palindrome. ";


{
}else
echo "$string is not a Palindrome. ";

?>

<form method="post">
Enter a String: <input type="text"
name="string" value="<?php echo
isset ($ POST('string']) ? $ POST'string']:;
?>">
<input type="submit" value="Check">
</form>
11. PHP Script to Create a Class Fruit with
Constructor and Display Details

<?php
{
class Fruit
public $name, $color, $price;

public
{
function Construct ($name, $color,
$price)
$this->name $name;
$this->color = $color;
$this->price= $price;
}

public function displayDetails () {


echo "Eruit Name: $this->name <br>";
echo "Color: $this->color <br>";
echo "Price: $this->price <br>";
}

= new
$apple Fruit ("Apple", "Red", 150);
$apple->displayDetails () ;
?>
12. PHP Script to Convert Temperature from
Fahrenheit to Celsius Using Sticky Form

<?php
{
function fahrenheitToCelsius ($fahrenheit)
return ($fahrenheit - 32) * 5 / 9;
}

if ($ SERVER["REQUEST METHOD'"] == "POST") {

$fahrenheit = $ POST |'fahrenheit'];


= ;
$celsius fahrenheitToCelsius ($fahrenheit)
echo "$fahrenheit&deg;F
.
is equal to
round($celsius, 2) "& deg;C";
}

?>

<form method="post">
Enter Temperature in Fahrenheit:
<input type-"number" name-"fahrenheit"
value"<?php echo isset: ($ POST ['fahrenheit']) ?
$ POST['fahrenheit'] "; ?>">
<input type="submit" value="Convert ">
</form>

You might also like