PHP Basic PS

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

PSG COLLEGE OF TECHNOLOGY

DPARTMENT OF APPLIED MATHEMATICS AND COMPUTATIONAL SCIENCES

BSc. COMPUTER SYSTEMS DESIGN

FIFTH SEMESTER

21XA02 – OPEN SOURCE SOFTWARE

PROBLEM SHEET –PHP (Basic, echo, print and operator and conditional statement)

Date: 26 June 2024 Due: 03 July 2024

1. Write a PHP script to get the PHP version and configuration information.
<?php
phpinfo( );
?>
CODE:-
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
phpinfo();
?>
</body>
</html>
OUTPUT:-

2. Write a program to print “Hello World” using echo only?


CODE:-
<html>
<head>
<title>Hello world</title>
</head>
<body>
<?php echo '<p>Hello World</p>';?>
</body>
</html>
OUTPUT:-

3. Write a program to print “Hello PHP” using PHP variable?


CODE:-
<html>
<head>
<title>HellO</title>
</head>
<body>
<?php $name="Hello PHP";echo $name?>
</body>
</html>

4. Write a program to print “Welcome to the PHP World” using some part of the text in
variable & some part directly in echo.

CODE:-
<html>
<head>
<title>HellO</title>
</head>
<body>
<?php $name="Welcome";echo $name," to the PHP World"?>
</body>
</html>
5. Write a program to print two PHP variables using single echo statement.
CODE:-
<html>
<head>
<title>HellO</title>
</head>
<body>
<?php $name="Welcome";$str=" to my home";echo $name,$str ;?>
</body>
</html>

6. Write a PHP program to check whether a number is positive, negative or zero.

CODE:-

<html>
<head>
<title>Number Check</title>
</head>
<body>
<h2>Check whether a number is positive, negative, or zero</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
Enter a number: <input type="number" name="number">
<input type="submit" value="Check">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['number'])) {
$number = $_POST['number'];

if ($number > 0) {
echo "The number $number is positive.";
} elseif ($number < 0) {
echo "The number $number is negative.";
} else {
echo "The number is zero.";
}
} else {
echo "Please enter a number.";
}
}
?>
</body>
</html>

7. Write a PHP script which displays all the numbers between 200 and 250 that are divisible by
4.
Expected Output : 200,204,208,212,216,220,224,228,232,236,240,244,248
CODE:-

<html>
<head>
<title>Numbers Divisible by 4</title>
</head>
<body>
<h2>Numbers between 200 and 250 divisible by 4:</h2>
<?php

for ($i = 200; $i <= 250; $i++)


{
if ($i % 4 == 0)
{
echo $i;

if ($i != 248)
{
echo ", ";
}
}
}
?>
</body>
</html>

8. Write a PHP program to compute the sum of the two given integer values. If the two values
are the same, then returns triple their sum

CODE:-

<html>
<head>
<title>Compute Sum</title>
</head>
<body>
<h2>Compute Sum of Two Given Integers</h2>
<form method="post">
Enter first number: <input type="number" name="value1" required><br>
Enter second number: <input type="number" name="value2" required><br>
<input type="submit" value="Compute">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
function computeSum($a, $b) {
if ($a == $b) {
return 3 * ($a + $b);
} else {
return $a + $b;
}
}

echo "The result for values $value1 and $value2 is: " . computeSum($value1, $value2);
}
?>
</body>
</html>

9. Write a PHP program which iterates the integers from 1 to 50. For multiples of three print
"CAR" instead of the number and for the multiples of five print "BUS". For numbers which are
multiples of both three and five print "CARBUS"

CODE:-

<html>
<head>
<title>CAR and BUS</title>
</head>
<body>
<h2>Integers from 1 to 50 with CAR and BUS replacements:</h2>
<?php
for ($i = 1; $i <= 50; $i++) {
if ($i % 3 == 0 && $i % 5 == 0) {
echo "CARBUS";
} elseif ($i % 3 == 0) {
echo "CAR";
} elseif ($i % 5 == 0) {
echo "BUS";
} else {
echo $i;
}

if ($i < 50) {


echo ", ";
}
}
?>
</body>
</html>

10. Write a program to count 5 to 15 using PHP loop (while and do-while)
CODE:-

<html>
<head>
<title>Count from 5 to 15</title>
</head>
<body>
<h2>Count from 5 to 15 using a while loop:</h2>
<?php
$i = 5;
while ($i <= 15) {
echo $i;
if ($i < 15) {
echo ", ";
}
$i++;
}
?>
<h2>Count from 5 to 15 using a do-while loop:</h2>
<?php
$j = 5;
do {
echo $j;
if ($j < 15) {
echo ", ";
}
$j++;
} while ($j <= 15);
?>
</body>
</html>

11. Write a program to calculate factorial of a number using for loop in PHP.

CODE:-

<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<h2>Factorial Calculator</h2>
<form method="post">
Enter a number: <input type="number" name="number" required><br>
<input type="submit" value="Calculate">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
$factorial = 1;

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


$factorial *= $i;
}

echo "The factorial of $number is: $factorial";


}
?>
</body>
</html>

12. Write a PHP program using nested for loop that creates a chess board.
CODE:-

<html>
<head>
<title>Chess Board</title>
<style>
table {
border-collapse: collapse;
margin: 20px;
}
td {
width: 50px;
height: 50px;
text-align: center;
}
.black {
background-color: black;
}
.white {
background-color: white;
}
</style>
</head>
<body>
<h2>Chess Board</h2>
<table border="1">
<?php
for ($row = 1; $row <= 8; $row++) {
echo "<tr>";
for ($col = 1; $col <= 8; $col++) {
if (($row + $col) % 2 == 0) {
echo '<td class="white"></td>';
} else {
echo '<td class="black"></td>';
}
}
echo "</tr>";
}
?>
</table>
</body>
</html>
13. Create a simple HTML form and accept the user name and display the name through PHP
echo statement.

CODE:-

<html>
<head>
<title>Display Username</title>
</head>
<body>
<h2>Enter Your Name</h2>
<form method="post">
Name: <input type="text" name="username" required><br>
<input type="submit" value="Submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = htmlspecialchars($_POST['username']);
echo "<h3>Hello, " . $username . "!</h3>";
}
?>
</body>
</html>

14. Create an HTML form to perform sum or addition of two numbers in PHP programming.
CODE:-

<html>
<head>
<title>Addition of Two Numbers</title>
</head>
<body>
<h2>Addition of Two Numbers</h2>
<form method="post">
<label for="num1">Enter first number:</label>
<input type="number" id="num1" name="num1" required><br><br>

<label for="num2">Enter second number:</label>


<input type="number" id="num2" name="num2" required><br><br>

<input type="submit" value="Calculate Sum">


</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$sum = $num1 + $num2;
echo "<h3>The sum of $num1 and $num2 is: $sum</h3>";
}
?>
</body>
</html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy