PHP Programs
Q: Get name of the form from the user and show greeting text.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>Enter Name</title>
<style>
div {
margin: 2px;
padding: 2px;
font-size: 40px;
font-family: 'Poppins';
}
</style>
</head>
<body>
<h2>Enter Your Name</h2>
<form action="greet_name.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"
required>
<br><br>
<button>Submit</button>
</form>
<?php
$name = $_POST['name'];
echo "<div>Hello, $name!</div>";
echo "<div>Nice to meet you </div>";
?>
</body>
</html>
Q: Write a PHP Program to check whether the given number is
palindrome or not.
<?php
$word = 1221;
$reversed = strrev($word);
if ($word == $reversed){
echo 'The entered number is a palindrome.';
} else {
echo 'The entered number is not a
palindrome.';
}
?>
Q: Write a PHP Program to check whether the given number is
Armstrong or not.
<?php
$num = '410';
$temp = 0;
$len = strlen($num);
for ($i=0; $i < $len; $i++) {
$temp = $num[$i] ** $len + $temp;
}
if ($temp == (int)$num){
echo "The $num is Armstrong";
} else {
echo "The $num is not Armstrong";
}
?>
Q: Write a PHP Program to find largest values of two numbers
using nesting of function.
<?php
function findLargest($a, $b) {
function maxValue($x, $y) {
return ($x > $y) ? $x : $y;
}
return maxValue($a, $b);
}
$num1 = 15;
$num2 = 20;
echo "The largest number between $num1 and $num2
is: " . findLargest($num1, $num2);
?>
Q: Write a Mathematical calculator program.
<?php
$num1 = 34;
$num2 = 4;
$choice = 6;
if ($choice == 1){
$calc = $num1 + $num2;
echo "$num1 + $num2 = $calc";
} else if ($choice == 2){
$calc = $num1 - $num2;
echo "$num1 - $num2 = $calc";
} else if ($choice == 3){
$calc = $num1 * $num2;
echo "$num1 * $num2 = $calc";
} else if ($choice == 4){
if ($num2 == 0){
echo "Error: Division by zero is not
allowed.";
} else {
$calc = $num1 / $num2;
echo "$num1 / $num2 = $calc";
}
} else if ($choice == 5){
$calc = $num1 ** $num2;
echo "$num1 power of $num2 = $calc";
}
?>
Q: Write a Age calculator program.
<?php
$year = 2007;
$current_year = date('Y');
$age = $current_year - $year;
echo "Your age is $age years.";
?>
Q: Write a PHP Program to check whether given number is
String palindrome or not.
<?php
$word = 'mom';
$reversed = strrev($word);
if ($word == $reversed){
echo 'The entered word is a palindrome.';
} else {
echo 'The entered word is not a
palindrome.';
}
?>
Q: Write a PHP Program using function.
<?php
function sqr(&$n){
$n *= $n;
return $n;
}
$num = 13;
echo "Square of $num: ". sqr($num);
?>
Q: Create a PHP page for login page without sql connection.
<?php
if (isset($_POST['name'])){
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from login where username
= '$username' and password = '$password'";
$result = mysqli_query($connection, $sql);
$row = mysqli_fetch_array($result,
MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
if ($count == 1) {
header('Location:welcome.php');
} else {
echo '<script> window.location.href =
"login.php";
alert("Login failed")
</script>';
}
}
?>
Q: Write a PHP program for Array Manipulation.
<?php
$a = array(1, 3, 1.3);
$num = array(2, 49, 10, 34);
//length of the array
echo 'Length of the
array:'.count($a).'<br>';
//push element end of the array
array_push($a, 'hello', true);
foreach ($a as $i) {
echo $i.', ';
}
//pop the last element of the array
echo '<br>';
array_pop($a);
foreach ($a as $i) {
echo $i.', ';
}
//pop the first element of the array
echo '<br>';
array_shift($a);
foreach ($a as $i) {
echo $i.', ';
}
//push element in the beginning of the
array
echo '<br>';
array_unshift($a, 'world', 1.1);
foreach ($a as $i) {
echo $i.', ';
}
//sort the array
echo '<br>';
sort($num);
foreach ($num as $i) {
echo $i.', ';
}
//reverse the array
echo '<br>';
$l = array_reverse($num);
foreach ($l as $i) {
echo $i.', ';
}
// check value in array
echo '<br>';
echo in_array(2, $num).'<br>';
// return index of value if present in
array
echo 'Index of 2: '.array_search(2,
$num).'<br>';
// slicing of the array
$sliced_array = array_slice($num, 1,
3);
foreach ($sliced_array as $i) {
echo $i.', ';
}
// merging two arrays
echo '<br>';
$merged_array = array_merge($a, $num);
foreach ($merged_array as $i) {
echo $i.', ';
}
// Convert array to a string
$str = implode(',', $a);
echo $str.'<br>';
// convert string to array
$c = explode(", ", "1, 2, 3, 4");
foreach ($c as $i) {
echo $i.' ';
}
// map function
echo '<br>';
function square($n) {
return $n * $n;
}
$squares = array_map('square', $c);
foreach ($squares as $i) {
echo $i.' ';
}
// filter function
echo '<br>';
function even($n) {
return $n % 2 == 0;
}
$evens = array_filter($c, 'even');
foreach ($evens as $i) {
echo $i.' ';
}
?>
Q: Write a PHP program to design personal information.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>Personal Information Form</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f2f5;
}
.form-container {
width: 400px;
padding: 20px;
background-color: #ffffff;
box-shadow: 0px 4px 8px rgba(0, 0, 0,
0.2);
border-radius: 8px;
}
.form-container h2 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
.form-container label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.form-container input[type="text"],
.form-container input[type="email"],
.form-container input[type="number"],
.form-container textarea {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
}
.form-container input[type="submit"] {
width: 100%;
padding: 12px;
background-color: #007bff;
color: #ffffff;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
.form-container input[type="submit"]:hover
{
background-color: #0056b3;
}
.result-container {
margin-top: 20px;
padding: 20px;
background-color: #e9ecef;
border-radius: 8px;
}
.result-container p {
margin: 5px 0;
}
</style>
</head>
<body>
<div class="form-container">
<h2>Personal Information</h2>
<form action="" method="post">
<label for="name">Full Name:</label>
<input type="text" id="name" name="name"
required>
<label for="email">Email:</label>
<input type="email" id="email" name="email"
required>
<label for="age">Age:</label>
<input type="number" id="age" name="age"
min="1" required>
<label for="address">Address:</label>
<textarea id="address" name="address"
rows="3" required></textarea>
<input type="submit" name="submit"
value="Submit">
</form>
</div>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get and sanitize input data
$name = htmlspecialchars($_POST["name"]);
$email = htmlspecialchars($_POST["email"]);
$age = htmlspecialchars($_POST["age"]);
$address = htmlspecialchars($_POST["address"]);
// Display the result
echo '<div class="result-container">';
echo '<h3>Your Submitted Information:</h3>';
echo '<p><strong>Name:</strong> ' . $name .
'</p>';
echo '<p><strong>Email:</strong> ' . $email .
'</p>';
echo '<p><strong>Age:</strong> ' . $age .
'</p>';
echo '<p><strong>Address:</strong> ' .
nl2br($address) . '</p>';
echo '</div>';
}
?>
</body>
</html>
Q: Create a PHP page for login page with sql connection.
<?php
include('bk.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-
width, initial-scale=1.0">
<title>Login Page</title>
<style>
/* Basic reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Body styling */
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: Arial, sans-serif;
background-color: #f0f2f5;
}
/* Login container */
.login-container {
width: 350px;
padding: 40px;
background-color: #ffffff;
box-shadow: 0px 4px 8px rgba(0, 0, 0,
0.2);
border-radius: 8px;
text-align: center;
}
/* Title styling */
.login-container h2 {
margin-bottom: 20px;
color: #333;
font-size: 24px;
}
/* Input fields styling */
.login-container input[type="text"],
.login-container input[type="password"] {
width: 100%;
padding: 12px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
/* Button styling */
.login-container input[type="submit"] {
width: 100%;
padding: 12px;
margin-top: 20px;
background-color: #007bff;
border: none;
border-radius: 5px;
color: #ffffff;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
/* Hover effect */
.login-container input[type="submit"]:hover
{
background-color: #0056b3;
}
/* Additional link styling */
.login-container a {
display: block;
margin-top: 15px;
color: #007bff;
text-decoration: none;
font-size: 14px;
}
.login-container a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<form action="bk.php" method="post">
<input type="text" name="username"
placeholder="Username" required>
<input type="password" name="password"
placeholder="Password" required>
<input type="submit" value="Login"
name="submit">
<a href="#">Forgot Password?</a>
</form>
</div>
</body>
</html>
<?php
if (isset($_POST['name'])){
$server = 'localhost';
$username = 'root';
$password = '';
// $db_name = 'login';
$connection = new mysqli($server,
$username, $password);
if ($connection->connect_error) {
die("Connection failed: ". $connection-
>connect_error);
}
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from login where username
= '$username' and password = '$password'";
$result = mysqli_query($connection, $sql);
$row = mysqli_fetch_array($result,
MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
if ($count == 1) {
header('Location:welcome.php');
} else {
echo '<script> window.location.href =
"login.php";
alert("Login failed")
</script>';
}
}
?>
Q: Write a PHP program to read from existing file.
<?php
$read = fopen('myfile.txt', 'r') or die("File
not exists");
echo fread($read, filesize('myfile.txt'));
fclose($read);
?>
Q: Write a PHP Program to write in a file.
<?php
$write = fopen('myfile.txt', 'w');
if ($write){
fwrite($write, 'My name is Tanishq');
fclose($write);
echo 'File created and written
successfully!';
}
?>
Q: Write a PHP program to calculate date and Time.
<?php
date_default_timezone_set('Asia/kolkata');
echo "Date: ".date('d-m-Y');
echo '<br>';
echo "Time: ".date('h:i:s A');
?>