Q3
c) Write a code in PHP which accepts two strings from user and displays
them after concatenation.
<!DOCTYPE html>
<html>
<body>
<form method="post">
String 1: <input type="text" name="string1"><br>
String 2: <input type="text" name="string2"><br>
<input type="submit" value="Concatenate">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$string1 = $_POST['string1'];
$string2 = $_POST['string2'];
echo "Concatenated String: " . $string1 . $string2;
}
?>
</body>
</html>
d) Write a PHP function to calculate factorial of a number using recursion.
<?php
function factorial($n) {
if ($n <= 1) {
return 1;
}
return $n * factorial($n - 1);
}
// Example usage
$number = 5;
echo "Factorial of $number is: " . factorial($number);
?>
e) Write a PHP program to print greatest number among given 3 numbers
<?php
function isPrime($num) {
if ($num <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
return false;
}
}
return true;
}
// Example usage
$number = 29;
if (isPrime($number)) {
echo "$number is a prime number.";
} else {
echo "$number is not a prime number.";
}
?>
C)Write a code in PHP which accepts two strings from user and displays
them after concatenation.
<!DOCTYPE html>
<html>
<body>
<form method="post">
String 1: <input type="text" name="string1"><br>
String 2: <input type="text" name="string2"><br>
<input type="submit" value="Concatenate">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$string1 = $_POST['string1'];
$string2 = $_POST['string2'];
echo "Concatenated String: " . $string1 . $string2;
}
?>
</body>
</html>
d) Write a PHP program to calculate factorial of a number.
<?php
function factorial($n) {
if ($n <= 1) {
return 1;
return $n * factorial($n - 1);
// Example usage
$number = 5;
echo "Factorial of $number is: " . factorial($number);
?>
e) Write a PHP script to check whether a number is prime or not.
<?php
function isPrime($num) {
if ($num <= 1) {
return false;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
return false;
return true;
// Example usage
$number = 29;
if (isPrime($number)) {
echo "$number is a prime number.";
} else {
echo "$number is not a prime number.";
?>
c) Write a code in PHP which accepts two strings from user and displays
them after concatenation.
<!DOCTYPE html>
<html>
<body>
<form method="post">
String 1: <input type="text" name="string1"><br>
String 2: <input type="text" name="string2"><br>
<input type="submit" value="Concatenate">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$string1 = $_POST['string1'];
$string2 = $_POST['string2'];
echo "Concatenated String: " . $string1 . $string2;
}
?>
</body>
</html>
d) Write a PHP program to calculate factorial of a number.
<?php
function factorial($n) {
if ($n <= 1) {
return 1;
}
return $n * factorial($n - 1);
// Example usage
$number = 5;
echo "Factorial of $number is: " . factorial($number);
?>
e) Write a PHP script to check whether a number is prime or not.
Q4
c) Write a menu driven program in PHP to display arithmatic operations.
d) Write a PHP program to generate random password.
e) Write a PHP program to create login page and welcome user on next
page.
c) Write PHP program for arsort() function.
d) Write a PHP program to display record of employee with
fields(empid,empname,salary,dept).
e) Write a PHP program to create login page and welco
<?php
$marks = array("John" => 85, "Alice" => 92, "Bob" => 75);
arsort($marks);
echo "Sorted Array (Descending Order):<br>";
foreach ($marks as $key => $value) {
echo "$key: $value<br>";
}
?>
d) Write a PHP program to display record of employee with
fields(empid,empname,salary,dept).
<?php
$employees = array(
array("empid" => 101, "empname" => "John", "salary" => 50000, "dept"
=> "IT"),
array("empid" => 102, "empname" => "Alice", "salary" => 60000, "dept"
=> "HR"),
array("empid" => 103, "empname" => "Bob", "salary" => 45000, "dept"
=> "Finance"),
);
echo "<table border='1'>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Salary</th>
<th>Department</th>
</tr>";
foreach ($employees as $employee) {
echo "<tr>
<td>{$employee['empid']}</td>
<td>{$employee['empname']}</td>
<td>{$employee['salary']}</td>
<td>{$employee['dept']}</td>
</tr>";
}
echo "</table>";
?>
e) Write a PHP program to create login page and welcome user on next
page.
Log in
<!DOCTYPE html>
<html>
<body>
<form method="post" action="welcome.php">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Welcome
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$password = $_POST['password'];
// Simple validation
if ($username == "admin" && $password == "1234") {
echo "Welcome, $username!";
} else {
echo "Invalid username or password.";
}
}
?>