PHPPracticals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Question 1: Write a program in PHP to demonstrate looping statements in PHP.

<?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);

// Foreach Loop (for Arrays)


echo "<br>Foreach Loop (for Arrays):<br>";
$colors = array("red", "green", "blue", "yellow", "orange");
foreach ($colors as $color) {
echo "Color: $color<br>";
}

?>
Output:
Question 2: Write a program in PHP to demonstrate conditional statements in PHP.
<?php
// Define variables
$temperature = 25;
$isRaining = true;

// Example 1: Simple if statement


if ($temperature > 30) {
echo "It's a hot day.";
} else {
echo "It's not too hot.";
}

echo "<br>";

// Example 2: if-else statement


if ($isRaining) {
echo "Bring an umbrella.";
} else {
echo "No need for an umbrella.";
}

echo "<br>";

// Example 3: if-else if-else statement


$score = 85;

if ($score >= 90) {


echo "You got an A.";
} elseif ($score >= 80) {
echo "You got a B.";
} elseif ($score >= 70) {
echo "You got a C.";
} elseif ($score >= 60) {
echo "You got a D.";
} else {
echo "You failed.";
}
?>
Output:
Question 3: Write a program in PHP to Create an Array, Insert elements in Array, Accessing elements
from Array and Displaying elements of Arrays.
<?php
// Create an array and insert elements into it
$fruits = array("Apple", "Banana", "Cherry", "Date");

// Access and display elements from the array


echo "Accessing Elements from the Array:<br>";
echo "First Element: " . $fruits[0] . "<br>";
echo "Second Element: " . $fruits[1] . "<br>";
echo "Third Element: " . $fruits[2] . "<br>";
echo "Fourth Element: " . $fruits[3] . "<br>";

// Display all elements of the array


echo "<br>Displaying all elements of the Array:<br>";
foreach ($fruits as $fruit) {
echo $fruit . "<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>";
}

// Call the function with an argument


sayHello("John"); // Output: Hello, John!

// Call the function with a different argument


sayHello("Alice"); // Output: Hello, Alice!

?>

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;
}

// Method to display student information


public function displayInfo() {
echo "Student Name: " . $this->name . "<br>";
echo "Roll Number: " . $this->rollNumber . "<br>";
}
}

// Create an object of the "Student" class


$student1 = new Student("Alice", "S12345");

// Access and display the object's properties


echo "Accessing object properties:<br>";
echo "Student Name: " . $student1->name . "<br>";
echo "Roll Number: " . $student1->rollNumber . "<br>";

// Call the "displayInfo" method to display student information


echo "<br>Calling object method:<br>";
$student1->displayInfo();

?>
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>

<input type="submit" value="Register">


</form>
</body>
</html>

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.";
}

// If there are validation errors, display them


if (!empty($errors)) {
echo "<h2>Registration Failed</h2>";
echo "<ul>";
foreach ($errors as $error) {
echo "<li>$error</li>";
}
echo "</ul>";
} else {
// If validation passes, display the entered data
echo "<h2>Registration Successful</h2>";
echo "<p>Name: $name</p>";
echo "<p>Email: $email</p>";
}
}
?>
Output:
Question 9: Use MySQL in command line mode for following operations.
1. Show databases
Output:

2. Create a database
Output:
3. Use database
Output:

4. Create Table
Output:
5. Add data into a table
Output:

6. Select data from table


Output:
7. Rename a table
Output:

8. Delete data from table


Output:
9. Delete a table
Output:
Question 10: Write a program in PHP to connect to MySQL, and selecting the database, executing
simple queries, and retrieving query results.
<?php
// Database credentials
$hostname = 'localhost'; // MySQL server hostname
$username = 'root'; // MySQL username
$password = ''; // MySQL password
$database = 'mydatabase'; // Database name

// Create a connection to the MySQL database


$connection = new mysqli($hostname, $username, $password, $database);

// Check if the connection was successful


if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}

// Simple query example: Select all records from a table


$query = "SELECT * FROM stuinfo";

// Execute the query


$result = $connection->query($query);

// Check if the query was executed successfully


if (!$result) {
die("Query failed: " . $connection->error);
}

// Fetch and display the query results


if ($result->num_rows > 0) {
echo "<table><tr><th>Roll</th><th>Name</th><th>Eame</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["roll"] . "</td><td>" . $row["stu_name"] . "</td><td>" .
$row["stu_email"] . "</td></tr>";
}
echo "</table>";
} else {
echo "No results found.";
}

// Close the database connection


$connection->close();
?>
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy