PHP Programs 1-10
PHP Programs 1-10
Output: Output:
1 2 3 4 5 6 7 8 9 10
Summer
While loop: Winter
Autumn
<?php Rainy
$num=2;
While($num < 12){
$num += 2;
echo $num, “\n”;
}
?>
Output:
4 6 8 10 12
Do while loop:
<?php
$n=1;
Do{
echo $n, “\n”;
$n++;
}
While($n<=10);;
?>
Output:
1 2 3 4 5 6 7 8 9 10
<?php
$season =
array(“Summer”,”Winter”,”Autumn”,”Rainy”);
foreach($season as $element) {
echo”$element”;
Practicle 02 : Write a program in PHP to
demonstrate conditional statements in PHP.
Output:
number is equal to 20
Parcticle 03 : Write a program in PHP to
Create an Array, Insert elements in Array,
Accessing Elements form Array and
Displaying elements of Arrays.
Home.php
<?php require "my_variables.php"; ?><br>
<html>
<head>
<title><?php ($home_page); ?></title>
</head>
<body>
<?php include "header.php"; ?>
<?php include "menu.php"; ?>
<?php include "footer.php"; ?>
<h1>Welcome to Our Website!</h1>
<p>Here you will find lots of useful resources.</p>
</body>
</html>
<?php require "my_functions.php"; ?>
my_functions.php
<html>
<head>
</head>
<body>
<section style="background-color:#bcdeef;width: 30%;border: 0px solid light;">
<h6 style>Copyright <b>©</b> 2024 svm udgir</h6>
</section>
</body>
</html>
my_Variables.php
<html>
<head>
</head>
<body>
<section style="background-color:#bcdeef;width: 30%; border: 0px solid light;">
<h3 style> SVM UDGIR</h3>
</section>
</body>
</html>
header.php
<html>
<head>
</head>
<body>
<a href=Home>Home |</a>
</body>
</html>
menu.php
<html>
<head>
</head>
<body>
<a href=About>About |</a>
</body>
</html>
footer.php
<html>
<head>
</head>
<body>
<a href="contact">Contact</a>
</body>
</html>
Output:
SVM UDGIR
Creating function:
<?php
function funcGeek()
{
echo "This is Geeks for Geeks";
}
?>
Output:
This is Geeks for Geeks
Calling function:
<?php
proGeek(2, 3, 5);
?>
Output:
The product is 30
Practicle 06 : Write a program in PHP to declare a class, creating an object, demonstrate
writing methods and declaring properties, Accessing objects.
<?php
//include class definition
require "Rectangle.php"
Output:
0
0
600
1750
Practicle 07 :- Write a program in PHP to demonstrate string functions.
<?php
echo strlen("Hello world!");
?>
Output:
12
<?php
echo str_word_count(“Hello world!”);
?>
Output:
2
<?php
echo strrev(“Hello world!”); //output
?>
Output:
!dlrow olleH
<?php
echo strpos(“Hello World!”, “World”);
?>
Output:
6
<?php
echo str_replace(“world”, “Dolly”, “Hello world!”);
?>
Output:
Hello Dolly!
Practicle 08 : Write a program in PHP to create / design a user
registration form, validate form data and display entered form data
on webpage.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if($_SERVER["REQUEST_METHOD"] =="POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Output:
Practicle 09 : Use mySQL in command line mode for following operations:
<?php
$servername = “localhost”;
$username = “root”;
$password = “”;
$dbname = “my_DB”;
//Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//check connection
If(!$conn) {
die(“Connection failed: “ .mysqli_connect_error());
}
echo “Connected successfully”;
?>
Output:
Connected successfully
<?php
$servername = “localhost”;
$username = “root”;
$password = “”;
//Create connection
$conn = mysqli_connect($servername, $username, $password);
//check connection
If(!$conn) {
die(“Connection failed: “ .mysqli_connect_error());
}
//Create database
$sql = “CREATE DATABASE myDB”;
if (mysqli_query($conn, $sql)) {
echo “Database created successfully”;
} else {
echo “Error creating database: “ .mysqli_error($conn);
}
Mysqli_close($conn);
?>
Output:
Database created successfully
<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDB”;
//Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//check connection
If(!$conn) {
die(“Connection failed: “ .mysqli_connect_error());
}
//sql to create table
$sql = “CREATE TABLE MyGuests (
Id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR (50))”;
if (mysqli_query($conn, $sql))
{
echo “Table MyGuests created successfully”;
} else {
echo “Error creating table: “ .mysqli_error($conn);
}
Mysqli_close($conn);
?>
Output:
Table MyGuests created successfully
<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDB”;
//Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//check connection
If(!$conn) {
die(“Connection failed: “ .mysqli_connect_error());
}
$sql = “INSERT INTO MyGuests(firstname, lastname, email)
VALUES (‘Harshad’, ‘Badnale’, ‘[email protected]’)”;
if(mysqli_query($conn, $sql)) {
echo “New record created successfully”;
} else {echo “Error: “ .$sql. “<br>” .mysqli_error($conn);
}
Mysqli_close($conn);
?>
Output:
New record created successfully
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Output:
id: 1 – Name: John Doe
id: 2 – Name: Mary Moe
id: 3 – Name: Julie Dooley
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_errno) {
printf("Connect failed: %s", $mysqli->connect_error);
exit();
}
$sql = "RENAME TABLE Student TO Employee ";
if ($mysqli->query($sql)) {
printf("table renamed successfully.");
}
if ($mysqli->errno) {
printf("table could not rename: %s", $mysqli->error);
}
$mysqli->close();
Output:
table renamed successfully.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
Output:
Record deleted successfully
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if($mysqli→connect_errno ) {
printf("Connect failed: %s<br />", $mysqli→connect_error);
exit();
}
printf('Connected successfully.<br />');
Output:
Connected successfully
Table dropped successfully.
Practicle 10: Write a program in PHP to connecting to MySQL and selecting
the database,
Executing simple queries, and retrieving query results.
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username "username";
$password = "password";
$dbname= "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: .mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " .$row["id"]. " - Name: . $row["firstname"]. "
" .$row["lastname"]. "<br>";
} else {
} echo "0 results";
}
mysqli_close($conn);
?>
</body>
</html>
Output: -
id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
Retrieving Query Results:-
<html>
<body>
<?php
$username = "root";
$password = "";
$database = "mydb";
$result = mysqli_query($conn,$query);
if(mysqli_num_rows($result)>0)
{
while ($row = mysqli_fetch_assoc($result))
$field1name = $row["id"];
$field2name = $row["name"];
$field3name = $row["class"];
$field4name = $row["address"];
echo '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
</tr>';
}
}
mysqli_close($conn);
?>
</body>
</html>
Output:
ID Emp Name Class Name Address
1 Paresh Waichale MCA Latur
3 Harshad Badnale MBBS Latur
4 Sainath kedase MBBS Latur
5 Wishnu Wagmare MBBS Udgir
6 Partik Dhoble MBBS Udgir
8 Sumit Shelhale MCS Latur
11 Parbhudha Firange MCS Latur