All Important PHP Practicals with Answers
1. Check whether a number is positive, negative, or zero.
<?php
$num = -5;
if($num > 0){
echo "Positive";
} elseif($num < 0){
echo "Negative";
} else {
echo "Zero";
}
?>
2. Check whether a number is even or odd.
<?php
$num = 10;
if($num % 2 == 0){
echo "Even";
} else {
echo "Odd";
}
?>
3. Check whether a string or number is a palindrome.
<?php
$str = "madam";
if($str == strrev($str)){
echo "Palindrome";
} else {
echo "Not Palindrome";
}
?>
4. Print numbers from 1 to 10 using for and while loop.
<?php
echo "Using for loop:<br>";
for($i = 1; $i <= 10; $i++) {
echo $i . " ";
}
echo "<br>Using while loop:<br>";
$i = 1;
while($i <= 10) {
echo $i . " ";
$i++;
}
?>
5. Print multiplication table for a given number.
<?php
$num = 5;
for($i = 1; $i <= 10; $i++) {
echo "$num x $i = " . ($num * $i) . "<br>";
}
?>
6. Create and loop through indexed, associative, and multidimensional arrays.
<?php
$indexed = array("apple", "banana", "cherry");
foreach($indexed as $val) {
echo $val . "<br>";
}
$assoc = array("name" => "Dreacky", "age" => 20);
foreach($assoc as $key => $val) {
echo "$key: $val<br>";
}
$multi = array(
array("Volvo", 22, 18),
array("BMW", 15, 13)
);
for($i = 0; $i < count($multi); $i++) {
for($j = 0; $j < count($multi[$i]); $j++) {
echo $multi[$i][$j] . " ";
}
echo "<br>";
}
?>
7. Perform array operations: count elements, add item, remove item, sort array.
<?php
$arr = array("banana", "apple", "cherry");
echo "Count: " . count($arr) . "<br>";
$arr[] = "date"; // Add
unset($arr[1]); // Remove "apple"
sort($arr); // Sort
print_r($arr);
?>
8. Use string functions: strcmp(), substr(), str_replace(), strrev(), strtolower(), strlen().
<?php
echo strcmp("abc", "abc"); // 0
echo substr("hello", 1, 3); // ell
echo str_replace("world", "PHP", "Hello world");
echo strrev("PHP"); // PHP reversed
echo strtolower("HELLO");
echo strlen("hello");
?>
9. Declare a user-defined function with default and variable-length arguments.
<?php
function greet($name = "User") {
echo "Hello $name";
}
greet("Dreacky");
function sum(...$nums) {
return array_sum($nums);
}
echo sum(1, 2, 3);
?>
10. Create a PHP class with constructor, destructor, and access modifiers; compute area of
rectangle.
<?php
class Rectangle {
public $length, $width;
function __construct($l, $w) {
$this->length = $l;
$this->width = $w;
}
function area() {
return $this->length * $this->width;
}
function __destruct() {
echo "Destroyed";
}
}
$obj = new Rectangle(10, 5);
echo $obj->area();
?>
11. Demonstrate single, multilevel, and hierarchical inheritance.
<?php
class A {
function showA() { echo "Class A<br>"; }
}
class B extends A {
function showB() { echo "Class B<br>"; }
}
class C extends B {
function showC() { echo "Class C<br>"; }
}
$obj = new C();
$obj->showA();
$obj->showB();
$obj->showC();
?>
12. Demonstrate method overriding and function overloading.
<?php
class Base {
function greet() {
echo "Hello from Base";
}
}
class Child extends Base {
function greet() {
echo "Hello from Child";
}
}
$obj = new Child();
$obj->greet();
// Overloading with __call
class Overload {
function __call($name, $args) {
echo "Called method $name with args " . implode(", ", $args);
}
}
$o = new Overload();
$o->sum(1, 2);
?>
13. Create a login form and manage login using session.
<?php
// login.php
session_start();
$_SESSION['username'] = $_POST['username'];
echo "Logged in as " . $_SESSION['username'];
?>
14. Use session to count page views using isset and unset.
<?php
session_start();
if(isset($_SESSION['views'])) {
$_SESSION['views']++;
} else {
$_SESSION['views'] = 1;
}
echo "Views: " . $_SESSION['views'];
?>
15. Connect to MySQL and perform INSERT, UPDATE, DELETE, SELECT.
<?php
$conn = mysqli_connect("localhost", "root", "", "test");
mysqli_query($conn, "INSERT INTO users (name) VALUES ('Dreacky')");
mysqli_query($conn, "UPDATE users SET name='Updated' WHERE id=1");
mysqli_query($conn, "DELETE FROM users WHERE id=1");
$result = mysqli_query($conn, "SELECT * FROM users");
while($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . "<br>";
}
mysqli_close($conn);
?>
16. Create a form with multiple submit buttons to perform string operations.
<form method="post">
<input type="text" name="str">
<input type="submit" name="reverse" value="Reverse">
<input type="submit" name="upper" value="Uppercase">
<input type="submit" name="lower" value="Lowercase">
<input type="submit" name="length" value="Length">
</form>
<?php
$str = $_POST['str'];
if(isset($_POST['reverse'])) echo strrev($str);
if(isset($_POST['upper'])) echo strtoupper($str);
if(isset($_POST['lower'])) echo strtolower($str);
if(isset($_POST['length'])) echo strlen($str);
?>