Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Exercise

1. Write a various comparison operators in PHP with example.


Ans:
1) Equal (==): This operator checks if two values are equal.
2) Identical (===): Checks if two values are equal and of the same type.
3) Not Equal (!= or <>): Checks if two values are not equal.
4) Greater Than (>): Checks if the left operand is greater than the right operand.
5) Less Than (<): Checks if the left operand is less than the right operand.
6) Greater Than or Equal To (>=): Checks if the left operand is greater than or equal to the right operand.
7) Less Than or Equal To (<=): Checks if the left operand is less than or equal to the right operand.

2. Write a various Logical operators in PHP with example.


Ans:
1) Logical AND (&&): Returns true if both operands are true.
2) Logical OR (||): Returns true if at least one of the operands is true.
3) Logical XOR (Exclusive OR) (xor): Returns true if exactly one of the operands is true, but not both.
4) Logical AND (and): Same as &&, returns true if both operands are true.
5) Logical OR (or): Same as ||, returns true if at least one of the operands is true.

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1.Write a program to find given number is even or odd.


Ans:
<?php
echo "Enter a number: ";
$number = fgets(STDIN);

$number = (int)$number;
if ($number % 2 == 0) {
echo "The number $number is even.\n";
} else {
echo "The number $number is odd.\n";
}
?>

Output:

Enter a number: 4
The number 4 is even.

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1.Write any program using if condition with for loop.


Ans:
<?php
echo "Even numbers between 1 and 20 using if condition with for loop:\n";

for ($i = 1; $i <= 20; $i++) {


if ($i % 2 == 0) {
echo $i . " ";
}
}
?>

Output:

Even numbers between 1 and 20 using if condition with for loop:


2 4 6 8 10 12 14 16 18 20

2. Write a program to display pyramids of star/patterns using increment/decrement.


Ans:

<?php

echo "Pyramid of Stars:\n";


$base = 5;

for ($i = 1; $i <= $base; $i++) {


for ($j = 1; $j <= $base - $i; $j++) {
echo " ";
}
for ($j = 1; $j <= 2 * $i - 1; $j++) {
echo "* ";
}
echo "\n";
}
?>

Output:

Pyramid of Stars:
*
***
*****
*******
*********
Marks obtained Dated signature of
Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Develop a program to using Associative array.


Ans:
<?php
$grades = array(
"Raj" => 85,
"Raghav" => 92,
"Atharva" => 78
);

foreach ($grades as $student => $grade) {


echo "Student: $student, Grade: $grade\n";
}
?>

Output:

Student: Raj, Grade: 85


Student: Raghav, Grade: 92
Student: Atharva, Grade: 78

2. Develop a program to using Multidimensional array.


Ans:

<?php
$classroom = array(
"Raj" => array("Math" => 85, "Science" => 92, "English" => 78),
"Raghav" => array("Math" => 90, "Science" => 88, "English" => 95),
"Atharva" => array("Math" => 82, "Science" => 90, "English" => 85)
);

foreach ($classroom as $student => $grades) {


echo "Student: $student\n";
foreach ($grades as $subject => $grade) {
echo "Subject: $subject, Grade: $grade\n";
}
echo "\n";
}
?>
Output:

Student: Raj
Subject: Math, Grade: 85
Subject: Science, Grade: 92
Subject: English, Grade: 78

Student: Raghav
Subject: Math, Grade: 90
Subject: Science, Grade: 88
Subject: English, Grade: 95

Student: Atharva
Subject: Math, Grade: 82
Subject: Science, Grade: 90
Subject: English, Grade: 85

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Write a program to demonstrate PHP maths function.


Ans:
<?php
$num1 = 25;
$num2 = 10;

echo "Number 1: $num1\n";


echo "Number 2: $num2\n";

echo "Power: " . pow($num1, $num2) . "\n";

echo "Square Root of $num1: " . sqrt($num1) . "\n";

$number = -10;
echo "Absolute Value of $number: " . abs($number) . "\n";

$floatNumber = 10.67;
echo "Round of $floatNumber: " . round($floatNumber) . "\n";

$floatNumber = 10.01;
echo "Ceiling of $floatNumber: " . ceil($floatNumber) . "\n";

$floatNumber = 10.99;
echo "Floor of $floatNumber: " . floor($floatNumber) . "\n";
?>

Output:

Number 1: 25
Number 2: 10
Power: 95367431640625
Square Root of 25: 5
Absolute Value of -10: 10
Round of 10.67: 11
Ceiling of 10.01: 11
Floor of 10.99: 10

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Write a program to demonstrate parameterized function.


Ans:
<?php

function sayHello($name) {
echo "Hello, $name!\n";
}

sayHello("Raj");
sayHello("Raghav");
sayHello("Atharva");
?>

Output:

Hello, Raj!
Hello, Raghav!
Hello, Atharva!

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Write use of various graphics function.


Ans:

1) gd_info(): Retrieve information about the currently installed GD library.


2) getimagesize(): Get the size of an image.
3) getimagesizefromstring(): Get the size of an image from a string.
4) imagesx(): Return the width of the given image.
5) imagesy(): Return the height of the given image.
6) imagesettile(): Set the tile image for filling the area.

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Write a program to demonstrate parameterized constructor.


Ans:
<?php
class Student {
public $name;
public $age;

public function __construct($name, $age) {


$this->name = $name;
$this->age = $age;
}
}

$student1 = new Student("Raj", 20);


echo "Student 1: Name - " . $student1->name . ", Age - " . $student1->age . "\n";

$student2 = new Student("Raghav", 22);


echo "Student 2: Name - " . $student2->name . ", Age - " . $student2->age . "\n";
?>

Output:

Student 1: Name - Raj, Age - 20


Student 2: Name - Raghav, Age - 22

2. Write a program to demonstrate default constructor.


Ans:

<?php
class Car {
public $brand;
public $model;

public function __construct() {


$this->brand = "Toyota";
$this->model = "Corolla";
}
}

$car1 = new Car();


echo "Car 1: Brand - " . $car1->brand . ", Model - " . $car1->model . "\n";
?>

Output:

Car 1: Brand - Toyota, Model – Corolla


Marks obtained Dated signature of
Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Develop a PHP code for serialization.


Ans:
<?php
$data = array("name" => "Raj", "age" => 30, "city" => "Nevi Mumbai");
$serializedData = serialize($data);
echo "Serialized Data: $serializedData\n";
$deserializedData = unserialize($serializedData);
echo "Deserialized Data:\n";
print_r($deserializedData);
?>

Output:
Serialized Data: a:3:{s:4:"name";s:3:"Raj";s:3:"age";i:30;s:4:"city";s:11:"Nevi Mumbai";}
Deserialized Data:
Array
(
[name] => Raj
[age] => 30
[city] => Nevi Mumbai
)

2. Develop a PHP code for introspection.


Ans:
<?php
class Rectangle
{
var $dim1 = 2;
var $dim2 = 10;

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");

$object_class = get_class($S);

print_r($class_properties);
print_r($object_properties);
print_r($class_methods);
print_r($object_class);
?>

Output:

Array
(
[dim1] => 2
[dim2] => 10
)
Array
(
[dim1] => 2
[dim2] => 10
)
Array
(
[0] => Rectangle
[1] => area
[2] => display
)
Rectangle

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Develop a PHP code for serialization.


Ans:

index.html
<html>
<body>
<form method="post" action="validate.php">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

validate.php

<?php
$name = isset($_POST["name"]) ? $_POST["name"] : "";
$email = isset($_POST["email"]) ? $_POST["email"] : "";

echo "<p>Name: $name</p>";


echo "<p>Email: $email</p>";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<p>Form Validated<p>";
} else {
echo "<p>Form Not Validated<p>";
}
?>
Output:

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Write a use and syntax of following controls:


b. List box b. Hidden field box
c. Combo box
Ans:

index.html
<html>
<body>
<form method="post" action="validate.php">
<label for="cars">Select a car:</label>
<select id="cars" name="cars[]" multiple>
<option value="Tata">Tata</option>
<option value="Mahindra">Mahindra</option>
<option value="KiA">KiA</option>
<option value="Hero Honda">Hero Honda</option>
</select><br><br>

<input type="hidden" name="userid" value="06">

<label for="gender">Select gender:</label>


<select id="gender" name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select><br><br>

<input type="submit" value="Submit">


</form>
</body>
</html>

validate.php

<?php
$cars = isset($_POST["cars"]) ? $_POST["cars"] : [];
$userid = isset($_POST["userid"]) ? $_POST["userid"] : "";
$gender = isset($_POST["gender"]) ? $_POST["gender"] : "";

echo "<p>Selected Cars:</p>";


echo "<ul>";
foreach ($cars as $car) {
echo "<li>$car</li>";
}
echo "</ul>";
echo "<p>User ID: $userid</p>";
echo "<p>Gender: $gender</p>";
?>

Output:

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Write a simple PHP program to check that emails are valid.


Ans:
index.html
<html>
<body>
<form method="post" action="validate.php">
<label for="email">Enter Email:</label>
<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

validate.php

<?php
$email = isset($_POST["email"]) ? $_POST["email"] : "";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<p>$email is Valid email.<p>";
} else {
echo "<p>$email is not valid email.<p>";
}
?>

Output:

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Write a program to start and destroy a session.


Ans:
<?php
session_start();

$_SESSION["username"] = "Atharav Butte";


$_SESSION["email"] = "atharvabutte03@gmail.com";

echo "Session variables are set.<br>";


echo "Username: " . $_SESSION["username"] . "<br>";
echo "Email: " . $_SESSION["email"] . "<br>";

session_destroy();

if (session_status() === PHP_SESSION_NONE) {


echo "Session is destroyed.";
} else {
echo "Session is still active.";
}
?>

Output:

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1.Describe mail function parameter in detail.


Ans:

Parameters of mail() function:


1) to (required): The email address of the recipient(s) where the email will be sent. This can be a single
email address or multiple addresses separated by commas.

2) subject (required): The subject line of the email. It should be a brief summary of the email's content.

3) message (required): The actual message content of the email. This can be plain text or HTML formatted
content.

4) headers (optional): Additional email headers, such as From, Cc, Bcc, Reply-To, etc. These headers are
typically used to set the sender's email address, reply-to address, and other email parameters. It should be
formatted as a string.

5) parameters (optional): Additional parameters that can be used to specify options for sending the email.
This is rarely used and can be left empty in most cases.

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Write a PHP code to insert data into employee table..


Ans:

<?php
$conn = new mysqli("localhost", "root", "", "atharva");

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$name = "Atharav Butte";


$salary = 50000;

$sql = "INSERT INTO employee (name, salary) VALUES ('$name', '$salary')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Output:

New record created successfully.

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)
Exercise

1. Write a PHP program to Update table data from student database.


Ans:
<?php
$conn = new mysqli("localhost", "root", "", "student");

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE stud SET marks = '99' WHERE id= 1";

if ($conn->query($sql) === TRUE) {


echo "Updated Sucessfully<br><br >";
} else
echo "Error: " . $sql . "<br>" . $conn->error;

$sql_select = "SELECT * FROM stud";


$result = $conn->query($sql_select);

echo "<table border = 1 style='border-collapse: collapse'>";


echo "<tr><th>ID</th><th>Name</th><th>Marks</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>" . $row["marks"] . "</td>";
echo "</tr>"; }
echo "</table>";
$conn->close();
?>

Output:

Marks obtained Dated signature of


Teacher
Process Product Total(50)
Related(35) Related(15)

You might also like