Practical Exam Questions
Practical Exam Questions
1. Write PHP program to print even numbers upto 10 and sum of them.
Ans:
<?php
$sum=0;
for($i=0; $i<=10;$i+=2){
echo"$i</br>";
$sum+=$i;
}
echo"Sum=$sum";
?>
------
2. Write a PHP program to print prime number upto n.
Ans:
<?php
$count = 0 ;
$number = 2 ;
while ($count <20 )
{
$div_count=0;
for ( $i=1;$i<=$number;$i++)
{
if (($number%$i)==0)
{
$div_count++;
}}
if ($div_count<3)
{
echo $number." , ";
$count=$count+1;
}
$number=$number+1;
}
?>
--------
3) Write PHP program to print reverse of number.
Ans:
<?php
$num = 123;
$revnum = 0;
while ($num> 1)
?>
------------------------------------------------
<?php
$array = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
for($i = 0; $i<count($array);$i++)
echo "\n";
?>
5) Write PHP program to sort associative and multidimensional array.
Ans:
<?php
// Associative array
asort($assoc_array);
print_r($assoc_array);
// Multidimensional array
$multi_array = array(
array(3, 1, 4),
array(1, 5, 9),
array(2, 6, 5)
);
});
print_r($multi_array);
?>
------------------------------------------------
<?php
// Interface definition
interface Animal {
// Class definitions
class Cat implements Animal {
foreach($animals as $animal) {
$animal->makeSound();
?>
7) Write PHP program based on introspection.
Ans:
<?php
class Rectangle
var $dim1 = 2;
function Rectangle($dim1,$dim2)
$this->dim1 = $dim1;
$this->dim2 = $dim2;
function area()
return $this->dim1*$this->dim2;
function display()
$S = new Rectangle(4,2);
$class_properties = get_class_vars("Rectangle");
$object_properties = get_object_vars($S);
$class_methods = get_class_methods("Rectangle");
print_r($class_properties);
print_r($object_properties);
print_r($class_methods);
print_r($object_class);
?>
--------------------------------------------
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
<?php
session_start();
$_SESSION['username'] = 'john_doe';
$_SESSION['email'] = 'john.doe@example.com';
session_destroy();
echo "Session destroyed.";
?>
<?php
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "clg";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
// date()
echo "Current date: " . date("Y-m-d") . "\n";
// strtotime()
echo "Timestamp of $date: " . strtotime($date) . "\n";
// date_create()
$date_obj = date_create($date);
echo "Date object: " . date_format($date_obj, "Y-m-d") . "\n";
// date_diff()
$date1 = date_create("2023-04-09");
$date2 = date_create("2023-05-01");
$diff = date_diff($date1, $date2);
echo "Difference in days: " . $diff->format("%a") . "\n";
?>
The cookie is a clinet side resources. The session is a server side resources.
It os not holding the variable in cookie. It is holding the multiple variable in session.
GET has limits on the amount of information POST has no limits on the amount of
to send. The limitation is about 2048 information to send.
characters
Can be bookmarked Cannot be bookmarked.
Parameter remains in browser history. Parameter are not saved in browser history.
Only ASCII characters are allowed. No restrictions Binary data is also allowed.
3. State the difference between for and for each loop.
ANS:
For loop For each loop
The iteration is clearly visible. The block of The iteration is hidden. The block of code is
code is repeated as long as the condition is repeated until iterating over the array is
met or the counter meets a specific value. completed.
The stop condition is specified easily. The stop condition has to be explicitly
specified.
Upon working with collections, it needs the It can simply work without the usage of the
usage of the count() function. count() method.
It joins an array of elements into strings. It splits the array based on the separator.
The output example is ‘“ Car”,” Truck”,” Bus”’. The output example is ‘Array ( [0] => Car [1]
=> Truck [2] => Bus )
5. State user defined function and explain with example.
ANS:
Definition: -
i. A function is a block of code written in a program to perform some specific task.
ii. They take information as parameters, execute a block of statements or perform
operations on these parameters and return the result.
iii. A function will be executed by a call to the function.
The above code creates a car with a constructor which initializes its member variable
named color and price.
An object of the variable is created, and it is cloned to demonstrate deep cloning
Ans:
i. Serialization is a technique used by programmers to preserve their working
data in format that can later be restored to its previous form.
ii. Serializaling an object means converting it to a byte stream representation
that can be stored in a file.
iii. Serialization in php is mostly automatic, it requires little extra work from
you,beyond calling the serialize() & unserialize functions.
Syntax: Serialize(value1);
Example:
<?php
$s_data= serialize(array('Welcome', 'to', 'PHP'));
print_r($s_data . "<br>");
$us_data=unserialize($s_data);
print_r($us_data);
?>
8. How do you validate user inputs in PHP?
ANS: There are several ways to validate user inputs in PHP: