rohidas

Hi I am Rohidas lande.I have teaching in C.D.jain college of commerce Shrirampur .I have Nine year in teaching Experience to teach various programming langauge,script languages,databases like Java,C,C++,php,python,html,css,javascript,mysql,oracle.

CA-304 PHP Practical Solved Slips

University of Pune CA-304 PHP first Ten Solved Practical Slips

PHP Solved Practical Slips

Write a PHP script for the following: Design a form to accept a string. Write a function to count the total number of vowels (a,e,i,o,u) from the string. Show the occurrences of each vowel from the string. Check whether the given string is a palindrome or not, without using built-in function. (Use radio buttons and the concept of function. Use ‘include’ construct or require stmt.)

FileName:program1.html
<html>
<body>
<form action="includedata.php" method="post">
Enter the String:<input type="text" name="str">
Vowel Count:<input type="radio" name="vowel1" value="1">
String Palindrome:<input type="radio" name="vowel1" value="2">
<input type="submit" value="submit">
<form>
</body>
</html>
FileName:program1.php

<?php
function vowel($str1)
{
$ca=0,$ce=0,$ci=0,$co=0,$cu=0;
$len=strlen($str1);
for($i=0;$i<$len;$i++)
{
switch($str1[$i])
{
case 'a':
case'A':echo"\n
$str1[$i] found at postion $i";
$ca++;
break;

case 'e':
case'E':echo"\n
$str1[$i] found at postion $i";
$ce++;
break;

case 'o':
case'O':echo"\n
$str1[$i] found at postion $i";
$co++;
break;

case 'i':
case'I':echo"\n
$str1[$i] found at postion $i";
$ci++;
break;

case 'u':
case'U':echo"\n
$str1[$i] found at postion $i";
$cu++;
break;

}
}
echo"Occurance of 'a':".$ca;
echo"Occurance of 'e':".$ce;"
echo"Occurance of 'i':".$ci;
echo"Occurance of 'o':".$co;
echo"Occurance of 'u':".$cu;
}

function palindrome($str1)
{
$len=strlen($str1);
$rev=strrev($str1);
$f=0;
for($j=0;$j<$len;$j++)
{
if($rev[$j]==$str1[$j])
{
$f=0;
}
else
{ $f=1;
break;
}
}
if($f==0)
{
echo"string is palindrome";
}
else
{
echo"string is not palindrome";
}
}
?>

FileName:-includedata.php

<?php
include("program1.php");
$ch=$_POST["vowel1"];
$str1=$_POST['str'];
switch($ch)
{

case "1":vowel($str1);
break;

case "2":palindrome($str1);
break;

default:echo"invalid choice";
}
?>

Write a PHP script for the following: Design a form to accept two strings from the user. Find the first occurrence and the last occurrence of the small string in the large string. Also count the total number of occurrences of small string in the large string. Provide a text box to accept a string, which will replace the small string in the large string. (Use built-in functions)

program2.html
<html>
<body>
<form action="program2.php" method="get">
<center>
<table>
<tr><td>Enter First String : </td><td><input type="text" name="str1"></td></tr>
<tr><td>Enter second String : </td><td><input type="text" name="str2"></td></tr>
<tr><td>Enter String To Replace : </td><td><input type="text" name="str3"></td></tr>
<tr><td>Occurrences</td><td><input type="radio" name="ch" value=1></td></tr>
<tr><td>Replace</td><td><input type="radio" name="ch" value=2></td></tr>
<tr><td>Split</td><td><input type="radio" name="ch" value=2></td></tr>
<tr><td> </td><td><input type="submit" value=Display></td></tr>
</table>
</center>
</form>>
</body>
</html>

Prgram2.php
<?php
$str1 = $_POST['str1'];
$str2 = $_POST['str2'];
$str3 = $_POST['str3'];
$ch = $_POST['ch'];
echo "First String is = "str1."";

echo "second String is = ".$str2."";

echo "String for Replace is = ".$str3."";

if(strlen($str1)>strlen($str2))
{
switch($ch)
{
case 1 : $pos = strpos($str1,$str2);
if($pos != 0)
echo "string '$str2' Not present at the start of '$str1'.";
else echo "string '$str2' present at the start of '$str1'.";
break;


case 2 : $str4 = str_replace($str2,$str3,$str1);
echo "After replacing string $str4";
break;

case 3:$s=preg_split("//",$str1);
foreach($s as $v)
echo "\t$v";
break;
}
}
else
{
switch($ch)
{
case 1 : $pos = strpos($str2,$str1);
if($pos != 0)
echo "string '$str1' Not present at the start of '$str2'.";
else echo "string '$str1' present at the start of '$str2'.";
break;

case 2 : $str4 = str_replace($str1,$str3,$str2);
echo "After replacing string $str4";
break;

case 3:echo"after Spliting the string::";
$s=preg_split("//",$str2);
foreach($s as $v)
echo"\t$v";
}
}
?>

Write a PHP script for the following: Design a form to accept two numbers from the user. Give options to choose the arithmetic operation (use radio buttons). Display the result on the next form. (Use the concept of function and default parameters. Use ‘include’ construct or require stmt).

FileName:-program3.html <html> <body> <form method="post" action="program3_1.php"> Enter first number :<input type="text" name="no1" value=" "><br> Enter Second number :<input type="text" name="no2" value=" "><br> <input type='radio' name='op' value='1'>Addition<br> <input type='radio' name='op' value='2'>Multiplication<br> <input type='radio' name='op' value='3'>Subtraction<br> <input type='radio' name='op' value='4'>Division<br> <input type='submit' name='submit' value='submit'> </form> </body> </html> FileName:-calculator1.php <?php function add($a,$b) { $s=$a+$b; return $s; } function sub($a,$b) { $s=$a-$b; return $s; } function mul($a,$b) { $s=$a*$b; return $s; } function div($a,$b) { if($b==0) echo "Division not possible.<br>"; else $s=$a/$b; return $s; } ?> FileName:-program3_1.php <?php include 'calculator1.php'; $a=$_POST['no1']; $b=$_POST['no2']; $c=$_POST['op']; switch($c) { case 1: $d=add($a,$b); echo "Addition :$d";break; case 2: $d=mul($a,$b); echo "Multiplication :$d";break; case 3: $d=sub($a,$b); echo "Subtraction:$d";break; case 4: $d=mul($a,$b); echo "Division:$d";break; } ?>

Write a PHP script for the following: Design a form to accept two strings from the user. Find whether the small string appears at the start of the large string. Provide a text box to accept the string that will replace all occurrences of small string present in the large string. Also split the large string into separate words. (Use regular expressions)

FileName:-program4.html <html> <body> <form action="program4.php" method="post"> <pre> Enter first string :<input type='text' name='str1'><br> Enter second string :<input type='text' name='str2'><br> Enter string for replace:<input type='text' name='str3'><br></h2> <input type='radio' name="ch" value=1>Occurence. <input type='radio' name="ch" value=2>Replace. <input type='radio' name="ch" value=3>split <br> <input type=submit value=ok> <input type=reset value=cancel> </pre> </form> </body> </html>
FileName:-program4.php <?php $str1=$_POST['str1']; $str2=$_POST['str2']; $str3=$_POST['str3']; $ch=$_POST['ch']; echo"First string=$str1.<br>"; echo"Second String=$str2.<br>"; echo"String for replace=$str3.<br>"; if(strlen($str1)>strlen($str2)) { switch($ch) { case 1: $pos=strpos($str1,$str2); if($pos!=0) echo"String '$str2' Not present at the start of '$str1'.<br>"; else echo"String '$str2' Present at the strat of '$str1'.<br>"; break; case 2: $str4=str_replace($str2,$str3,$str1); printf("\nAfter replacing string::"); echo $str4; break; case 3: $s=preg_split("//",$str1); foreach($s as $v) echo "\t$v <br>"; break; } } else { switch($ch) { case 1:$pos=strpos($str2,$str1); if($pos!=0) echo"String '$str1' Not present at the start of '$str2'.<br>"; else echo"String '$str1' Present at the start of '$str2'.<br>"; break; case 2: $str4=str_replace($str1,$str3,$str2); echo "After replacing string::$str4<br>"; break; case 3: echo"After splitting the string::<br>"; $s=preg_split("//",$str2); foreach($s as $v) echo "\t$v <br>"; } } ?>

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 4 text boxes. (Hint : Use of explode function.)

program5.html
<html>
<head>
<style>
table
{
border-collapse: collapse;
background-color: aqua;
}
table, th, td
{
border: 1px solid blue;
}
</style>
</head>
<body>
<form method="post" action="program5.php">
<table border="1">
<center><h1>Enter details of 5 Items </h1>
<h3>Item code <input type="text" name="itemcode" placeholder="ex 1,2,3,4,5 "/></h3>
<h3>Item name <input type="text" name="itemname" /></h3>
<h3>Units sold <input type="text" name="unitssold" /></h3>
<h3>Rate <input type="text" name="rate" /></h3>
<input type="submit" value="Submit"/>
</center>
</table>
</form>
</body>
</html>


program5.php

<?php $itemcode = $_POST['itemcode'];
$itemname = $_POST['itemname'];
$unitssold = $_POST['unitssold'];
$rate = $_POST['rate'];
$i_code = explode(',',$itemcode);
$i_name = explode(',',$itemname);
$i_unit = explode(',',$unitssold);
$i_rate = explode(',',$rate);
$z=count($i_code);
$tamt=0;
for($i=0;$i<$z;$i++)
{
$tamt+=$i_unit[$i]*$i_rate[$i];
}
echo "<table align=center border=1>";
echo "<tr> <td><b>Item Code </b></td> <td><b>Item Name</b></td><td><b> Units Sold</b></td><td><b>Rate </b></td></tr>";
for($i=0;$i<$z;$i++)
{
echo "<tr> <td>".$i_code[$i]."</td><td>".$i_name[$i]."</td><td>".$i_unit[$i]."</td><td>".$i_rate[$i]." </td></tr>";
}
echo "<tr><th colspan=3>Total amount </th><td>".$tamt."</td></tr>";
echo "</table>";
?>

Write a PHP script for the following: Design a form to accept two strings. Compare the two strings using both methods (= = operator &strcmp function). Append second string to the first string. Accept the position from the user; from where the characters from the first string are reversed. (Use radio buttons)

FileName:-program6.html <html> <head> <title> html</title> </head> <body bgcolor="violet" > <form action="program6.php" method="post"> <pre> Enter first string ::<input type="text" name="str1"> Enter second string::<input type="text" name="str2"> Enter position::<input type='text' name="pos"> <input type="radio" name="ch" value=1>compare <input type="radio" name="ch" value=2>append. <input type="radio" name="ch" value=3>position for reverse. <input type="submit" value="check"> <input type="reset" value="cancel"> </pre> </form> </body> </html> FileName:-program6.php <?php $str1=$_POST['str1']; $str2=$_POST['str2']; $pos=$_POST['pos']; $ch=$_POST['ch']; echo"First string :: $str1.<br><br>"; echo "Second string::$str2.<br><br>"; echo"position for reverse::$pos.<br><br>"; echo"choice is::$ch.<br><br>"; switch($ch) { case 1: if($str1==$str2) echo "Both string are equal.<br>"; else echo"Both string are not equal.<br>"; break; case 2: echo"After appending::"; echo "$str1"."$str2"; echo"<br>"; break; case 3: $len=strlen($str1)-$pos; $s=substr($str1,$pos,$len); $str4=strrev($s); echo "After reverse::$str4.<br>"; break; } ?>

Write a menu driven PHP program to perform the following operations on an associative array: i. Display the elements of an array along with the keys. ii. Display the size of an array iii. Delete an element from an array from the given index. iv. Reverse the order of each element’s key-value pair.[Hint: use array_flip()] v. Traverse the elements in an array in random order [[Hint: use shuffle()].

FileName:-program7.html <html> <form action='program7.php' method='post'> <pre> <h3>OPERATIONS On Array</h3> 1<input type='radio' name='a' value='1'>Display.<br> 2<input type='radio' name='a' value='2'>size.<br> 3<input type='radio' name='a' value='3'>delete.<br> 4<input type='radio' name='a' value='4'>reverse.<br> 5<input type='radio' name='a' value='5'>traverse.<br> <input type='submit' value='ok'> <input type='reset' value='cancel'><br> </pre> </form> </body> </html> FileName:program7.php <?php$array=array('zero'=>0,'one'=>1,'two'=>2,'three'=>3,'four'=>4,'five'=>5); $ch=$_POST['a']; switch($ch) { case 1:foreach($array as $key=>$value) { echo"key:$key val:$value.<br>"; } break; case 2:echo sizeof($array); break; case 3 :$len=sizeof($array); array_splice($array,$len); print_r( $array); break; case 4 : print_r(array_flip($array)); break; case 5 : shuffle($array); print_r($array); break; } ?>

Write a menu driven PHP program to perform the following operations on associative arrays: a) Sort the array by values (changing the keys) in ascending, descending order. b) Also sort the array by values without changing the keys. c) Filter the odd elements from an array. d) Sort the different arrays at a glance using single function. e) Merge the given arrays. f) Find the Union, intersection & set difference of two arrays.

FileName:program8.html <html> <body bgcolor="pink"> <form action="program8.php" method="post"> <h2>Enter choice :</h2> <input type="radio" name="ch" value=1> Sort array by values in ascending,descending order<br> <input type="radio" name="ch" value=2> Sort array by values without changing key values <br> <input type="radio" name="ch" value=3> Filter odd elements from array <br> <input type="radio" name="ch" value=4> Sort different array at glance using single function<br> <input type="radio" name="ch" value=5> Merge given two arrays <br> <input type="radio" name="ch" value=6> Find intersection of two array <br> <input type="radio" name="ch" value=7> Find union of two array <br> <input type="radio" name="ch" value=8> Find set difference of two array <br> <br> <input type="submit" value="SUBMIT"> <input type="reset" value="CLEAR"> </body> </html> FileName:-program8.php <?php function is_odd($var) { if($var%2==1) return $var; } $choice=$_POST['ch']; $arr=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'g'=>7,'h'=>8); $arr1=array('l'=>11,'m'=>22,'n'=>33,'o'=>44,'p'=>55,'q'=>66,'r'=>77,'s'=>88); $arr2=array('a'=>10,'b'=>20,'c'=>30,'d'=>40,'e'=>50,'f'=>60,'g'=>70,'h'=>80); switch($choice) { case 1: sort($arr); echo "Array in ascending order:<br>"; print_r($arr); rsort($arr); echo "<br>Array in descending order:<br>"; print_r($arr); break; case 2: asort($arr); echo "Array in ascending order:<br>"; print_r($arr); arsort($arr); echo "<br>Array in descending order:<br>"; print_r($arr); break; case 3:print_r(array_filter($arr,'is_odd')); break; case 4:array_multisort($arr,0,$arr1,1,$arr2,0); print_r($arr); echo "<br>"; print_r($arr1); echo "<br>"; print_r($arr2); echo "<br>"; break; case 5:print_r(array_merge($arr,$arr1)); break; case 6:print_r(array_intersect($arr,$arr1)); break; case 7: $union=array_merge($arr,$arr1); print_r(array_unique($union)); break; case 8: print_r(array_diff($arr,$arr1)); break; } echo "<br><a href ='program8.html'> RETURN</a>"; ?>

Write PHP script to define an interface which has methods area(), volume(). Define constant PI. Create a class cylinder which implements this interface and calculate area and volume.

FileName:program9.html <html> <body> <center> <h2> Calculate area and value of cylinder</h2> <form action="program9.php" method="POST"> <h3> Enter radius : <input type=text name=r><br> </h3> <h3> Enter height : <input type=text name=h><br><br> </h3> <input type=submit value=submit name=Show> <input type=reset value=Reset name=Reset> </form> </center> </body> <html> FileName:program9.php <?php $r=$_POST['r']; $h=$_POST['h']; define('PI','3.14'); interface cal { function area($r,$h); function vol($r,$h); } class cylinder implements cal { function area($r,$h) { $area = 2*PI*$r*($r+$h); echo "<h3>The area of cylinder is :$area</h3>"; } function vol($r,$h) { $vol = PI*$r*$r*$h; echo "<h3>The volume of cylinder is :$vol</h3>"; } } $c = new cylinder; $c->area($r,$h); $c->vol($r,$h); ?>

Write class declarations and member function definitions for an employee(code, name, designation).Design derived classes as emp_account(account_no, joining_date) from employee and emp_sal(basic_pay, earnings, deduction) from emp_account. Write a menu driven PHP program a) to build a master table b) to sort all entries c) to search an entry d) Display salary.

FileName:program10.html <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> </head> <body> <form class="" action="program10.php" method="post"> <h2>Employee Information</h2> <input type="radio" name="rad" value="1">Build a Master Table <br><br> <input type="radio" name="rad" value="2">Sort all entries <br><br> <input type="radio" name="rad" value="3">Search an entry by Employee's Name <input type="text" name="ename"> <br><br> <input type="radio" name="rad" value="4">Display Salary <br><br> <input type="submit" name="sub" value="Submit"> </form> </body> </html> FileName:-program10.php <?php class employee { public $code,$name,$desig; function __construct($a,$b,$c) { $this->code=$a; $this->name=$b; $this->desig=$c; } public function display_emp() { echo'<td>'.$this->code.'</td> <td>'.$this->name.'</td> <td>'.$this->desig.'</td>'; } public function getname() { return $this->name; } } class emp_acc extends employee { public $ano,$jdate; function __construct($a,$b,$c,$d,$e) { parent::__construct($a,$b,$c); $this->ano=$d; $this->jdate=$e; } public function display_acc() { echo'<td>'.$this->ano.'</td> <td>'.$this->jdate.'</td>'; } } class emp_sal extends emp_acc { public $bpay,$earns,$ded,$total; function __construct($a,$b,$c,$d,$e,$f,$g,$h) { parent::__construct($a,$b,$c,$d,$e); $this->bpay=$f; $this->earns=$g; $this->ded=$h; $this->total=$this->bpay + $this->earns - $this->ded; } public function display_sal() { echo'<td>'.$this->bpay.'</td> <td>'.$this->earns.'</td> <td>'.$this->ded.'</td> <td>'.$this->total.'</td> '; } } $emp[0]=new emp_sal('A923B','Ravindra','Staff','10001','21/09/2022','20000','5000','3000'); $emp[1]=new emp_sal('A823B','Kiran','HR','10002','22/09/2022','25000','5000','3000'); $emp[2]=new emp_sal('A723B','Sudhir','Analyst','10003','23/09/2022','22000','5000','3000'); $emp[3]=new emp_sal('A623B','Mahesh','Organiser','10004','24/09/2022','21000','5000','3000'); $emp[4]=new emp_sal('A523B','Ak','Manager','10005','25/09/2022','30000','5000','3000'); $emp[5]=new emp_sal('A423B','Ravi','Staff','10006','26/09/2022','20000','5000','3000'); $emp[6]=new emp_sal('A323B','Vashali','Analyst','10007','27/09/2022','21000','5000','3000'); $emp[7]=new emp_sal('A223B','Mahi','Organiser','10008','28/09/2022','21000','5000','3000'); $emp[8]=new emp_sal('A123B','Karna','Staff','10009','29/09/2022','20000','5000','3000'); $emp[9]=new emp_sal('A023B','Suraj','Staff','10010','30/09/2022','20000','5000','3000'); $ch=$_POST['rad']; $ename=$_POST['ename']; $flag=0; function mastertable($emp) { echo '<table border="1" width="100%"> <tr> <th>Code</th> <th>Name</th> <th>Designation</th> <th>Ac No</th> <th>Join date</th> <th>Basic Pay</th> <th>Earnings</th> <th>Deduction</th> <th>Total Salary</th></tr>'; for($i=0;$i<10;$i++) { echo '<tr>'; $emp[$i]->display_emp(); $emp[$i]->display_acc(); $emp[$i]->display_sal(); echo '</tr>'; } echo '</table>'; } switch($ch) { case 1: mastertable($emp); break; case 2: echo 'Sorting w.r.t Employee Code <br><br>'; function srt($a,$b) { return strcmp($a->code,$b->code); } usort($emp,"srt"); mastertable($emp); break; case 3: echo "Searching for employee $ename .....<br><br>"; for($i=0;$i<10;$i++) { $temp=$emp[$i]->getname(); if($temp==$ename) { $flag=1; break; } } if($flag==0) { echo "Not Found<br>"; } else { echo "Found in the records<br>"; } break; case 4: echo "Displaying Salary of all Employees .... <br><br>"; echo '<table border="1" width="100%"> <tr> <th>Employee Name</th> <th>Basic Pay</th> <th>Earnings</th> <th>Deduction</th> <th>Total Salary</th> </tr>'; for($i=0;$i<10;$i++) { echo '<tr> <td>'.$emp[$i]->getname().'</td> '; $emp[$i]->display_sal(); } break; ?>

Post a Comment

0 Comments