PHPPracticals
PHPPracticals
PHPPracticals
<?php
// For Loop
echo "For Loop:<br>";
for ($i = 1; $i <= 5; $i++) {
echo "Iteration $i<br>";
}
// While Loop
echo "<br>While Loop:<br>";
$j = 1;
while ($j <= 5) {
echo "Iteration $j<br>";
$j++;
}
// Do-While Loop
echo "<br>Do-While Loop:<br>";
$k = 1;
do {
echo "Iteration $k<br>";
$k++;
} while ($k <= 5);
?>
Output:
Question 2: Write a program in PHP to demonstrate conditional statements in PHP.
<?php
// Define variables
$temperature = 25;
$isRaining = true;
echo "<br>";
echo "<br>";
Output:
Question 4: Write a program in PHP to demonstrate including multiple files in PHP webpage.
<?php
include 'Que1.php';
include 'Que2.php';
include 'Que3.php';
?>
Output:
Question 5: Write a program in PHP for Creating and Calling your own functions.
<?php
// Define a function
function sayHello($name) {
// Function body
echo "Hello, " . $name . "!"."<br>";
}
?>
Output:
Question 6: Write a program in PHP to declare a class, creating an object, demonstrates writing
methods and declaring properties, accessing objects.
<?php
// Define a class called "Student"
class Student {
// Declare properties
public $name;
public $rollNumber;
// Constructor method
public function __construct($name, $rollNumber) {
$this->name = $name;
$this->rollNumber = $rollNumber;
}
?>
Output:
Question 7: Write a program in PHP to demonstrate String Functions.
<?php
// String for demonstration
$string = "Hello, World!";
// String Length
$length = strlen($string);
echo "String Length: " . $length . "<br>";
// Uppercase
$uppercase = strtoupper($string);
echo "Uppercase: " . $uppercase . "<br>";
// Lowercase
$lowercase = strtolower($string);
echo "Lowercase: " . $lowercase . "<br>";
// Substring
$substring = substr($string, 0, 5);
echo "Substring: " . $substring . "<br>";
// Replace
$replaced = str_replace("World", "PHP", $string);
echo "Replace: " . $replaced . "<br>";
// Concatenation
$concatenated = $string . " PHP";
echo "Concatenation: " . $concatenated . "<br>";
// Trim
$whitespaceString = " Trim this string ";
$trimmed = trim($whitespaceString);
echo "Trimmed: " . $trimmed . "<br>";
// Explode
$csv = "apple,banana,cherry";
$fruits = explode(",", $csv);
echo "Explode: ";
print_r($fruits);
?> <?php
// String for demonstration
$string = "Hello, World!";
// String Length
$length = strlen($string);
echo "String Length: " . $length . "<br>";
// Uppercase
$uppercase = strtoupper($string);
echo "Uppercase: " . $uppercase . "<br>";
// Lowercase
$lowercase = strtolower($string);
echo "Lowercase: " . $lowercase . "<br>";
// Substring
$substring = substr($string, 0, 5);
echo "Substring: " . $substring . "<br>";
// Replace
$replaced = str_replace("World", "PHP", $string);
echo "Replace: " . $replaced . "<br>";
// Concatenation
$concatenated = $string . " PHP";
echo "Concatenation: " . $concatenated . "<br>";
// Trim
$whitespaceString = " Trim this string ";
$trimmed = trim($whitespaceString);
echo "Trimmed: " . $trimmed . "<br>";
// Explode
$csv = "apple,banana,cherry";
$fruits = explode(",", $csv);
echo "Explode: ";
print_r($fruits);
?>
Output:
Question 8: 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>
<title>User Registration Form</title>
</head>
<body>
<h2>User Registration</h2>
<form method="post" action="Que8.php">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required><br><br>
<label for="email">Email:</label>
<input type="email" name="email" id="email" required><br><br>
Output:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$name = $_POST["name"];
$email = $_POST["email"];
// Perform basic data validation (you can add more validation rules)
$errors = [];
if (empty($name)) {
$errors[] = "Username is required.";
}
if (empty($email)) {
$errors[] = "Email is required.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email format.";
}
2. Create a database
Output:
3. Use database
Output:
4. Create Table
Output:
5. Add data into a table
Output:
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: