0% found this document useful (0 votes)
6 views

PHP Project File

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PHP Project File

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Index

Serial Number Topic


1. Basic PHP Syntax

2. Variables and Data Types

3. Conditional Statements

4. Loops

5. Arrays

6. Functions

7. String Manipulation

8. File Handling

9. Sessions

10. Form Handling


MySQL Database
11.
Connection
12. MySQL Database Query
1.Write a PHP script to print "Hello, World!" to the screen.

<?php
echo "Hello, World!";
?>

Output:

2. Create a PHP script that declares variables of different


data types: integer, float, string, and boolean. Print the
variables.

<?php
$integerVar = 10;
$floatVar = 20.5;
$stringVar = "Hello, PHP!";
$booleanVar = true;

echo "Integer: " . $integerVar . "<br>";


echo "Float: " . $floatVar . "<br>";
echo "String: " . $stringVar . "<br>";
echo "Boolean: " . ($booleanVar ? 'true' : 'false') . "<br>";
?>
Output:

3. Write a PHP script that checks if a number is even or odd.

<?php
$number = 15;

if ($number % 2 == 0) {
echo "$number is even.";
} else {
echo "$number is odd.";
}
?>

Output:

4. Write a PHP script to print numbers from 1 to 10 using a


for loop.

<?php
for ($i = 1; $i <= 10; $i++) {
echo $i . " ";
}
?>

Output:

5. Create an array of fruits and print each fruit using a


foreach loop.

<?php
$fruits = array("Apple", "Banana", "Cherry", "Date", "Elderberry");

foreach ($fruits as $fruit) {


echo $fruit . "<br>";
}
?>
Output:
6. Write a PHP function that takes two numbers as
arguments and returns their sum.

<?php
function addNumbers($num1, $num2) {
return $num1 + $num2;
}

echo "Sum: " . addNumbers(10, 20);


?>

Output:

7. Write a PHP script that reverses a string.

<?php
$string = "Hello, World!";
$reversedString = strrev($string);

echo "Original String: " . $string . "<br>";


echo "Reversed String: " . $reversedString;
?>

Output:
8. Write a PHP script to read the content of a file named
"example.txt" and print it.

<?php
$filename = "example.txt";

if (file_exists($filename)) {
$fileContent = file_get_contents($filename);
echo "File Content:<br>" . nl2br($fileContent);
} else {
echo "File does not exist.";
}
?>

Output:

9. Write a PHP script to start a session, set a session


variable, and retrieve it.

<?php
session_start();

$_SESSION['username'] = 'JohnDoe';

echo "Username: " . $_SESSION['username'];


?>

10. Create an HTML form with a text input and a submit


button. Write a PHP script to handle the form submission
and display the input value.

<!DOCTYPE html>
<html>
<body>

<form method="post" action="handle_form.php">


Name: <input type="text" name="name">
<input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
echo "Name: " . $name;
}
?>
</body>
</html>

11. Write a PHP script to connect to a MySQL database

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";

// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);

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

12. Write a PHP script to fetch data from a table named


users.

<?php
include 'config.php';

$sql = "SELECT id, name, email FROM users";


$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["name"] . " - Email: " . $row["email"] . "<br>";
}
} else {
echo "0 results";
}

$conn->close();
?>

You might also like