0% found this document useful (0 votes)
19 views20 pages

WT Slip Solution

The document contains a series of PHP scripts and HTML forms that demonstrate various programming tasks, including displaying PHP configuration, counting vowels in a string, performing arithmetic operations, and manipulating arrays. Each slip presents a different coding challenge, showcasing the use of functions, form handling, and basic PHP syntax. The examples are designed to help users understand PHP programming concepts through practical applications.

Uploaded by

kiranpc041
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views20 pages

WT Slip Solution

The document contains a series of PHP scripts and HTML forms that demonstrate various programming tasks, including displaying PHP configuration, counting vowels in a string, performing arithmetic operations, and manipulating arrays. Each slip presents a different coding challenge, showcasing the use of functions, form handling, and basic PHP syntax. The examples are designed to help users understand PHP programming concepts through practical applications.

Uploaded by

kiranpc041
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Slip 1

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;

echo "<h3>Vowel Count Results:</h3>";


foreach ($vowels as $v) {
$count = substr_count($str, $v);
echo "Vowel '$v' occurs $count times.<br>";
$total += $count;
}
echo "Total number of vowels: $total";
}
?>
Slip 2
Q1: Write a PHP script to display student information on web page.
<?php
$student = array("Name" => "Om Kadam", "Age" => 20, "Course" => "BCA");
echo "<h3>Student Information</h3>";
foreach ($student as $key => $value) {
echo "$key: $value<br>";
}
?>

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;

echo "Maximum number is: $max";


?>

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'];

echo "Using == : " . ($s1 == $s2 ? "Equal" : "Not equal") . "<br>";


echo "Using strcmp() : " . (strcmp($s1, $s2) == 0 ? "Equal" : "Not equal") . "<br>";

$combined = $s1 . $s2;


$prefix = substr($combined, 0, $pos);
$suffix = substr($combined, $pos);
$reversed = strrev($suffix);

echo "Final string: " . $prefix . $reversed;


}
?>
Slip 5
Q1: Write a PHP script to check number 153 is Armstrong or not.
<?php
$num = 153;
$sum = 0;
$temp = $num;

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: Menu-driven program to show key-value and size of associative array.


<?php
$student = array("Name" => "Om", "Age" => 20, "Course" => "BCA");

echo "<h3>Student Details:</h3>";


foreach ($student as $key => $value) {
echo "$key => $value<br>";
}

echo "Size of array: " . count($student);


?>
Slip 6
Q1: Write a PHP script to check whether accepted number is prime or not.
<?php
$num = 17;
$isPrime = true;

if ($num <= 1) $isPrime = false;


for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
$isPrime = false;
break;
}
}

echo $isPrime ? "$num is a prime number." : "$num is not a prime 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']);
}
?>

Q2: Reverse order of array elements using array_reverse().


<?php
$arr = array(10, 20, 30, 40, 50);
echo "Original: ";
print_r($arr);

$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);

unset($data[1]); // deleting second element

echo "<br>After deletion:<br>";


print_r($data);
?>
Slip 9
Q1: Print Floyd's triangle.
<?php
$num = 1;
for ($i = 1; $i <= 4; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo $num++ . " ";
}
echo "<br>";
}
?>

Q2: Perform stack operations using array_push() and array_pop().


<?php
$stack = array();
array_push($stack, "PHP");
array_push($stack, "HTML");

echo "Stack after push:<br>";


print_r($stack);

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: Queue operations: insert, delete, display using array.


<?php
$queue = array();
array_push($queue, "Om");
array_push($queue, "Karan");

echo "Queue: ";


print_r($queue);

array_shift($queue); // remove first element


echo "<br>After Dequeue: ";
print_r($queue);
?>
Slip 11
Q1: Write a PHP script to get the PHP version and configuration information.
<?php
phpinfo();
?>

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;

foreach ($vowels as $v) {


$count = substr_count($str, $v);
echo "$v: $count<br>";
$total += $count;
}
echo "Total vowels: $total";
}
?>
Slip 12
Q1: Write a PHP script to display student information on web page.
<?php
$student = array("Name" => "Om", "Roll" => 101, "Course" => "BCA");
foreach ($student as $key => $value) {
echo "$key: $value<br>";
}
?>

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'];

echo "== : " . ($s1 == $s2 ? "Equal" : "Not equal") . "<br>";


echo "strcmp() : " . (strcmp($s1, $s2) == 0 ? "Equal" : "Not equal") . "<br>";

$combined = $s1 . $s2;


$part1 = substr($combined, 0, $pos);
$part2 = substr($combined, $pos);
echo "Final: " . $part1 . strrev($part2);
}
?>
Slip 15
Q1: Write a PHP script to check number 153 is Armstrong or not.
<?php
$num = 153; $sum = 0; $temp = $num;
while ($temp != 0) {
$r = $temp % 10;
$sum += $r ** 3;
$temp = (int)($temp / 10);
}
echo $sum == $num ? "$num is Armstrong" : "$num is not Armstrong";
?>

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']);
}
?>

Q2: Reverse order of array using array_reverse().


<?php
$arr = array(1,2,3,4,5);
echo "Original: "; print_r($arr);
echo "<br>Reversed: "; print_r(array_reverse($arr));
?>
Slip 18
Q1: Check if entered string is a palindrome.
<!DOCTYPE html>
<html><body>
<form method="post">
<input type="text" name="str">
<input type="submit" value="Check">
</form></body></html>

<?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>";
}
?>

Q2: Perform stack operations using array_push and array_pop.


<?php
$stack = array();
array_push($stack, "PHP");
array_push($stack, "HTML");
print_r($stack);
array_pop($stack);
echo "<br>After pop:<br>";
print_r($stack);
?>
Slip 20
Q1: Display source code of a webpage.
<?php
echo htmlspecialchars(file_get_contents("https://www.example.com"));
?>

Q2: Queue operations using array.


<?php
$queue = array();
array_push($queue, "Om", "Karan");
echo "Queue: "; print_r($queue);
array_shift($queue);
echo "<br>After Dequeue: "; print_r($queue);
?>

You might also like