0% found this document useful (0 votes)
24 views

PHP Output

The document contains 10 PHP programs that demonstrate various concepts like generating Fibonacci series, form validation, page view counter, hit counter using cookies, generating multiplication tables, file handling operations, age calculator, passing data between pages, displaying data from MySQL table using stored procedures, and creating a MySQL table.

Uploaded by

yogar2600
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

PHP Output

The document contains 10 PHP programs that demonstrate various concepts like generating Fibonacci series, form validation, page view counter, hit counter using cookies, generating multiplication tables, file handling operations, age calculator, passing data between pages, displaying data from MySQL table using stored procedures, and creating a MySQL table.

Uploaded by

yogar2600
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Program – 1 - Fibonacci series.

Coding
<?php
$a = 0;
$b = 1;
echo "Fibonacci Series Numbers Less Than 100 : \n";
while ($a < 100) {
echo $a . "\n";
$temp = $a;
$a = $b;
$b = $temp + $b;
}
?>

Output

Fibonacci Series Numbers Less Than 100 : 0 1 1 2 3 5 8 13 21 34 55 89

Program – 2 - controls and functions.


Coding
<form action="" method="post">
<label for="age">Enter your age:</label>
<input type="number" id="age" name="age" required>
<input type="submit" value="Check Eligibility">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n = $_POST["age"];
if ($n == 18) {
echo "You are eligible";
} else if ($n < 18) {
echo "You are not eligible";
} else {
echo "You are eligible";
} } ?>

Output
Program – 3 - page views (refresh) count.
Coding
<?php
session_start(); // Start the session
// Check if the page views count is set in the session
if (!isset($_SESSION['page_views'])) {
$_SESSION['page_views'] = 1; // Initialize the count if not set
} else {
$_SESSION['page_views']++; // Increment the count on each refresh
}
?>
<p>This page has been viewed <?php echo $_SESSION['page_views']; ?> times.</p>

Output

Program – 4 - hit counter using cookies.


Coding

<?php
// Function to increment and get the hit count
function incrementAndGetHitCount() {
// Check if 'hit_count' cookie is set
$hitCount = isset($_COOKIE['hit_count']) ? $_COOKIE['hit_count'] + 1 : 1;

// Set the 'hit_count' cookie with updated value and expiration time
setcookie('hit_count', $hitCount, time() + 3600 * 24);

// Return the updated hit count


return $hitCount;
}

// Display the visitor number


echo "<p>You Are Visitor Number: " . incrementAndGetHitCount() . "</p>";
?>
Output
Program – 5 - generate multiplication table.
Coding
<html>
<head>
<title>Multiplication Table</title>
</head>
<body>
Enter a number: <input type="number" id="inputNum">
<button onclick="createTable()">Generate Table</button>
<table id="resultTable"></table>
<script>
function createTable() {
var num = document.getElementById("inputNum").value;
var table = document.getElementById("resultTable");
table.innerHTML = "";
for(var i = 1; i <= 10; i++) {
var row = `<tr><td>${num} x ${i} = ${num*i}</td></tr>`;
table.innerHTML += row;
}
}
</script>
</body>
</html>

Output
Program – 6 – perform Read, Write, Append operation on file.
Coding

<?php

// Read file
$filename = "test.txt";
$file = fopen($filename, "r");
$content = fread($file, filesize($filename));
fclose($file);
echo "File Content: " . $content;

// Write file
$filename = "test.txt";
$content = "New content written";
$file = fopen($filename, "w");
fwrite($file, $content);
fclose($file);

// Append file
$filename = "test.txt";
$content = "Appended content";
$file = fopen($filename, "a");
fwrite($file, $content);
fclose($file);

?>

Output
Program – 7 - compute age for a given date.

Coding

<body>

<h2>Age Calculator</h2>

<form action="" method="get">

<label for="dob">Date of Birth:</label>

<input type="date" id="dob" name="dob" required>

<button type="submit">Calculate Age</button>

</form>

<?php

// Check if 'dob' key is set in the $_GET array

if (isset($_GET['dob'])) {

// Get the date of birth from user input

$birthDate = new DateTime($_GET['dob']);

// Get today's date

$today = new DateTime(date('Y-m-d'));

// Calculate age by getting the difference in years between dates

$age = $today->diff($birthDate)->y;

// Print out the age

echo "Age: $age";

?>

</body>
Output

Program – 8 - message passing mechanism between pages.

Coding

Index.php file

<html>

<body>

<form action="welcome.php" method="post">

Name: <input type="text" name="name"><br>

E-mail: <input type="text" name="email"><br>

<input type="submit">

</form>

</body>

</html>

welcome.php file

<html>

<body>

<div class="welcome-container">

<h2>Welcome <?php echo $_POST["name"]; ?></h2>

<p>Your email address is: <?php echo $_POST["email"]; ?></p>

</div>

</body>

</html>
Output

Output 9
Program – 9 - display the table content using store procedures.

MYSQL – coding

-- Create a database

CREATE DATABASE IF NOT EXISTS CollegeDB;

USE CollegeDB;

-- Create a student table

CREATE TABLE IF NOT EXISTS Students (

StudentID INT AUTO_INCREMENT PRIMARY KEY,

FirstName VARCHAR(50),

LastName VARCHAR(50),

Age INT,

Course VARCHAR(50)

);

-- Insert some sample data

INSERT INTO Students (FirstName, LastName, Age, Course) VALUES

('JAGATHISH', 'KUMAR', 19, 'BCA'),

('MADHAN', 'KUMAR', 20, 'B.COM'),

('RANJITH', 'KUMAR', 21, 'MBA');

-- Create a stored procedure to display table content

DELIMITER //

CREATE PROCEDURE DisplayStudents()

BEGIN

SELECT * FROM Students;

END //

DELIMITER ;
Program – 10 - My Sql program to create a table student.

Coding

-- Create a new database if it doesn't exist

CREATE DATABASE IF NOT EXISTS school_database;

-- Switch to the created database

USE school_database;

-- Create the 'student' table

CREATE TABLE IF NOT EXISTS student (

student_id INT AUTO_INCREMENT PRIMARY KEY,

first_name VARCHAR(50) NOT NULL,

last_name VARCHAR(50) NOT NULL,

date_of_birth DATE,

gender ENUM('Male', 'Female', 'Other'),

email VARCHAR(100) UNIQUE,

phone_number VARCHAR(15),

address VARCHAR(255)

);

Output

You might also like