A Simple Webpage Using PHP
A Simple Webpage Using PHP
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #2C6BF5; /* light gray background */
}
</style>
<title>My PHP Webpage</title> </head>
<body>
<?php
// Define a variable
$message = "Hello, world!"; // Output the variable
echo "<h1>" . $message . "</h1>"; ?>
<p>This is a simple webpage using PHP.</p> </body>
</html>
OUTPUT :
2. IF – ELSE STATEMENTS IN PHP
<?php
// Use an if else statement to check if x is less than 10
$x = 10;
if ($x < 10) {
echo "x is less than 10.<br>";
} else {
echo "x is not less than 10.<br>";
}
?>
OUTPUT :
<?php
// Use if elseif else statement to check largest of two number.
$x=20;
$y =15; $z=22; if($x>$y && $x>$z){
echo " $x is largest";
}
elseif($y>$x && $y>$z){
echo " $y is largest";
} else{ echo " $z is largest";
}
?>
OUTPUT :
22 is largest
4. SWITCH STATEMENT IN PHP
<?php
// Use a switch statement to check the value of x
$x = 5;
switch ($x) {
case 1:
echo "x is equal to 1.<br>";break;
case 2:
echo "x is equal to 2.<br>";
break;
case 3:
echo "x is equal to 3.<br>";
break;
default:
echo "x is not equal to 1, 2, or 3.<br>"; break;
} ?>
OUTPUT:
X is not equal to 1, 2, or 3.
5. WHILE LOOP STATEMENT IN PHP
<?php
// Loop through numbers 1 to 10 using a while loop
echo "Using a while loop:<br>";
$i = 1;
while ($i <= 10) {
echo $i . " ";
$i++;
}
echo "<br>";
?>
OUTPUT:
<?php
// Use a do-while loop to print the numbers from 1 to 5
echo "Using a do-while loop:<br>";
$x = 1;
do {
echo $x . " ";
$x++;
} while ($x <= 5);
echo "<br>";
?>
OUTPUT:
<?php
// Loop through numbers 1 to 10 using a for loop
echo "Using a for loop:<br>";
for ($i = 1; $i <= 10; $i++) {
echo $i . " ";
}
?>
OUTPUT:
<?php
// Loop through an array of fruits using a foreach loop
echo "Using a foreach loop:<br>";
$fruits = array("apple", "banana", "orange", "kiwi");
foreach ($fruits as $fruit) {
echo $fruit . " "; }
echo "<br>";
?>
OUTPUT:
<?php
// create an indexed array
$name_one = array("zack", "anthony", "ram", "salim", "raghav");
// Accessing the elements directly
echo "Accessing the 1st array elements directly:\n";
echo $name_one[2], "\n";
echo $name_one[0], "\n";
echo $name_one[4], "\n ";
?>
OUTPUT:
<?php
// One way to create an associative array
$name_two = array("zack"=>"zara", "anthony"=>"any","ram"=>"rani",
"salim"=>"sara","raghav"=>"ravina");
// Accessing the elements directly
echo "Accessing the elements directly:\n";
echo $name_two["zack"], "\n";
echo $name_two["salim"], "\n <br>";
?>
OUTPUT:
<?php
// Defining a multidimensional array
$favorites = array(
array(
"name" => "Dave Punk",
"mob" => "5689741523",
"email" => "davepunk@gmail.com, <br>"
),
array(
"name" => "Monty Smith",
"mob" => "2584369721",
"email" => "montysmith@gmail.com, <br>"
),
array("name" => "John Flinch",
"mob" => "9875147536",
"email" => "johnflinch@gmail.com, <br>"
),
);
// Accessing elements
echo "Dave Punk email-id is: " . $favorites[0]["email"], "\n";
echo "John Flinch mobile number is: " . $favorites[2]["mob"],"\n";
?>
OUTPUT:
<?php
$numbers = readline("Enter the numbers:");
$numbers_array = explode(",", $numbers);
$numbers_array = array_map('trim', $numbers array);
$numbers_array = array_map('intval',
$numbers_array); sort($numbers_array);
echo "sorted numbers:";
echo implode(",", $numbers_array);
?>
Output:
Enter the numbers : 5,6,7,3,2
sorted numbers : 2,3,5,6,7
13. CREATION OF SESSION
<?php
// Start the session
session_start();
// Set session variables
$_SESSION['username'] = 'JohnDoe';
$_SESSION['email'] = 'johndoe@example.com';
?>
<!DOCTYPE html>
<html>
<head>
<title>Session Test</title>
</head>
<body>
<h1>Welcome, <?php echo $_SESSION['username']; ?>!</h1>
<p>Your email address is: <?php echo $_SESSION['email']; ?></p>
</body>
</html>
OUTPUT:
Welcome, JohnDoe!
Your email address is: johndoe@example.com
14. CREATION OF COOKIES
<?php
// Set the cookie
setcookie("username", "JohnDoe", time() + 86400); // expires after 1 day ?>
<!DOCTYPE html>
<html>
<head>
<title>Cookie Test</title>
</head>
<body>
<?php
// Check if the cookie is set
if(isset($_COOKIE['username'])) {
echo "Welcome back, " . $_COOKIE['username'] . "!"; } else {
echo "You are a new visitor!"; }
?> </body> </html>
OUTPUT:
<?php
// Open a file for writing
$myfile = fopen("example.txt", "w") or die("Unable to open file!");
// Write some text to the file
$txt = "Hello, world!";
fwrite($myfile, $txt);
// Close the file
fclose($myfile);
// Open the file for reading
$myfile = fopen("example.txt", "r") or die("Unable to open file!");
// Read the contents of the file
echo fread($myfile,filesize("example.txt"));
// Close the file fclose($myfile);
// Delete the file unlink("example.txt");
?>
OUTPUT:
Hello, world!
16. SIMPLE APPLICATION USING POST METHOD.
1. postexample.html
<html>
<head>
<title>Post Example</title>
</head>
<body>
<h1>Post Example</h1>
<label for="name">Name:</label>
</form>
</body>
</html>
2. post.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
$name=$_POST['name'];
echo"<p>hello,$name!</p>";
?>
Output:
Hello, john!
17. SIMPLE APPLICATION USING GET METHOD.
1. getexample.html
<html>
<head>
<title>Get Example</title>
</head>
<body>
<h1>Get Example</h1>
<label for="name">Name:</label>
</form>
</body>
</html>
2. get.php
<?php
if (isset($_GET['submit'])) {
$name = $_GET['name'];
?>
Output:
Hello , Eren!
18. AN APPLICATION WITH INPUT VALIDATIONS.
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<style>
.error{color:red}
</style>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["email"])) {
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (empty($_POST["password"])) {
} else {
$password = test_input($_POST["password"]);
if (strlen($password) < 8) {
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
?>
<h2>Login Form</h2>
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Email:</label>
<br><br>
<label>Password:</label>
<br><br>
<?php
?>
</body>
</html>
Output:
19. PHP AND MYSQL CONNECTIVITY
<?php
// Database configuration
$servername = "localhost";
$username = "sample";
$password = "";
$dbname = "sample";
// Create a connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Close connection
mysqli_close($conn);
?>
OUTPUT :
Connected successfully
20. CREATING SIMPLE TABLE WITH CONSTRAINS
<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Execute query
if (mysqli_query($conn, $sql)) {
echo "Table Users created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
OUTPUT :
<?php
// Connect to database
$servername = "localhost";
$username = "sample";
$password = "";
$dbname = "sample";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
<?php
// Connect to database
$servername = "localhost";
$username = "sample";
$password = "";
$dbname = "sample";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Delete a row
$sql = "DELETE FROM Users WHERE id=12";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
OUTPUT:
// Update a row
$sql = "UPDATE Users SET email='newemail@example.com' WHERE id=11";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: <br>" . mysqli_error($conn);
}
OUTPUT:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
// Check connection
if ($conn->connect_error) {
myguests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
} else {
$conn->close();
?>
Output :
<?php
// Connect to database
$servername = "localhost";
$username = "sample";
$password = "";
$dbname = "sample";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Search data by first name
$firstname = "John";
$sql = "SELECT * FROM Users WHERE firstname='$firstname'";
$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"]. " - Email: " .
$row["email"]. "<br>";
}
} else {
echo "0 results";
}
// Search data by last name
$lastname = "kumar";
$sql = "SELECT * FROM Users WHERE lastname='$lastname'";
$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"]. " - Email: " .
$row["email"]. "<br>";
}
} else {
echo "0 results";
}
// Search data by email
$email = "ramkumar@example.com";
$sql = "SELECT * FROM Users WHERE email='$email'";
$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"]. " - Email: " .
$row["email"]. "<br>";
}
} else {
echo "0 results";
}
// Close connection
mysqli_close($conn);
?>
OUTPUT :