10 Write a PHP program to insert new item in array on any
position in PHP. <?php $array = [1, 2, 3, 4, 5]; $newItem = 6; $position = 2; // Replace with desired position array_splice($array, $position, 0, $newItem); print_r($array); ?>
11 Write a PHP script to implement constructor and
destructor. <?php class MyClass { public function __construct() { echo "Constructor called<br>"; } public function someMethod() { echo "Method called<br>"; } public function __destruct() { echo "Destructor called<br>"; } } $obj = new MyClass(); $obj->someMethod(); unset($obj); // Destructor will be called when object is unset ?>
12 Write a PHP script to implement form handling using get
13 Write a PHP script to implement form handling using
post method. <!-- HTML Form --> <form action="process_post.php" method="post"> Name: <input type="text" name="name"> <input type="submit" value="Submit"> </form> <!-- process_post.php --> <?php $name = $_POST['name']; echo "Hello, $name!"; ?>
14 Write a PHP script that receive form input by the method
post to check the number is prime or not. <!-- HTML Form --> <form action="check_prime.php" method="post"> Number: <input type="text" name="number"> <input type="submit" value="Check Prime"> </form> <!-- check_prime.php --> <?php $number = $_POST['number']; $isPrime = true; for ($i = 2; $i <= sqrt($number); $i++) { if ($number % $i == 0) { $isPrime = false; break; } } if ($isPrime) { echo "$number is Prime."; } else { echo "$number is not Prime."; } ?>
15 Write a PHP script that receive string as a form input.