0% found this document useful (0 votes)
14 views9 pages

Practical 1

The document contains a series of PHP practical exercises completed by Kudzai Gracious Shereni, a student in IT-D Batch 3. Each exercise demonstrates different PHP functionalities, including displaying messages, integrating HTML with PHP, using arithmetic and logical operators, generating Fibonacci series, calculating grades, finding maximum numbers, calculating factorials, determining areas of shapes, and implementing a simple calculator. The document also includes outputs for each PHP script, showcasing the results of the code executed.

Uploaded by

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

Practical 1

The document contains a series of PHP practical exercises completed by Kudzai Gracious Shereni, a student in IT-D Batch 3. Each exercise demonstrates different PHP functionalities, including displaying messages, integrating HTML with PHP, using arithmetic and logical operators, generating Fibonacci series, calculating grades, finding maximum numbers, calculating factorials, determining areas of shapes, and implementing a simple calculator. The document also includes outputs for each PHP script, showcasing the results of the code executed.

Uploaded by

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

Name : Kudzai Gracious Shereni | Class : IT-D | Batch 3

PRACTICAL 1

1. Write a PHP script to display Welcome message.


<?php

echo"WELCOME";

?>

OUTPUT :

2. Write a program to demonstrate intergration of HTML with PHP.

<?php
$name = "Kudzai Shereni";
$age = 20;
?>
<!DOCTYPE html>
<html>
<head><title>PHP and HTML</title>
</head>
<body>
<h1>Welcome!</h1>
<p>Hello,my name is <?php echo $name; ?> <br> I'm <?php echo $age; ?> </p>
</body>
</html>

OUTPUT:

Enrollment No : 23171371181
Name : Kudzai Gracious Shereni | Class : IT-D | Batch 3

3. Write a PHP script to demonstrate arithmetic operators, comparison


operator, and logical operator.
<?php

$a=20;

$b=10;

$c="20";

$d=30;

$sum= $a + $b;

echo "addition= $sum ";

echo "<br>";

$sub= $a - $b;

echo "subtraction= $sub";

echo "<br>";

$mul= $a * $b;

echo "multiplication= $mul ";

echo "<br>";

$div= $a / $b;

echo "division= $div ";

echo "<br>";

echo "<br> <b> Comparison operator : </b> <br>";

var_dump( $a == $c);

var_dump( $a == $b);

var_dump( $a == $d);

echo "<br> <b>Identical operator : </b> <br>";

var_dump( $a == $c);

Enrollment No : 23171371181
Name : Kudzai Gracious Shereni | Class : IT-D | Batch 3

var_dump( $a == $d);

echo "<br> <b> Not Identical operator : </b> <br>";

var_dump($a !== $c);

var_dump($a !== $d);

echo "<br> <b> Logical operators : </b> <br/>";

if($a > $c && $a > $d)

echo "true" ;

else

echo "false";

echo "<br/>";

if($a > $c && $a > $d)

echo "true";

else

echo "false" ;

?>

OUTPUT

Enrollment No : 23171371181
Name : Kudzai Gracious Shereni | Class : IT-D | Batch 3

4. Write PHP script to print Fibonacci series.

<?php
// Function to generate Fibonacci series
function fibonacci($n) {
$first = 0;
$second = 1;
echo "Fibonacci series up to $n terms: <br>";
echo $first . " " . $second . " ";
for ($i = 3; $i <= $n; $i++) {
$next = $first + $second;
echo $next . " ";
$first = $second;
$second = $next;
}
}
$terms = 10;
fibonacci($terms);
?>

OUTPUT:

Enrollment No : 23171371181
Name : Kudzai Gracious Shereni | Class : IT-D | Batch 3

5. Write a PHP script to generate result and display grade.

<?php
$marks = 85;
if ($marks >= 90) {
$grade = "A+";
} elseif ($marks >= 80) {
$grade = "A";
} elseif ($marks >= 70) {
$grade = "B+";
} elseif ($marks >= 60) {
$grade = "B";
} elseif ($marks >= 50) {
$grade = "C+";
} elseif ($marks >= 40) {
$grade = "C";
} else {
$grade = "F";
}

// Display result
echo "Marks: " . $marks . "<br>";
echo "Grade: " . $grade;
?>

OUTPUT:

6. Write PHP script to find maximum number out of three given numbers.

<?php
$a=20;

Enrollment No : 23171371181
Name : Kudzai Gracious Shereni | Class : IT-D | Batch 3

$b=50;
$c=10;
if($a > $b){
if($a > $c){
echo"greatest number :".$a;
}
else{
echo"greatest number :".$c;
}
}
if($b > $a){
if($b > $c){
echo"greatest number :".$b;
}
else{
echo"greatest number :".$c;
}
}
?>

OUTPUT:

7. Write PHP script to obtain 5! Using function.

<?php
// Function to calculate factorial
function factorial($n) {
$result = 1;
for ($i = 1; $i <= $n; $i++) {
$result *= $i;
}
return $result;
}

Enrollment No : 23171371181
Name : Kudzai Gracious Shereni | Class : IT-D | Batch 3

echo "5! = " . factorial(5);


?>
OUTPUT:

8. Write PHP script to find area of different objects.

<?php
// Area of a square
$side = 4;
$square = $side * $side;
echo "Area of square : " . $square . " square units <br>";
// Area of a rectangle
$length = 5;
$width = 3;
$rectangle = $length * $width;
echo "Area of rectangle : " . $rectangle . " square units <br>";
// Area of a circle
$radius = 7;
$circle = pi() * $radius * $radius;
$circleAreaFormatted = number_format($circle, 3);
echo "Area of circle : " . $circleAreaFormatted . " square units <br>";
// Area of a triangle
$base = 6;
$height = 4;
$triangle = 0.5 * $base * $height;
echo "Area of triangle : " . $triangle . " square units <br>";
?>

OUTPUT:

Enrollment No : 23171371181
Name : Kudzai Gracious Shereni | Class : IT-D | Batch 3

9. Write PHP script to prepare calculator(switch -case) .

<?php
// Simple calculator with hardcoded numbers and operation
$num1 = 10;
$num2 = 5;
$operation = 'subtract'; // Operation ('add', 'subtract', 'multiply', 'divide')

switch ($operation) {
case 'add':
$result = $num1 + $num2;
break;
case 'subtract':
$result = $num1 - $num2;
break;
case 'multiply':
$result = $num1 * $num2;
break;
case 'divide':
if ($num2 != 0) {
$result = $num1 / $num2;
} else {
$result = "Cannot divide by zero";
}
break;
default:
$result = "Invalid operation.";
break;
}
echo "Result : " . $result;
?>

OUTPUT:

Enrollment No : 23171371181
Name : Kudzai Gracious Shereni | Class : IT-D | Batch 3

Enrollment No : 23171371181

You might also like