PHP is a widely-used open-source scripting language primarily used for web development. It can be embedded into HTML, and it is especially good for tasks like creating dynamic web pages, handling forms, and manipulating databases. Below are some exercises in various aspects of PHP to give you a hands-on understanding of the language.
The simplest PHP program. Save it as `hello.php`.
<?php
echo "Hello, World!";
?>
Run it by navigating to its location via a web server like Apache. You'll see "Hello, World!" displayed.
Create a PHP script that concatenates two strings.
<?php
$string1 = "Hello,";
$string2 = " World!";
echo $string1 . $string2;
?>
Demonstrate basic arithmetic operations: addition, subtraction, multiplication, division.
<?php
$a = 10;
$b = 20;
echo "Addition: " . ($a + $b) . "<br>";
echo "Subtraction: " . ($a - $b) . "<br>";
echo "Multiplication: " . ($a * $b) . "<br>";
echo "Division: " . ($a / $b) . "<br>";
?>
Create an indexed array and an associative array.
<?php
$indexedArray = [1, 2, 3, 4, 5];
$assocArray = ['name' => 'John', 'age' => 30];
echo $indexedArray[2]; // Output will be 3
echo $assocArray['name']; // Output will be John
?>
Implement a simple age verification system.
<?php
$age = 18;
if ($age < 18) {
echo "You are too young.";
} elseif ($age >= 18 && $age < 21) {
echo "You are young but not a minor.";
} else {
echo "You are over 21.";
}
?>
Create a loop that prints numbers 1 to 10.
<?php
for ($i = 1; $i <= 10; $i++) {
echo $i . "<br>";
}
$array = [1, 2, 3];
foreach ($array as $element) {
echo $element . "<br>";
}
?>
Create a function that takes two numbers and returns their sum.
<?php
function add($a, $b) {
return $a + $b;
}
echo add(3, 4); // Output will be 7
?>
Create a simple HTML form and handle its submission with PHP.
<form action="handle.php" method="post">
Name: <input type="text" name="name"><br>
Age: <input type="number" name="age"><br>
<input type="submit">
</form>
<?php
$name = $_POST['name'];
$age = $_POST['age'];
echo "Hello, $name. You are $age years old.";
?>
For this, you'll need to have a MySQL server running.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Create a simple session counter.
<?php
session_start();
if (isset($_SESSION['count'])) {
$_SESSION['count']++;
} else {
$_SESSION['count'] = 1;
}
echo "You have visited this page " . $_SESSION['count'] . " times.";
?>
These are just basic exercises to get you started. PHP offers many more features like file handling, regular expressions, object-oriented programming, and so on.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here