Q3 and Q4 PHP
Q3 and Q4 PHP
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);
?>
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>
<?php
function factorial($n) {
if ($n <= 1) {
return 1;
// Example usage
$number = 5;
?>
<?php
function isPrime($num) {
if ($num <= 1) {
return false;
if ($num % $i == 0) {
return false;
return true;
// Example usage
$number = 29;
if (isPrime($number)) {
echo "$number is a prime number.";
} else {
?>
c) Write a code in PHP which accepts two strings from user and displays
<!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>
<?php
function factorial($n) {
if ($n <= 1) {
return 1;
}
// Example usage
$number = 5;
?>
Q4
e) Write a PHP program to create login page and welcome user on next
page.
fields(empid,empname,salary,dept).
<?php
$marks = array("John" => 85, "Alice" => 92, "Bob" => 75);
arsort($marks);
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.";
}
}
?>