PHP 20
PHP 20
PHP 20
24
String Manipulation
Exp. No.: 01
AIM:
To write a program for string manipulation by using php.
PROCEDURE:
Step 1: Initiated the program.
Step 2: Variable Declaration.
Step 3: String Length Calculation.
Step 4: String Replacement.
Step 5: Finding Substring Position.
Step 6: Custom Function for Word Count.
Step 7: Execute the program
PROGRAM:
<!DOCTYPE html>
<html>
<body>
<?php
$x = "Information Technology";
echo strlen($x). "<br>";
echo str_replace("Technology", "System", $x). "<br>";
echo strpos ($x,"Technology"). "<br>";
echo str_word_count($x). "<br>";
?>
</body>
</html>
OUTPUT:
RESULT:
Thus, the given program is executed and the output execute successfully.
AIM:
To write a program for Palindrome by using php.
Date : 11.1.24
Palindrome
Exp. No.: 02
PROCEDURE:
Step 1: Initiated the program.
Step 2: Define Palindrome Function.
Step 3: Clean the Input String.
Step 4: Reverse the Cleaned String.
Step 5: Check if the String is a Palindrome.
Step 6: Define Input Array and Iterate Through Each Element.
Step 7: Check Palindrome for Each Input String.
Step 8: End of Code.
PROGRAM:
<!DOCTYPE html>
<html>
<body>
<?php
function palindrome($str) {
$clean = strtolower(preg_replace('/[^A-Za-z0-9]/','',$str));
$reverse = strrev($clean);
if($clean == $reverse){
return true;
}
else{
return false;
}
}
$input = array("Eve","malayalam","Ede","12321","tamil");
foreach ($input as $input){
if (palindrome($input)){
echo "'$input' is palindrome.<br>";
}else{
echo "'$input' is not palindrome.<br>";
}
}
?>
</body>
</html>
OUTPUT:
RESULT:
Thus, the given program is executed and the output execute successfully.
Date : 18.1.24
Regular Expression
Exp. No.: 03
AIM:
To write a program for Regular Expression by using php.
PROCEDURE:
Step 1: Initiated the program.
Step 2: Assign Values to Variables.
Step 3: Match Pattern using preg_match().
Step 4: Match All Patterns using preg_match_all().
Step 5: Replace Pattern using preg_replace().
Step 6: End of Code.
PROGRAM:
<!DOCTYPE html>
<html>
<body>
<?php
$a="Hello Hello World";
$b="/Hello/";
$c="/World/";
echo preg_match($b,$a);
echo "<br>";
echo preg_match_all($b,$a);
echo "<br>";
echo preg_replace ($c,"King",$a);
echo "<br>";
?>
</body>
</html>
OUTPUT:
RESULT:
Thus, the given program is executed and the output execute successfully.
Date : 1.2.24
Array Sorting
Exp. No.: 04
AIM:
To write a program for Array Sorting by using php.
PROCEDURE:
Step 1: Initiated the program.
Step 2: Define an Array.
Step 3: Sort the Array in Ascending Order
Step 4: Display the Sorted Array in Ascending Order.
Step 5: Sort the Array in Descending Order.
Step 6: Display the Sorted Array in Descending Order.
Step 7: End the code.
PROGRAM:
<!DOCTYPE html>
<html>
<body>
<?php
$a = array(4,2,21,20,42,27,46);
$b=sort($a);
echo "Sorted in ascending order:<br>";
foreach ($a as $b) {
echo $b . "<br>";
}
rsort($a);
echo "Sorted in decending order:<br>";
foreach ($a as $a) {
echo $a . "<br>";
}
?>
</body>
</html>
OUTPUT:
RESULT:
Thus, the given program is executed and the output execute successfully.
Date : 15.2.24
Conversion Of Array To Variable
Exp. No.: 05 And Variable To Array
AIM:
To write a program for Conversion of array to variable and variable to array by using
php.
PROCEDURE:
Step 1: Initialize an Array.
Step 2: Convert Array to Variables using list().
Step 3: Display the Variables.
Step 4: Convert Variables to Array using compact().
Step 5: Display Array.
Step 6: End the code.
PROGRAM:
<!DOCTYPE html>
<html>
<body>
<?php
$array = [2,4,20];
list($var1, $var2, $var3) = $array;
echo "Variables from array:" ."<br>";
echo "var1: $var1"."<br>";
echo "var2: $var2"."<br>";
echo "var3: $var3"."<br>";
echo "<br>";
$var1 = 21;
$var2 = 27;
$var3 = 46;
$array = compact('var1', 'var2', 'var3');
echo "Array from variables:" ."<br>";
print_r($array);
?>
</body>
</html>
OUTPUT:
RESULT:
Thus, the given program is executed and the output execute successfully.
Date : 22.2.24
Inheritance And Interface
Exp. No.: 06
AIM:
To write a program for inheritance and interface by using php.
PROCEDURE:
Step 1: Initialize an Array.
Step 2: Convert Array to Variables using list().
Step 3: Display the Variables.
Step 4: Convert Variables to Array using compact().
Step 5: Display Array.
Step 6: End the code.
PROGRAM:
<!DOCTYPE html>
<html>
<body>
<?php
// Define an interface
interface Animal {
public function makeSound();
}
// Define a parent class
class Mammal {
protected $name;
public function __construct($name) {
$this->name = $name;
}
OUTPUT:
RESULT:
Thus, the given program is executed and the output execute successfully.
Date : 29.2.24
Drop a Record From table using MYSQL
Exp. No.: 07
AIM:
To Write a PHP Program to drop a record from table employee.
PROCEDURE:
Step 1: Create a database name students
Step 2: Create a table employee.
Step 3: Create php program with server name =”localhost” , user name =”root”,
password=”” & dbase =”students.
Step 4: create dbase connection ($conn = mysqli_connect($servername,
$username, $password, $dbname);
Step 5: To delete table record $sql = "DELETE FROM employee WHERE
ECODE=1001";
Step 6: The employee record is deleted successfully
MYSQL Database:
1.Create DATABASE students;
2.Use students;
3. in order to create table EMPLOYEE given below :
ECODE ENAME GENDER GRADE GROSS
We write the following command :
CREATE TABLE employee ( ECODE integer , ENAME varchar(20) , GENDER
char(1) ,
GRADE char(2) , GROSS integer ) ;
4. e.g. to enter a row into EMPLOYEE table (created above), we write
command as :
INSERT INTO employee VALUES(1001 , ‘Ravi’ , ‘M’ , ‘E4’ , 50000);
Table created
Database OUTPUT:
PROGRAM:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "students";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM employee WHERE ECODE=1001";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
}
else
{
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
OUTPUT:
RESULT:
Thus, the given program is executed and the output execute successfully.
Date : 7.3.24
Mongo DB
Exp. No.: 08
AIM:
To write a program for retrieving data from MongoDB.
PROCEDURE:
Step 1: Import modules: Import MongoClient from 'mongodb'.
Step 2: Define URI: Set URI as 'mongodb://127.0.0.1:27017/students'.
Step 3: Define constants: Set dbName and collectionName.
Step 4: Create MongoClient instance with URI.
Step 5: Log connection attempt message.
Step 6: Define asynchronous email function.
Step 7: Inside email function:
a. Log successful connection message.
b. Use try-catch-finally block for error handling and closing client.
c. Access specified database and collection using db object.
d. Use distinct method to retrieve email addresses.
e. Log retrieved email addresses.
Step 8: Call email function to start retrieval process.
Step 9: Export email function for use in other modules.
PROGRAM:
const { MongoClient } = require('mongodb');
const uri = 'mongodb://127.0.0.1:27017/students';
console.log('Connected to MongoDB');
try {
const db = client.db(dbName);
const collection = db.collection(collectionName);
const emails = await collection.distinct('email');
email();
module.exports={ email };
OUTPUT:
RESULT:
Thus, the given program is executed and the output execute successfully.
Date : 14.3.24
PHP MINI PROJECT
Exp. No.: 09
Abstract:
This project aims to develop a user registration system using PHP and MySQL,
facilitating user sign-up by securely storing registration data in a MySQL database. Through
PHP scripting, the system ensures input validation, error handling, and scalability for
efficient user registration and management.
PROCEDURE:
Step 1: Design and create a MySQL database with tables to store user registration data.
Step 2: Create an HTML form with fields for username, email, and password to collect
user input..
Step 3: Write PHP code to validate form inputs, connect to the database, and insert
user data securely..
Step 4: Implement error handling mechanisms to display informative messages for
validation errors or database connection failures..
Step 5: Execute SQL queries to insert user registration data securely into the MySQL
database tables..
PROGRAM:
HTML CODE
DOCTYPE html>
<head>
<title>Register Form Design</title>
<link rel="stylesheet" type="text/css" href="style.css">
<body>
<div class="box">
<img src="student.png" class="user">
<h1>Register Here</h1>
<form name="myform2" action="register.php" method="POST">
<p>Username</p>
<input type="text" name="uname1" placeholder="Enter Username"
required="">
<p>Email</p>
<input type="Email" name="email" placeholder="Enter email id" required="">
<p>Password</p>
<input type="password" name="upswd1" placeholder="Enter Password"
required="">
<p>Retype Password</p>
<input type="password" name="upswd2" placeholder="Re-Enter Password"
required="">
<input type="submit" name="" value="Register">
</form>
</div>
</body>
</head>
</html>
PHP CODE
$uname1 = $_POST['uname1'];
$email = $_POST['email'];
$upswd1 = $_POST['upswd1'];
$upswd2 = $_POST['upswd2'];
if (!empty($uname1) || !empty($email) || !empty($upswd1) || !
empty($upswd2) )
{
RESULT:
:
CONCLUSION:
The PHP and MySQL-based user registration system provides a secure and
efficient platform for user sign-up. Through robust validation, error handling, and
database security measures, the project ensures data integrity and user
satisfaction. Continuous maintenance and updates will sustain the system's
reliability and resilience over time.