Php Journal
Php Journal
echo"Addition: ".($a+$b);
echo "<br>";
echo"Subtraction: ".($a-$b);
echo "<br>";
echo"Division: ".($a/$b);
echo "<br>";
echo"Multiplication: ".($a*$b);
echo "<br>";
?>
OUTPUT:
Addition: 12
Subtraction: 8
Division: 5
Multiplication: 20
2. Print current date and time using date() function.
સર એે લખાયેલાે
<?php
// Display the current date and time in the format: day-month-year hour:minute:second
echo date("d-m-Y h:i:s");
?>
Note: we have to use i for minutes, not m
OUTPUT:
06-02-2025 09:18:55
<?php
// Set the timezone to Indian Standard Time (IST)
date_default_timezone_set("Asia/Kolkata");
// Display the current date and time in the format: day-month-year hour:minute:second
echo date("d-m-Y h:i:s");
?>
OUTPUT:
06-02-2025 01:48:55
3. Display dates in different formats.
<?php
echo date('Y') . '<br>'; // Output: 2025
OUTPUT:
2025
February-2025
06-February-2025
Thursday-06-February-2025
4. Check whether a given number is odd or even.
<?php
$a = 8;
if ($a % 2 == 0)
{
echo "Number is even";
} else
{
echo "Number is odd";
}
?>
OUTPUT:
Number is even
5. Print constant value (i.e., $pi=3.14) of the variable OR Use define to find the area of a
circle, where Area=pi*r*r.
<?php
define("PI", 3.14);
$r = 2;
$area = PI * $r * $r;
echo "The area of the circle is: " . $area;
?>
OUTPUT:
The area of the circle is: 12.56
6.Find the maximum number among three numbers (using if..else statement).
<?php
$a = 10;
$b = 25;
$c = 15;
OUTPUT:
b is the greatest number.
7. Perform an arithmetic operation based on user choice (using Switch case and passing
the parameter between two pages).
J7.html
<html>
<head>
<title>Simple Calculation</title>
</head>
<body>
<h1>Simple Calculation</h1>
<form action="J7.php" method="post">
NO1: <input type="text" id="no1" name="no1" value=""><br>
NO2: <input type="text" id="no2" name="no2"value=""><br>
Operation:<select name="op" id="op">
<option value="+">Addition</option>
<option value="-">Subtraction </option>
<option value="*">Multiplication</option>
<option value="/">Division </option>
</select><br><br>
switch ($ch) {
case '+':
echo "Addition of two numbers: " . ($a + $b);
break;
case '-':
echo "Subtraction of two numbers: " . ($a - $b);
break;
case '*':
echo "Multiplication of two numbers: " . ($a * $b);
break;
case '/':
echo "Division of two numbers: " . ($a / $b);
break;
default:
echo "Invalid Choice";
break;
}
}
?>
OUTPUT:
<html>
<head>
<title>Fibonacci </title>
</head>
<body><h2>Fibonacci series</h2>
<form action="" method="post">
Number: <input type="text" name="number" value="<?php echo $_POST['number'] ?? '';
?>">
<input type="submit" name="print" value="Print Fibonacci series">
</form>
<?php
function fibo($number) {
if ($number == 0) {
return 0;
} else if ($number == 1) {
return 1;
} else {
return fibo($number - 1) + fibo($number - 2);
}
}
if (isset($_POST['print'])) {
$number = $_POST['number'];
for ($cnt = 0; $cnt < $number; $cnt++) {
echo "Fibonacci($cnt): " . fibo($cnt) . "<br>";
}
}
?>
</body>
</html>
OUTPUT
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<h2>Simple Calculator</h2>
<form action="" method="post">
NO1: <input type="text" name="no1" value=""><br>
NO2: <input type="text" name="no2" value=""><br>
Operation:
<select name="op">
<option value="+">Addition</option>
<option value="-">Subtraction</option>
<option value="*">Multiplication</option>
<option value="/">Division</option>
</select><br><br>
<input type="submit" name="calculate" value="Calculate">
</form>
<?php
if (isset($_POST['calculate'])) {
$no1 = $_POST['no1'];
$no2 = $_POST['no2'];
$op = $_POST['op'];
OUTPUT
Result: 5
<html>
<head>
<title>Factorial</title>
</head>
<body><h2>Factorial</h2>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"
method="post">
Enter Number : <input type="number" name="number" id="number">
<input type="submit" name="submit" value="submit">
</form>
<?php
if ($_POST) {
$fact = 1;
$number = $_POST['number'];
echo "Factorial of $number :</br></br>";
OUTPUT
9. Create an array of type (Numeric, Associative, and Multidimensional) and display
content on the screen.
<?php
// Numeric Array
$numericArray = array(10, 20, 30);
// Associative Array
$associativeArray = array(
"name" => "RAM BHAI",
"age" => 25,
"city" => "HOME"
);
// Multidimensional Array
$multiArray = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
Associative Array:
name: RAM BHAI
age: 25
city: HOME
Multidimensional Array:
123
456
789
10. Create an array with 10 elements, find the maximum and minimum elements from it.
<?php
function getMax($array)
{$n = count($array);
$max = $array[0];
OUTPUT:
Maximum number is: 79
Minimum number is: 1
<?php
$ch="a";
$str = "Sardar Patel University";
$occ = substr_count($str, $ch);
OUTPUT:
The number of occurrences is/are:3
12. Write a program to generate the even number & odd number series up to a given
number.
<html>
<head
<title>Odd Even</title>
</head>
<body>
<form method="POST">
Enter a Number:<input type="text" name="number" ><br>
<input type="submit" value="Submit">
</form>
<?php
$a="";
$b="";
if ($_POST)
{
$n = $_POST['number'];
OUTPUT:
J13.html
<html>
<head>
<title>Marksheet SEM-6</title>
</head>
<body>
<form action="J13.php" method="post">
ROLL NO :<input type="text" name="rno"/><br>
NAME :<input type="text" name="nm"/><br>
MARKS OF SUB 1:<input type="text" name="m1"/><br>
MARKS OF SUB 2:<input type="text" name="m2"/><br>
MARKS OF SUB 3:<input type="text" name="m3"/><br>
<input type="submit" >
</body>
</html>
J13.php
<?php
$con = mysqli_connect("localhost", "root", "", "marksheet");
if ($con)
{
$rno = $_POST["rno"];
$nm = $_POST["nm"];
$m1 = $_POST["m1"];
$m2 = $_POST["m2"];
$m3 = $_POST["m3"];
$tot = $m1 + $m2 + $m3;
$per = $tot / 3;
J14.html
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="J14.php" method="post">
USERNAME: <input type="text" name="unm" value=""><br>
PASSWORD: <input type="text" name="pwd" value=""><br>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>
J14.php
<?php
session_start();
if (isset($_POST['submit'])) {
$name = $_POST['unm'];
$pwd = $_POST['pwd'];
if($name!="&&$pwd=")
{
$sel = "SELECT * FROM login WHERE unm = '$name' AND pwd = '$pwd'";
$query=mysqli_query($conn,$sel)or die(mysqli_error($conn));
$res=mysqli_fetch_row($query);
if($res)
{
$_SESSION['name'] = $name;
//header(location:welcome.php);
echo" Welcome to home";
}
else
{
echo "You Entered username or password is incorrect";
}
}
else
{
echo "Enter both usernmae and password";
}
}
?>
OUTPUT:
Welcome to home
15.
J15.php
<html>
<head>
<title>Journal-15</title>
</head>
<body>
<form action="Insert.php" method="post">
First name: <input type="text" name="fname" value=""><br>
Last name: <input type="text" name="lname" value=""><br>
Date of Birth: <input type="date" name="bdate" value=""><br>
//you can use text or date in DOB
<input type="submit" name="submit" value="Submit"><br>
<a href="Insert.php">Insert</a><br>
<a href="Display.php">Display</a>
</form>
</body>
</html>
Display.php
<!-- Display.php -->
<?php
$con = mysqli_connect("localhost", "root", "", "birthday");
if (!$con) {
die("Error.. Not Connected: " . mysqli_connect_error());
} else {
$query = "SELECT * FROM bdate"; // Fixed table name
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {
echo "<table>";
echo "<tr>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>Date of Birth</th>";
echo "<th>Edit</th>";
echo "<th>Delete</th>";
echo "</tr>";
Delete.php
<!-- delete.php -->
<?php
$con = mysqli_connect("localhost", "root", "", "birthday");
if (isset($_GET['del']))
{
$id = $_GET['del'];
if ($result)
{
header("Location: display.php");
} else
{
echo "Please check your query";
}
} else
{
header("Location: display.php");
exit();
}
mysqli_close($con);
?>
Insert.php
<!-- insert.php -->
<?php
$con = mysqli_connect("localhost", "root", "", "birthday");
if (!$con)
{
die("Error.. Not Connected: " . mysqli_connect_error());
}
if (isset($_POST['submit']))
{
if (empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['bdate']))
{
echo "Please fill in the blanks";
}
else
{
$fnm = $_POST['fname'];
$lnm = $_POST['lname'];
$dob = $_POST['bdate'];
$query = "INSERT INTO bdate (fname, lname, bdate) VALUES ('$fnm', '$lnm', '$dob')";
$result = mysqli_query($con, $query);
if ($result)
{
header("Location: display.php");
exit();
}
else
{
echo "Please check your query";
}
}
}
else
{
header("Location: j15.php");
exit();
}
mysqli_close($con);
?>
Edit.php
<!-- edit.php -->
<?php
$con = mysqli_connect("localhost", "root", "", "birthday");
if (!$con)
{
die("Error.. Not Connected: " . mysqli_connect_error());
}
$id = $_GET['GETID'];
mysqli_close($con);
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<form action="update.php" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
First name: <input type="text" name="fname" value="<?php echo $fnm; ?>"><br>
Last name: <input type="text" name="lname" value="<?php echo $lnm; ?>"><br>
Date of Birth: <input type="date" name="bdate" value="<?php echo $dob; ?>"><br>
<input type="submit" name="update" value="Update">
</form>
</body>
</html>
Update.php
<?php
$con = mysqli_connect("localhost", "root", "", "birthday");
if (!$con) {
die("Error.. Not Connected: " . mysqli_connect_error());
}
if ($result) {
header("Location: display.php");
exit();
} else {
echo "Error updating record.";
}
}
} else {
header("Location: j15.php");
exit();
}
mysqli_close($con);
?>
OUTPUT:
16. Delete multiple records from the database on given ID.
<html>
<head>
<title>Student Information</title>
</head>
<body>
<form method="POST">
<table border="1">
<tr>
<td>Roll Number:</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="nm"/></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="Delete" name="Submit"/>
</td>
</tr>
</table>
</form>
<?php
$cn = mysqli_connect('localhost', 'root', '', 'birthday');
if ($cn)
{
if (isset($_POST['Submit']))
{
$id = $_POST['id'];
$nm = $_POST['nm'];
$del = "DELETE FROM stud_info WHERE id='$id'";
mysqli_query($cn, $del);
}
echo '<table border="1">';
echo '<tr>';
echo '<th>Roll no</th>';
echo '<th>Name</th>';
echo '</tr>';
$dis = "SELECT * FROM stud_info";
$result = mysqli_query($cn, $dis);
while ($row = mysqli_fetch_assoc($result))
{
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['nm'] . '</td>';
echo '</tr>';
}
echo '</table>';
mysqli_close($cn);
}
Else
{
echo "DB not Connected";
}
?>
</body>
</html>
OUTPUT:
17. Search records from the database using where username starts with 'A'.
<?php
$cn = mysqli_connect('localhost', 'root', '', 'birthday');
if ($cn) {
echo "DB is connected...";
echo '<table border="1">';
echo '<tr>';
echo '<th>Roll Number</th>';
echo '<th>Name</th>';
echo '</tr>';
$query1 = "SELECT * FROM stud_info WHERE nm LIKE 'A%'";
$result = mysqli_query($cn, $query1);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['nm'] . '</td>';
echo '</tr>';
}
} else
{
echo "0 results";
}
echo '</table>';
}
mysqli_close($cn);
?>
18. Check the existence of file or database using die() function to handle an error and risk
of crashing.
// for file
<?php
if (file_exists("mytestfile.txt"))
{
$file = fopen("mytestfile.txt", "r");
} else
{
die("Error: The file does not exist");
}
?>
//for database
<?php
$con = mysqli_connect('localhost', 'root', '', 'TYIT');
if (!$con)
{
die('Could not connect: ' . mysqli_connect_error());
}
if (mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
}
else
{
echo "No records found!";
}
mysqli_close($con);
?>
19. Write a script which store the password of the user using cookies and retrieve it using
table.
<html>
<head>
<script>
function check()
{
if(document.getElementById("user").value=="<?php echo isset($_COOKIE['user']) ?
$_COOKIE['user'] : ''; ?>")
{
document.getElementById("pass").value="<?php echo isset($_COOKIE['pass']) ?
$_COOKIE['pass'] : ''; ?>";
}
}
</script>
</head>
<body>
<form method="post">
Username: <input type="text" id="user" name="user">
<br>
Password: <input type="password" id="pass" name="pass" onfocus="check()">
<br>
<input type="checkbox" name="remember"> Remember me
<br>
<input type="submit" name="save" value="Save">
</form>
</body>
</html>
<?php
if(isset($_POST['save']))
{
$user = $_POST['user'];
$pass = $_POST['pass'];
if(isset($_POST['remember']))
{
setcookie("user", $user, time() + 300);
setcookie("pass", $pass, time() + 300);
}
}
?>
20. Count the number of page hit using session variable.
<?php
session_start();
if (isset($_SESSION['visit'])) {
$_SESSION['visit'] += 1;
echo("page visited " . $_SESSION['visit'] . " timer");
} else {
$_SESSION['visit'] = 1;
echo("you visit site for " . $_SESSION['visit'] . " timer");
}
?>
<html>
<body bgcolor="pink">
<br>
site visited earlier:
<?php
echo($_SESSION['visit']);
?>
</body>
</html>
21. Write a function to validate email (using Regular Expression).
<!DOCTYPE html>
<html>
<head>
<style>
.error { color: #FF0000; }
</style>
</head>
<body>
<?php
// Define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
</body>
</html>