0% found this document useful (0 votes)
66 views13 pages

Web Tech Slip

The documents contain sample questions and answers related to PHP programming. Various PHP concepts are covered like arrays, strings, functions, classes, forms, MySQL etc. A total of 6 documents are provided with 2 questions each covering basic to intermediate PHP topics.

Uploaded by

oskie
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)
66 views13 pages

Web Tech Slip

The documents contain sample questions and answers related to PHP programming. Various PHP concepts are covered like arrays, strings, functions, classes, forms, MySQL etc. A total of 6 documents are provided with 2 questions each covering basic to intermediate PHP topics.

Uploaded by

oskie
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/ 13

slip no:1

q1.write a php script to get the php version and configuration information.
ans:<?php
echo phpinfo();
?>

q2.design a html form to accept a string.write php script for the following.
a)write a function to count the total number of vowels from the script.
b)show the occurrences of each vowel from the script.
ans:<html>
<head>
<title>count vowels</title>
</head>
<body>
<form method="post" action="count_vowels.php">
<label for="inputstring">enter a string:</label><br>
<input type="text" id="inputstring" name="inputstring"><br><br>
<input type="submit" value="submit">
</form>
</html>

<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
$intputstring=$_POST['inputstring'];
function countvowels($str){
$vowels=['a','e','i','o','u'];
$vowelcount=0;
$voweloccurrences=array_fill_keys($vowels,0);
for($i=0; $i<strlen($str);$i++){
if(in_array(strtolower($str[$i]),$vowels)){
$vowelcount++;
$voweloccurrences[strtolower($str[$i])]++;
}
}
echo"total nnumber of vowels:".$vowelcount."<br>";
echo"occurrences of each vowels:<br>";
foreach($voweloccurrences as $vowel=>$count){
echo strtoupper ($vowel).":".$count."<br>";
}
}
countvowels($inputstring);
}
?>

slip no:2
q1.write a php script to display student information on web page.
ans:<?php
echo "name:deepa pawar";
echo "class:s.y.b.c.a(sci)";
echo :college name:"mit acsc college,alandi";
?>
q2.write a php program to define interface shape which has two method as area() and volume().define
a constant pi.create a class cylinder implement this interface and calculate area and volume.
ans:<?php
interface shape{
public function area();
public function volume();
}
class cylinder implements shape {
const pi=3.14;
private $radius;
private $height;
public function
--construct($radius,$height){
$this->radius=$radius;
$this->height=$height;
}
public function area(){
return 2*pi*this->radius*(this->radius+this->height);
}
public function volume(){
return pi*this->radius*this->radius*this->height;
}
$cylinder=new cylinder(5,10);
echo "area of the cylinder:".
$cylinder->area()."square units\n";
echo "volume of the cylinder:".
$cylinder->volume()."cubic units\n";
?>

slip no:3
q.1 write a php script to display time table of your class(use html table tags in echo).
ans:<?php
$class_name = "SYBCA";
$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");
$time_slots = array("8:00 AM - 9:00 AM", "9:00 AM - 10:00 AM", "10:00 AM - 11:00 AM", "11:00 AM - 12:00 PM", "1:00 P

echo "<h2>{$class_name} Timetable</h2>";


echo "<table border='1'>";
echo "<tr><th>Time</th>";

foreach ($days as $day) {


echo "<th>{$day}</th>";
}
echo "</tr>";

foreach ($time_slots as $time_slot) {


echo "<tr><td>{$time_slot}</td>";

foreach ($days as $day) {


echo "<td>Subject</td>";
}
echo "</tr>";
}
echo "</table>";
?>
q2.write a php script for the following:design a form to accept the details of 5 different items,
such as item code,item name,units sold,rate.display the bill in the tabular format.use only 4text
boxes.(hint:use of explode function.)
ans:<html>
<head>
<title> bill display</title>
</head>
<body>
<h2>enter details of 5 items</h2>
<form method="post">
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
$items=explode(",",$_POST['items'])
echo "<table border='1'>";
echo "<tr><th>item code</th><th>item name</th><th>units sold</th><th>rate</th></tr>";
foreach($items as $item){
$details=explode(":",$item);
echo "<tr>";
foreach($details as $detail){
echo "<td>$detail</td>";
}
echo "</tr>";
}
echo"</table>";
}
?>
<label>enter item details(item code:item name:units sold:rate)</label><br>
<textarea name="items" rows="5" cols="50"></textarea><br><br>
<input type="submit" value="generate bill">
</form>
</body>
</html>

slip no:4
q1.write a php script to declare 3 variables and print maximum among them.
ans:<?php
$a=10;
$b=20;
$c=30;
$max=max($a,$b,$c);
echo "the maximum value among $a,$b,and $c is:$max";
?>

q2.write a php script for the following: design a form to accept two strings.compare the two strings using both metho
string.accept the position from the user;form the 1st string are reversed.(use radio buttons)
ans:<html>
<body>
<h2>string comparison and manipulation</h2>
<form method="post">
<label>enter string 1:</label><br>
<input type="text" name="string1"><br><br>
<label>enter string 2:</label><br>
<input type="text" name="string2"><br><br>
<input type="radio" name="position" value="start" checked>append at the start<br>
<input type="radio" name="position" value="end" checked>append at the end<br>
<input type="submit" name="submit" value="compare and append">
</form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
$string1=$_post['string1'];
$string2=$_post['string2'];
$position=$_post['position'];
if($string1==$string2){
echo " strings are equal using == operator.<br>";
}else{
echo " strings are is not equal using == operator.<br>";
}
$comparison =strcmp($string1,$string2);
if($comparison==0){
echo " strings are equal using strcmp function.<br>";
}else{
echo " strings are is not equal using strcmp function.<br>";
}
if($comparison=="start"){
$result=$string2.$string1;
}else{
$result=$string1.$string2;
}
echo "result after appending:$result";
}
?>
</body>
</html>

slip no:5
q1.write a php script to check number 153 is armstrong or not.
ans:<?php
$no=153;
$num=$no;
$result=0;
while($num!=0)
{
$rem=$num%10;
$result=$result+($rem*$rem*$rem);
%num=$num/10;
}
if($result==$no)
echo " num is armstrong";
else
echo " num is not armstrong";
?>

q2.write a menu driven program to perform the following operations on an associative array:
a)display the elements of an array along with the keys.
b)display the size of an array.
ans:<html>
<body>
<h2>menu-driven program for associative array</h2>
<form method="post">
<p>select an operation:</p>
<input type="radio" name="operation" value="display">display elements of the array along with the keys<br>
<input type="radio" name="operation" value="size">display the size of the array<br><br>
<input type="submit" name="submit" value="perform operation">
</form>
<?php
$arr=array('key1'=>'value1','key2'=>'value2','key3'=>'value3');
if($_SERVER["REQUEST_METHOD"]=="POST")
$operation=$_post['operation'];
switch($operation){
case 'display':
echo "<h3>elements of the array with keys:</h3>";
foreach($arr as key=>$value)
echo "key: $key,value:$value<br>";
}
break;
case 'size':
$size=count($arr);
echo "<h3>size of the array:</h3>";
echo "the size of the array is:$size";
break;
default:echo "invalid operation selected";
}
}
?>
</body>
</html>

slip no:6
q1.write a php script to check whether accepted number is prime or not.
ans:<?php
$f = 0;
$n = $_POST['num'];
for ($i = 2; $i < $n; $i++) {
if ($n % $i == 0) {
$f = 1;
break;
}
}
if ($f == 1) {
echo "Number is not prime ", $n;
} else {
echo "Number is prime : ", $n;
}
?>

q2.write a ajax program to search student name according to the character typed and display list using array.
ans:<html>
<head>
<script>
function searchnames(){
var input=document.getelementbyid("search").value;
var xmlhttp=new xmlhttprequest();
xmlhttp.onreadystatechange=function(){
if(this.readystate==4 && this.status==200){
document.getelementbyid("result").innerhtml=this.responsetext;
}
};
xmlhttp.open("GET","search.php?input="+input,true);
xmlhttp.send();
}
</script>
<head>
<body>
<h2>search student names</h2>
<label>enter the character:</label>
<input type="text" id="search" onkeyup="searchnames()">
<div id="result"></div>
</body>
</html>

slip no:7
q1.design a html form to accept a string.write php function to reverse a string.
ans:<html>
<body>
<form action="slip17_1-1.php" method="post">
Enter the string :
<input type="text" name="ch">
<input type="submit" class="sub">
</form>
</body>
</html>
<?php
$ch = $_POST['ch'];
$ch1 = strrev($ch);
echo "Original string : ", $ch, "Revesed String : ", $ch1;
?>

q2.declare array.reverse the order of elements,making the 1st element last and last element 1st and similarly rearra
ans: <?php
$originalArray = array(1, 2, 3, 4, 5);
$reversedArray = array_reverse($originalArray);
echo "Original Array: " . implode(", ", $originalArray) . "<br>";
echo "Reversed Array: " . implode(", ", $reversedArray) . "\n";
?>

slip no:8
q1.design a htnl form to accept a string.write a php function that checks whether a passed string is a palindrome or n
ans:<html>
<body>
<form action="slip17_1-1.php" method="post">
Enter the string :
<input type="text" name="ch">
<input type="submit" class="sub">
</form>
</body>
</html>
<?php
$ch = $_POST['ch'];
$ch1 = strrev($ch);
if ($ch == $ch1) {
echo "GIven string is palidrom : ", $ch;
} else {
echo "Given string is not palidrom : ", $ch;
}

q2.declare a multidimensional array.display specific element from a multidimensional array.also delete given elemen
ray content).
ans: <?php
$multiArray = array(
array("Aarav", "Patel", 25),
array("Neha", "Sharma", 30),
array("Rahul", "Verma", 22),
);
echo "<h3>Original Array:</h3>";
printArray($multiArray);

$specificElement = $multiArray[1][2];
echo "<h3>Specific Element:</h3>";
echo "Value at \$multiArray[1][2]: $specificElement";

$rowToDelete = 0;
$columnToDelete = 1;
unset($multiArray[$rowToDelete][$columnToDelete]);

echo "<h3>Array after deletion:</h3>";


printArray($multiArray);

function printArray($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
?>

slip no:9
q1.write a php script to print following floyd's triangle.
1
23
456
7 8 9 10
ans:<?php
$a = 1;
for ($i = 1; $i < 6; $i++) {
for ($j = 1; $j < $i; $j++) {
echo $a . "\t";
$a++;
}
echo "<br>";
}
?>

q2.write a menu driven program to perform the following stack related operations.
a)insert an element in stack.
b)delete an element from stack.[hint:array_push(),array_pop()]
ans:refer slip no:19 q2

slip no :10
q1.write a php code to display source code of a webpage.
ans:<?php
$all_lines = file('https://github.io/harshaldaundkar1513/mitacs');
foreach ($all_lines as $line_num => $line) {
echo "line no : {$line_num} : " . htmlspecialchars($line) . "\n";
}
?>
or <?php
show_source("student.php");
?>

Q2. Write a menu driven program to perform the following queue related operations
d) Insert an element in queue
e) Delete an element from queue
f) Display the contents of queue

ans:<?php
$ch = $_POST['ch'];
$queue = array();
switch ($ch) {
case 1:
$element = $_POST['num'];
array_push($queue, $element);
echo "Element inserted into the queue: $element\n";
break;

case 2:
$deletedElement = array_shift($queue);
echo "Element deleted from the queue: $deletedElement\n";
break;

case 3:
echo "Contents of the queue: " . implode(', ', $queue) . "\n";
break;
case 4:
echo "Exiting program.\n";
exit;

default:
echo "Invalid choice. Please enter a number between 1 and 4.\n";
}

?>

slip no:11
q1.Write a PHP script to get the PHP version and configuration information.
ans: refer slip no 1..q.1

q2.Design a HTML form to accept a string. Write a PHP script for the following.
a) Write a function to count the total number of Vowels from the script.
b) Show the occurrences of each Vowel from the script. -->
ans:<html>
<body>
<form method="post" action="count_vowels.php">
<label for="inputstring">enter the string:</label><br>
<input type="text" id="inputstring" name="inputstring"><br><br>
<input type="submit" value="submit">
</form>
</body>
</html>

<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
$inputstring=$_post['inputstring'];
function countvowels($str){
$vowels=['a','e','i','o','u'];
$vowelcount=0;
$voweloccurrences=array_fill_keys($vowels,0);
for($i=0;$i<strlen($str);$i++){
if(in_array(strtolower($str[$i]),$vowels)){
$vowelscount++;
$voweloccurrences[strtolower($str[$i])++;
}
}
echo "total number of vowels".$vowelcount.<br>";
echo "occurrences of each vowel:<br>";
foreach($voweloccurrences as $vowel=>$count){
echo $vowel.":".$count.<br>";
}
}
countvowels($inputstring);
}
?>

slip no:12
q1.Write a PHP script to display student information on web page.
ans.refer slip no.2 q1

q2. Write a PHP script for the following:


a) Design a form to accept two numbers from the users.
b) Give option to choose an arithmetic operation (use Radio Button).
c) Display the result on next form.
d) Use concept of default parameter.
ans:<html>
<body>
<form action="slip12_2.php" method="post">
Enter 1st numbers :
<input type="text" name="num1"> <br>
Enter 2nd numbers :
<input type="text" name="num2"> <br>
<input type="radio" name="op" value="1">Addition
<input type="radio" name="op" value="2">Subtraction
<input type="radio" name="op" value="3">Multiplication
<input type="radio" name="op" value="4">Division
<input type="submit" value="submit">
</form>
</body>
</html>
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$ch = $_POST['op'];

switch ($ch) {
case 1:
echo "The additon is : " . $num1 + $num2;
break;
case 2:
echo "The subtraction is : " . $num1 - $num2;
break;
case 3:
echo "The multiplication is : " . $num1 * $num2;
break;
case 4:
echo "The division is : " . $num1 / $num2;
break;
default:
echo "Invalid input";
}
?>

slip no:13
q.1 Write a PHP script to script to display time table of your class( use HTML table tags in echo).
ans: refer slip no.3 q1

q2.write a php script for the following:design a form to accept the details of 5 different items,
such as item code,item name,units sold,rate.display the bill in the tabular format.use only 4text
boxes.(hint:use of explode function.)
ans:refer slip no.3 q2.

slip no:14
q.1write a php script to declare 3 variables and print maximum among them.
ans refer slip no.4 q1

q2.write a php script for the following: design a form to accept two strings.compare the two strings using both metho
string.accept the position from the user;form the 1st string are reversed.(use radio buttons)
ans: refer slip no.4 q2

slip no:15
q1.write a php script to check number 153 is armstrong or not.
ans: refer slip no.5 q1

q2.create a xml file which gives details of students admitted for different courses in your college.course name can be
elements in each course are in following format.
<course>
<level>...</level>
<intake capacity>....</intake capacity>
</course>
save the file with"course.xml"
ans:<?xml version="1.0" encoding="UTF-8"?>
<College>
<Course>
<Name>Arts</Name>
<Details>
<Level>Undergraduate</Level>
<IntakeCapacity>100</IntakeCapacity>
</Details>
</Course>

<Course>
<Name>Science</Name>
<Details>
<Level>Undergraduate</Level>
<IntakeCapacity>120</IntakeCapacity>
</Details>
</Course>

<Course>
<Name>Commerce</Name>
<Details>
<Level>Undergraduate</Level>
<IntakeCapacity>80</IntakeCapacity>
</Details>
</Course>

<Course>
<Name>Management</Name>
<Details>
<Level>Postgraduate</Level>
<IntakeCapacity>60</IntakeCapacity>
</Details>
</Course>
</College>

slip no:16
q1.write a php script to check whether accepted number is prime or not.
ans:refer slip no:6 q1

q2.Write a menu driven program the following operation on an associative array


c) Reverse the order of each element’s key-value pair. [Hint: array_flip()]
d) Traverse the element in an array in random order. [Hint: shuffle()] -->
ans: <html>
<body>
<form action="slip16_2-1.php" method="post">
1.Display <br>
2. Reverse the order of each element's key-value pair <br>
3. Traverse the element in an array in random order<br>
<input type="text" name="ch" id="">
<input type="submit" name="" id="">
</form>
</body>
</html>
<?php
$ch = $_POST['ch'];
$arr = array(
array("name" => "Harshal", "Age" => 20),
array("name" => "Rushi", "Age" => 22)
);
switch ($ch) {
case 1:
print_r($arr);
break;
case 2:
$arr1 = array_reverse($arr);
echo "Reversed array : <br>";
print_r($arr1);
break;
case 3:
shuffle($arr);
echo "Array in random order : <br>";
print_r($arr);
break;
default:
echo "Invalid choice ";
}
?>

slip no:17
q1.design a html form to accept a string.write php function to reverse a string.
ans:refer slip no.7 q1

q2. Declare array. Reverse the order of elements, making the first element last and last
element first and similarly rearranging other array elements.[Hint : array_reverse()]
ans:refer slip no.7 q2

slip no:18
q.1design a htnl form to accept a string.write a php function that checks whether a passed string is a palindrome or n
ans:refer slip no:8 q1

q2. Property (pno, description, area)


a. Owner (oname, address, phone)
b. An owner can have one or more properties, but a property belongs to exactly one owner.
c. Accept owner name from the user. Write a PHP script which will display all
properties which are own by that owner.
ans:<html>
<body>
<form action="slip18_2-1.php" method="post">
Enter the user name :
<input type="text" name="nam">
<input type="submit">
</form>
</body>
</html>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$dbconn = pg_connect("host = localhost dbname=slips user = harshal password = 1604 ");
$nam = $_POST['nam'];
$query = "select pno , description , area , addres , phone from Property p , Owner o where p.oname = o.oname and o
$result = pg_query($dbconn, $query);

echo "<table border = '1' >";


echo "<tr><th> Pno </th>";
echo "<th>description </th>";
echo "<th>area </th>";
echo "<th>addres </th>";
echo "<th>phone </th></tr>";

while ($row = pg_fetch_assoc($result)) {


echo "\t<tr>\n";
foreach ($row as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
pg_free_result($result);
pg_close($dbconn);
?>

slip no:19
q1. Write a PHP script to print following floyd’s triangle.
A
BC
DEF
GHIJ
ans: <?php
$char = 'A';
for ($i = 0; $i < 5; $i++) {
for ($j = 0; $j < $i; $j++) {
echo $char, "\t";
$char++;
}
echo "<br>";
}
?>
q2.Write a menu driven program to perform the following stack related operations.
c) Insert an element in stack.
d) Delete an element from stack.[Hint: array_push(), array_pop()] -->
ans: <html>
<body>
<form action="slip19_2.php" method="post">
1.Insert an element in stack <br>
<input type="number" name="num"> <br>
2.Delete an element from stack <br>
<input type="text" name="ch" id=""> <br>
<input type="submit" name="" id="">
</form>
</body>
</html>
<?php
$ch = $_POST['ch'];
$stack = array();
switch ($ch) {
case 1:
$element = $_POST['num'];
array_push($stack, $element);
echo "Element inserted into the queue: $element\n";
break;

case 2:
$deletedElement = array_pop($stack);
echo "Element deleted from the stack: $deletedElement\n";
break;
default:
echo "invalid input";
}
?>

slip no:20
q1.write a php code to display source code of a webpage.
ans:refer slip no:10 q1

q2. Q2. Write a menu driven program to perform the following queue related operations
d) Insert an element in queue
e) Delete an element from queue
f) Display the contents of queue
ans: refer slip no:10 q2

You might also like