WT Slip Solution
WT Slip Solution
Q1: Write a PHP script to get the PHP version and configuration information.
<?php
// Display PHP version and configuration info
phpinfo();
?>
Q2: Design a HTML form to accept a string. Write a PHP script to:
a) Count the total number of vowels
b) Show occurrences of each vowel
<!DOCTYPE html>
<html>
<body>
<form method="post">
Enter a string: <input type="text" name="inputStr">
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$str = strtolower($_POST["inputStr"]);
$vowels = ['a', 'e', 'i', 'o', 'u'];
$total = 0;
Q2: Create a form to accept two numbers and perform arithmetic operations using radio
buttons and default parameter.
<!DOCTYPE html>
<html>
<body>
<form method="post">
Number 1: <input type="text" name="num1"><br>
Number 2: <input type="text" name="num2"><br>
Operation: <br>
<input type="radio" name="op" value="add" checked> Add<br>
<input type="radio" name="op" value="sub"> Subtract<br>
<input type="radio" name="op" value="mul"> Multiply<br>
<input type="radio" name="op" value="div"> Divide<br>
<input type="submit" name="submit" value="Calculate">
</form>
</body>
</html>
<?php
function calculate($a, $b, $op = "add") {
switch($op) {
case "add": return $a + $b;
case "sub": return $a - $b;
case "mul": return $a * $b;
case "div": return ($b != 0) ? $a / $b : "Cannot divide by zero";
default: return "Invalid operation";
}
}
if(isset($_POST['submit'])) {
$n1 = $_POST['num1'];
$n2 = $_POST['num2'];
$op = $_POST['op'];
echo "Result: " . calculate($n1, $n2, $op);
}
?>
Slip 3
Q1: Write a PHP script to display time table of your class (use HTML table tags in echo).
<?php
echo "<table border='1'>";
echo "<tr><th>Day</th><th>Subject</th></tr>";
echo "<tr><td>Monday</td><td>Web Tech</td></tr>";
echo "<tr><td>Tuesday</td><td>DBMS</td></tr>";
echo "<tr><td>Wednesday</td><td>OOP</td></tr>";
echo "</table>";
?>
Q2: Design a form to accept 5 item details and display bill in tabular format using explode()
and only 4 text boxes.
<!DOCTYPE html>
<html>
<body>
<form method="post">
Item Codes (comma-separated): <input type="text" name="codes"><br>
Item Names (comma-separated): <input type="text" name="names"><br>
Units Sold (comma-separated): <input type="text" name="units"><br>
Rates (comma-separated): <input type="text" name="rates"><br>
<input type="submit" name="submit" value="Generate Bill">
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
$codes = explode(",", $_POST['codes']);
$names = explode(",", $_POST['names']);
$units = explode(",", $_POST['units']);
$rates = explode(",", $_POST['rates']);
echo "<table
border='1'><tr><th>Code</th><th>Name</th><th>Units</th><th>Rate</th><th>Total</th></tr>"
;
for ($i = 0; $i < count($codes); $i++) {
$total = $units[$i] * $rates[$i];
echo
"<tr><td>$codes[$i]</td><td>$names[$i]</td><td>$units[$i]</td><td>$rates[$i]</td><td>$to
tal</td></tr>";
}
echo "</table>";
}
?>
Slip 4
Q1: Write a PHP script to declare three variables and print maximum among them.
<?php
$a = 10;
$b = 25;
$c = 15;
$max = $a;
if($b > $max) $max = $b;
if($c > $max) $max = $c;
Q2: Accept two strings and compare using == and strcmp(). Append second string to first
and reverse from user position.
<!DOCTYPE html>
<html>
<body>
<form method="post">
String 1: <input type="text" name="str1"><br>
String 2: <input type="text" name="str2"><br>
Reverse from position: <input type="number" name="pos"><br>
<input type="submit" name="submit" value="Process">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
$s1 = $_POST['str1'];
$s2 = $_POST['str2'];
$pos = $_POST['pos'];
while ($temp != 0) {
$digit = $temp % 10;
$sum += $digit ** 3;
$temp = (int)($temp / 10);
}
if ($sum == $num)
echo "$num is an Armstrong number.";
else
echo "$num is not an Armstrong number.";
?>
Q2: Reverse key-value pair and shuffle array (associative array operations).
<?php
$data = array("one" => 1, "two" => 2, "three" => 3);
echo "<b>Original:</b><br>";
print_r($data);
$flipped = array_flip($data);
echo "<br><b>Flipped:</b><br>";
print_r($flipped);
$random = $data;
shuffle($random);
echo "<br><b>Shuffled (values only):</b><br>";
print_r($random);
?>
Slip 7
Q1: HTML form to accept string and reverse it using PHP function.
<!DOCTYPE html>
<html>
<body>
<form method="post">
Enter a string: <input type="text" name="str">
<input type="submit" value="Reverse">
</form>
</body>
</html>
<?php
function reverseString($s) {
return strrev($s);
}
if ($_POST) {
echo "Reversed: " . reverseString($_POST['str']);
}
?>
$rev = array_reverse($arr);
echo "<br>Reversed: ";
print_r($rev);
?>
Slip 8
Q1: Check whether a passed string is a palindrome or not.
<!DOCTYPE html>
<html>
<body>
<form method="post">
Enter string: <input type="text" name="str">
<input type="submit" value="Check">
</form>
</body>
</html>
<?php
if ($_POST) {
$s = $_POST['str'];
if ($s == strrev($s))
echo "$s is a Palindrome";
else
echo "$s is not a Palindrome";
}
?>
Q2: Declare multidimensional array and display specific element & delete.
<?php
$data = array(
array("ID" => 1, "Name" => "Om"),
array("ID" => 2, "Name" => "Karan")
);
echo "Original:<br>";
print_r($data);
array_pop($stack);
echo "<br>Stack after pop:<br>";
print_r($stack);
?>
Slip 10
Q1: Write a PHP script to display source code of a webpage.
<?php
echo htmlspecialchars(file_get_contents("https://www.example.com"));
?>
Q2: HTML form to accept a string and count vowels and their occurrences.
<!DOCTYPE html>
<html>
<body>
<form method="post">
Enter a string: <input type="text" name="str">
<input type="submit" value="Check Vowels">
</form>
</body>
</html>
<?php
if ($_POST) {
$str = strtolower($_POST['str']);
$vowels = ['a','e','i','o','u'];
$total = 0;
Q2: Form to accept 2 numbers, select arithmetic operation using radio buttons and display
result.
<!DOCTYPE html>
<html>
<body>
<form method="post">
Number 1: <input type="text" name="n1"><br>
Number 2: <input type="text" name="n2"><br>
<input type="radio" name="op" value="add" checked> Add
<input type="radio" name="op" value="sub"> Subtract
<input type="radio" name="op" value="mul"> Multiply
<input type="radio" name="op" value="div"> Divide<br>
<input type="submit" name="submit" value="Calculate">
</form>
</body>
</html>
<?php
function calc($a, $b, $op="add") {
switch($op){
case "add": return $a + $b;
case "sub": return $a - $b;
case "mul": return $a * $b;
case "div": return ($b != 0) ? $a / $b : "Cannot divide";
}
}
if(isset($_POST['submit'])) {
echo "Result: " . calc($_POST['n1'], $_POST['n2'], $_POST['op']);
}
?>
Slip 13
Q1: Display class timetable using echo and HTML table.
<?php
echo "<table border='1'>
<tr><th>Day</th><th>Subject</th></tr>
<tr><td>Monday</td><td>PHP</td></tr>
<tr><td>Tuesday</td><td>DBMS</td></tr>
</table>";
?>
Q2: Form to accept 5 item details using 4 text boxes. Display bill in table using explode().
<!DOCTYPE html>
<html>
<body>
<form method="post">
Item Names: <input type="text" name="name"><br>
Units: <input type="text" name="unit"><br>
Rates: <input type="text" name="rate"><br>
Codes: <input type="text" name="code"><br>
<input type="submit" name="submit" value="Bill">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
$names = explode(",", $_POST['name']);
$units = explode(",", $_POST['unit']);
$rates = explode(",", $_POST['rate']);
$codes = explode(",", $_POST['code']);
echo "<table
border='1'><tr><th>Code</th><th>Name</th><th>Unit</th><th>Rate</th><th>Total</th></tr>";
for($i=0;$i<count($codes);$i++) {
$total = $units[$i] * $rates[$i];
echo
"<tr><td>$codes[$i]</td><td>$names[$i]</td><td>$units[$i]</td><td>$rates[$i]</td><td>$to
tal</td></tr>";
}
echo "</table>";
}
?>
Slip 14
Q1: Write a PHP script to declare three variables and print maximum among them.
<?php
$a = 12; $b = 45; $c = 30;
$max = $a;
if($b > $max) $max = $b;
if($c > $max) $max = $c;
echo "Maximum is: $max";
?>
Q2: Accept two strings, compare using == and strcmp(), append and reverse from user-given
position.
<!DOCTYPE html>
<html>
<body>
<form method="post">
String 1: <input type="text" name="s1"><br>
String 2: <input type="text" name="s2"><br>
Position: <input type="number" name="pos"><br>
<input type="submit" name="submit" value="Process">
</form>
</body>
</html>
<?php
if ($_POST) {
$s1 = $_POST['s1'];
$s2 = $_POST['s2'];
$pos = $_POST['pos'];
Q2: Create an XML file with details of students admitted for different courses.
<?php
$xml = "<?xml version='1.0'?>
<Courses>
<Course><Name>Arts</Name><Level>UG</Level><Intake>60</Intake></Course>
<Course><Name>Science</Name><Level>UG</Level><Intake>80</Intake></Course>
<Course><Name>Commerce</Name><Level>UG</Level><Intake>70</Intake></Course>
<Course><Name>Management</Name><Level>PG</Level><Intake>50</Intake></Course>
</Courses>";
file_put_contents("Course.xml", $xml);
echo "Course.xml created";
?>
Slip 16
Q1: Write a PHP script to check whether accepted number is prime or not.
<?php
$n = 13; $prime = true;
if($n <= 1) $prime = false;
for($i = 2; $i <= sqrt($n); $i++) {
if($n % $i == 0) { $prime = false; break; }
}
echo $prime ? "$n is prime" : "$n is not prime";
?>
Q2: Reverse keys and shuffle array values (associative array operations).
<?php
$arr = array("a"=>1,"b"=>2,"c"=>3);
echo "Original:<br>"; print_r($arr);
$flip = array_flip($arr);
echo "<br>Flipped:<br>"; print_r($flip);
$val = array_values($arr);
shuffle($val);
echo "<br>Shuffled values:<br>"; print_r($val);
?>
Slip 17
Q1: Accept a string and reverse it using a PHP function.
<!DOCTYPE html>
<html><body>
<form method="post">
<input type="text" name="str">
<input type="submit" value="Reverse">
</form></body></html>
<?php
if ($_POST) {
echo strrev($_POST['str']);
}
?>
<?php
if ($_POST) {
$s = $_POST['str'];
echo $s == strrev($s) ? "$s is Palindrome" : "$s is not Palindrome";
}
?>
Q2: Display teacher info from PostgreSQL using PHP (sample code only).
<?php
$conn = pg_connect("host=localhost dbname=college user=postgres password=yourpass");
$result = pg_query($conn, "SELECT * FROM teacher");
while ($row = pg_fetch_assoc($result)) {
echo $row['tno']." ".$row['name']." ".$row['subject']." ".$row['research']."<br>";
}
pg_close($conn);
?>
Slip 19
Q1: Print Floyd's triangle with letters.
<?php
$char = 'A';
for ($i = 1; $i <= 4; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo $char++ . " ";
}
echo "<br>";
}
?>