Slip8:
Design an HTML form to accept two strings from the user. Write a PHP script for
the following.
a. Find whether the small string appears at the start of the large string.
b. Find the position of the small string in the big string.
c. Compare both the string for first n characters, also the comparison should not be case
sensitive.
HTML FILE
<html>
<head>
<title>String functions </title>
</head>
<body>
<form action="slip_8.php" method="GET">
enter first string:<input type="text" name="str1"><br>
enter second string:<input type="text" name="str2"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
PHP FILE
<?php
$a=$_GET["str1"];
$b=$_GET["str2"];
$len1=strlen($a);
$len2=strlen($b);
if($len1>$len2)
{
$pos=strpos($a,$b);
if($pos==0)
{
echo"the SECOND string appears at the start of the FIRST string<br>";
echo"the SECOND string appears at $pos position<br>";
}
}
else
if($len1<$len2)
{
$pos=strpos($b,$a);
if($pos==0)
{
echo"the FIRST string appears at the start of the SECOND string<br>";
}
}
if(strcasecmp($a,$b)==0)
{
echo "Both Strings are equal<br>";
}
else
if(strcasecmp($a,$b)>0)
{
echo "first string bigger<br>";
}
else
{
echo "second string is bigger<br>";
}
?>
Slip9:
Write a PHP script for the following: Design a form having a text box and a drop
down list containing any 3 separators(e.g. #, |, %, @, ! or comma) accept a strings from the
user and also a separator.
a. Split the string into separate words using the given separator.
b. Replace all the occurrences of separator in the given string with some other separator.
c. Find the last word in the given string.
HTML FILE
<html>
<body>
<form action="[Link]"method="GET">
Enter a string:<input type="text" name="str1"><br>
choose a label
<select name="sep" id="sep">
<option value="#">#</option>
<option value="!">!</option>
<option value="@">@</option>
</select><br>
<input type="radio" name="op" value="a">Split The String Using The Seperator<br>
<input type="radio" name="op" value="b">Replace The Occurences Of The
Seperator With Another Seperator<br>
<input type="radio" name="op" value="c">Find The Last Word Of The String<br>
<input type="submit"value="submit">
</form>
</body>
</html>
PHP FILE
<?php
$str=$_GET['str1'];
$sep=$_GET['sep'];
$op=$_GET['op'];
switch($op)
{
case 'a':
$m=explode($sep,$str);
foreach($m as $a)
{
echo "$a<br>";
}
break;
case 'b':
$cnt=substr_count($str,$sep);
$n=str_replace($sep,"!",$str,$cnt);
echo "After changing separators<br>";
echo $n;
break;
case 'c':
$ar=explode(" ",$str);
$cnt=count($ar);
echo "This is the last word : ".$ar[$cnt-1];
break;
}
?>
Slip10:
Write a script to accept two integers(Use html form having 2
textboxes). Write a PHP script to,
a. Find mod of the two numbers.
b. Find the power of first number raised to the second.
c. Find the sum of first n numbers (considering first number as n)
d. Find the factorial of second number.
(Write a separate function for each of the above operations.)
HTML FILE
<html>
<head>
<title>Assignment 3 </title>
</head>
<body>
<form action="[Link]" value="GET">
Enter Two Numbers<br>
<input type="text" name="n1"><br><br>
<input type="text" name="n2"><br><br>
Select An Operation<br>
<input type="radio" name="op" value="mod">Mod Of The Two Numbers<br><br>
<input type="radio" name="op" value="power">Power Of The First Number Raised
To The Second<br><br>
<input type="radio" name="op" value="sum">The Sum Of First n Numbers<br><br>
<input type="radio" name="op" value="fact">Factorial Of The Second
Number<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
PHP FILE
<?php
$n1=$_GET["n1"];
$n2=$_GET["n2"];
$op=$_GET["op"];
function mod($n1,$n2)
{
$ans=0;
if($n2!=0)
$ans=$n1%$n2;
return $ans;
}
function power($b,$p)
{
$pow=1;
for($i=1;$i<=$p;$i++)
{
$pow=$pow*$b;
}
return $pow;
}
function sum($n1)
{
$sum=0;
for($i=1;$i<=$n1;$i++)
{
$sum=$sum+$i;
}
return $n3;
}
function fact($n2)
{
$n3=1;
for($i=1;$i<=$n2;$i++)
{
$n3=$n3*$i;
}
return $n3;
}
switch($op)
{
case "mod":
$result=mod($n1,$n2);
echo "Mod of $n1 and $n2 is $result.";
break;
case "power":
$result=power($n1,$n2);
echo "$n1 raised to $n2 is $result.";
break;
case "sum":
$result=sum($n1);
echo "Sum of first $n1 number is $result.";
break;
case "fact":
$result=fact($n2);
echo"Factorial of $n2 is $result.";
break;
}
?>
Slip12:
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 statement)
PHP FILE
<?php
$x=$_POST['s1'];
$y=$_POST['s2'];
$op=$_POST['op'];
function add($x=4,$y=2)
{
$result=$x+$y;
echo"adition is $result";
}
function sub($x=4,$y=2)
{
$result=$x-$y;
echo"subtraction is $result";
}
switch ($op)
{
case"1":
add($x,$y);
break;
case"2":
sub($x,$y);
break;
}
?>
HTML FILE
<html>
<body>
<form action="[Link]"method="POST">
first number<input type=text name=s2><br>
second number<input type=text name=s1><br>
chose opration from below<br>
addition<input type=radio value="1" name=op> <br>
subtraction<input type=radio value="2"name=op> <br>
<input type= submit value="submit"><br>
</form>
</body>
</html>
PHP FILE
<?php
include'[Link]';
?>
Slip12:
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 statement)
[Link] [Link]
<?php <?php
function add($x=4,$y=2) function sub($x=4,$y=2)
{ {
$result=$x+$y; $result=$x-$y;
echo"adition is $result"; echo"subtraction is $result";
} }
?> ?>
[Link]
<html>
<body>
<form action="[Link]"method="POST">
first number<input type=text name=s2><br>
second number<input type=text name=s1><br>
chose opration from below<br>
addition<input type=radio value="1" name=op> <br>
subtraction<input type=radio value="2"name=op> <br>
<input type= submit value="submit"><br>
</form>
</body>
</html>
[Link]
<?php
$op=$_POST['op'];
$x=$_POST['s1'];
$y=$_POST['s2'];
switch ($op)
{
case"1":
include '[Link]';
add($x,$y);
break;
case"2":
include '[Link]';
sub($x,$y);
break;
}
?>
Slip12:
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 statement)
[Link]
<html>
<body>
<form method="post" action="[Link]">
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'>Subtraction<br>
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>
[Link]
<?php
include '[Link]';
$a=$_POST['no1'];
$b=$_POST['no2'];
$c=$_POST['op'];
switch($c)
{
case 1:
$d=add($a,$b);
echo "Addition :$d";
break;
case 2:
$d=sub($a,$b);
echo "Subtraction:$d";
break;
}
?>
[Link]
<?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;
}
?>
Slip15:
Design a form to accept string from the user and perform the following operations
[Link] select first 5 words from the string
b. Convert the given string to lowercase and then to Title case.
c. Pad the given string with “*” from left and right both the sides.
d. Remove the leading whitespaces from the given string.
[Link] the reverse of given string.
HTML FILE
<html>
<body>
<form action="[Link]" method=get>
Enter a String<input type=text name=t1><br>
<input type=radio name=op value=1>To select first 5 words from the string <br>
<input type=radio name=op value=2>Convert the given string to lowercase and then to Title
case.<br>
<input type=radio name=op value=3>Pad the given string with “*” from left and right both the
sides<br>
<input type=radio name=op value=4>Remove the leading whitespaces from the given string.
<br>
<input type=radio name=op value=5>Reverse of given string<br><br>
<input type=submit value=submit>
</form>
</body>
</html>
PHP FILE
<?php
$str=$_GET['t1'];
$ch=$_GET['op'];
ECHO "ORGINAL STRING:\n".$str."<br>";
switch($ch)
{
case 1:
$ar=explode(" ",$str,6);
foreach($ar as $a)
echo "$a<br>";
break;
case 2:
echo "<br>Converted the given string in to lowercase<br>";
$str=strtolower($str);
echo $str;
echo "<br><br>Converted the given string in to Title case.<br>";
$str=ucwords($str);
echo "<br>$str";
break;
case 3:
echo "After Pad the given string with “*” from left and right both the
sides<br>";
$str=str_pad($str,20,"*",STR_PAD_BOTH);
echo $str;
break;
case 4:
$str= preg_replace('/\s+/', '', $str);
echo $str;
break;
case 5:
$str=strrev($str);
echo $str;
break;
}
?>
Slip16:
Write a PHP script for the following: Design a form to accept the marks of 5
different subjects of a student, having serial number, subject name & marks out of 100.
Display the result in the tabular format which will have total, percentage and grade. Use only
3 text boxes.(Use array of form parameters)
HTML FILE
<html>
<head>
<title> marksheet </title>
</head>
<body>
<form name = "string" action = "[Link]" method = "GET">
student id <input type = "text" name = "v1"/></br></br>
Enter 5 Subject name<input type = "text" name = "v2"/></br></br>
Enter 5 subject marks<input type = "text" name = "v3"/></br></br>
<button type = "submit"> display marklist </button>
</form>
</body>
</html>
PHP FILE
<?php
$a =$_GET['v1'];
$b =$_GET['v2'];
$c =$_GET['v3'];
$sum=0;
echo "<h1> <center>Marksheet</center></h1>";
echo "<h3><center>student id:$a</h3><br>";
$d =explode(",",$b);
$e =explode(",",$c);
echo "<center><table border=2 width=50%>";
for($i=0;$i<=4;$i++)
{
echo "<tr>
<td>$d[$i]</td>
<td>$e[$i]</td>
</tr>";
$sum=$e[$i]+$sum;
}
$result=$sum/5;
echo "<tr><td>Total marks </td><td>$sum</td>";
echo"<tr><td>Percentage</td><td>$result</td></tr>";
echo "</center>"
?>
Slip19:
Write a PHP script to accept 2 strings from the user, the first string should be a
sentence and second can be a word.
a. Delete a small part from first string after accepting position and number of
characters to remove.
b. Insert the given small string in the given big string at specified position without
removing any characters from the big string.
[Link] some characters/ words from given big string with the given small string at
specified position.
HTML FILE
<html>
<body>
<form action='[Link]' method="GET">
Enter a Sentence <input type=text name=t1><br>
Enter a word <input type=text name=t2><br>
Enter position <input type=text name=t3><br>
Enter number of characters to remove<input type=text name=t4><br>
select operation<br>
<input type=radio name=op value=1>Delete a small part from first string after accepting position
and number of characters to remove. <br>
<input type=radio name=op value=2>Insert the given small string in the given big string at
specified position without removing any characters from the big string. <br>
<input type=radio name=op value=3>Replace some characters/ words from given big string with
the given small string at specified position<br>
<input type=submit value="Display Result">
</form>
</body>
</html
PHP FILE
<?php
$st=$_GET['t1'];
$wd=$_GET['t2'];
$ps=$_GET['t3'];
$nr=$_GET['t4'];
$dup_st=$st;
$op=$_GET['op'];
echo $st."<br>";
switch($op)
{
case 1:
$str=substr_replace($st,"",$ps,$nr);
echo "<br>$str<br>";
break;
case 2:
$str=substr_replace($st,$wd,$ps,0);
echo "<br>$str<br>";
break;
case 3:
$str=substr_replace($st,$wd,$ps,strlen($wd));
echo "<br>$str<br>";
break;
}
?>