PHP Project File
PHP Project File
3. Conditional Statements
4. Loops
5. Arrays
6. Functions
7. String Manipulation
8. File Handling
9. Sessions
<?php
echo "Hello, World!";
?>
Output:
<?php
$integerVar = 10;
$floatVar = 20.5;
$stringVar = "Hello, PHP!";
$booleanVar = true;
<?php
$number = 15;
if ($number % 2 == 0) {
echo "$number is even.";
} else {
echo "$number is odd.";
}
?>
Output:
<?php
for ($i = 1; $i <= 10; $i++) {
echo $i . " ";
}
?>
Output:
<?php
$fruits = array("Apple", "Banana", "Cherry", "Date", "Elderberry");
<?php
function addNumbers($num1, $num2) {
return $num1 + $num2;
}
Output:
<?php
$string = "Hello, World!";
$reversedString = strrev($string);
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:
<?php
session_start();
$_SESSION['username'] = 'JohnDoe';
<!DOCTYPE html>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
echo "Name: " . $name;
}
?>
</body>
</html>
<?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);
}
?>
<?php
include 'config.php';
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();
?>