PHP 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.)


HTML File:


<html>

<body>

<form method="GET" action="ass1a1.php">

Enter the String : <input type="text" name="inputStr"><br>

<input type="submit" name="Submit">

</form>

</body>

</html>


PHP Function:

NOTE: PHP function is saved as "ass1a1.php"


<?php


$string = $_GET['inputStr'];


$vowels = array("a"=>0,"e"=>0,"i"=>0,"o"=>0,"u"=>0);


for($i=0; $i<strlen($string); $i++) {

if(strtolower($string[$i]) == 'a') {

++$cnt;

++$vowels['a'];

}

if(strtolower($string[$i]) == 'e') {

++$cnt;

++$vowels['e'];

}

if(strtolower($string[$i]) == 'i') {

++$cnt;

++$vowels['i'];

}

if(strtolower($string[$i]) == 'o') {

++$cnt;

++$vowels['o'];

}

if(strtolower($string[$i]) == 'u') {

++$cnt;

++$vowels['u'];

}

}


echo "<h1>Total number of vowels found : ".$cnt."<h1>";

echo "Occurence of 'a' : ".$vowels['a']."<br>";

echo "Occurence of 'e' : ".$vowels['e']."<br>";

echo "Occurence of 'i' : ".$vowels['i']."<br>";

echo "Occurence of 'o' : ".$vowels['o']."<br>";

echo "Occurence of 'u' : ".$vowels['u']."<br>";

$str=strrev($string);

$a=strlen($string);

$f=0;

for($j=0;$j<$a;$j++)

{

    if($str[$j]==$string[$j])

    {

        $f=0;

    }

    else

    {

        $f=1;

        break;

    }

}

if($f==0)

{

    echo"string is palindrome";

}

else

{

    echo"string is not palindrome";

}

?>







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)



HTML File:


<html>

<head><title>php program</title></head>

<body bgcolor='#f1284e' >

<form action='ass1a2.php' method='post'>

<pre>

Enter 1'st string       :<input type='text' name='str1'><br>

                                                                                                                         

Enter 2'nd string       :<input type='text' name='str2'><br>

                                                                                                                         

Enter string to replace small string    :<input type='text' name='str'><br>

                                                                                                                         

        <input type='radio' name='ch' value=1>first and last occurence<br>

        <input type='radio' name='ch' value=2>total occurence<br>

        <input type='radio' name='ch' value=3>replace<br>

        <input type=submit value= 0k ><input type=reset value=cancel></h2>

</pre>

</form>

</body>


</html>


PHP Function:

NOTE: PHP function is saved as "ass1a2.php"


<?php

        $str1=$_POST['str1'];

        $str2=$_POST['str2'];

        $str=$_POST['str'];

        $ch=$_POST['ch'];

        $oc=0;

     

        echo"<br>string1:: $str1<br>string2:: $str2<br>string for replace:: $str</b><br>";

        if($str1>$str2)

        {

        switch($ch)

                {

                case 1: echo"<br>First occ is at possition :";echo strpos($str1,$str2);

                        echo"<br>Last  occ is at possition :";echo strrpos($str1,$str2);

                        break;

                case 2:$oc=substr_count($str1,$str2);

                        if($oc==0)

                                echo"<br>string '$str2' not present in string '$str1'<br>";

                        else

                                 echo"<br>sub string '$str2' present $oc times in string '$str1'<br>";

                        break;

                case 3:

                                $str3=str_replace($str2,$str,$str1);

                                echo"<br>After relacing string is::$str3";

                        break;

                }

        }

        else

        {

        switch($ch)

                {

                 case 1:echo"<br>First occ is at position :";echo strpos($str2,$str1);

                        echo"<br>Last occ is at possition :";echo strrpos($str2,$str1);

                        break;

                case 2:$oc=substr_count($str2,$str1);

                         if($oc==0)

                                echo"<br>string '$str1' not present in string '$str2'.<br>";

                        else

                                 echo"<br>sub string '$str1' present $oc times in string '$str2'.<br>";

                        break;

                case 3:

                                $str3=str_replace($str1,$str,$str2);

                                echo"<br>After relacing string::'$str3'.";

                        break;

                }

        }

     

?>

<h1><a href='a1a2.html'>go back</a></h1>






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)



HTML File:


<html>

        <body bgcolor=pink >

            <form action="ass1b1.php" method= "GET" >

            Enter first number :  <input type=text name=a ><br>

            Enter second number:  <input type=text name=b ><br>

                                                                                                                             

        Operation::

                <input type=radio name=c value=1>Addition.<br>

                <input type=radio name=c value=2>Subtraction.<br>

                <input type=radio name=c value=3>Multiplication.<br>

                <input type=radio name=c value=4>Division.<br>

        <input type=submit value=ok>    <input type=reset value=clear>

        </form>

        </body>


 </html>


PHP Function:

NOTE: PHP function is saved as "ass1b1.php"


<?php

$n1 = $_GET['a'];

$n2 = $_GET['b'];

$ch = $_GET['c'];


        if($ch==1)

        {

            $c = $n1 + $n2;

            echo"addition is: $c"; 

        }

        else if($ch==2)

        {

            $c = $n1 - $n2; 

            echo"subtraction is: $c";

        }

        else if($ch==3)

        {

            $c = $n1 * $n2; 

            echo"multiplication is: $c";

        }

        else if($ch==4)

        {

            $c = $n1 / $n2; 

            echo"Division is: $c";

        

        }

?>








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)



HTML File:

<html>
<body>
<form action='ass1b2' 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.                                                                                                   
                        <input type=submit value=ok>    <input type=reset value=cancel>
</pre>
</form>
</body>

</html>

PHP Function:
NOTE: PHP function is saved as "ass1b2.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.)



HTML file :

<html>
<head>
<style>
table {
    border-collapse: collapse;
}

table, th, td {
    border: 1px solid blue;
}
</style>
</head>
<body>
<form method="post" action="slip6_Q2.php">
<center><h1>Enter details of 5 Items </h1>
<h3>item code  <input type="text" name="itemcode" placeholder="ex 11,12,13,14,15" /></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>
</form>
</body>
</html>

PHP file :

<?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);
                             
                $t_amt = (($i_unit[0]*$i_rate[0])+($i_unit[1]*$i_rate[1])+($i_unit[2]*$i_rate[2])+($i_unit[3]*$i_rate[3])+($i_unit[4]*$i_rate[4]));

                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>";
                echo "<tr><td>".$i_code[0]."</td><td>".$i_name[0]."</td><td>".$i_unit[0]."</td><td>".$i_rate[0]."</td></tr>";
                echo "<tr><td>".$i_code[1]."</td><td>".$i_name[1]."</td><td>".$i_unit[1]."</td><td>".$i_rate[1]."</td></tr>";
                echo "<tr><td>".$i_code[2]."</td><td>".$i_name[2]."</td><td>".$i_unit[2]."</td><td>".$i_rate[2]."</td></tr>";
                echo "<tr><td>".$i_code[3]."</td><td>".$i_name[3]."</td><td>".$i_unit[3]."</td><td>".$i_rate[3]."</td></tr>";
                echo "<tr><td>".$i_code[4]."</td><td>".$i_name[4]."</td><td>".$i_unit[4]."</td><td>".$i_rate[4]."</td></tr>";
                echo "<tr><th colspan=4></th></tr>";
                echo "<tr><th colspan=3>Total amount </th><td>".$t_amt."</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)


HTML file :

<html>
<head>
<style>
table {
    border-collapse: collapse;
}

table, th, td {
    border: 1px solid blue;
}
</style>
</head>
<body>
<form method="post" action="slip6_Q2.php">
<center><h1>Enter details of 5 Items </h1>
<h3>item code  <input type="text" name="itemcode" placeholder="ex 11,12,13,14,15" /></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>
</form>
</body>
</html>

PHP file :

<?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);
                             
                $t_amt = (($i_unit[0]*$i_rate[0])+($i_unit[1]*$i_rate[1])+($i_unit[2]*$i_rate[2])+($i_unit[3]*$i_rate[3])+($i_unit[4]*$i_rate[4]));

                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>";
                echo "<tr><td>".$i_code[0]."</td><td>".$i_name[0]."</td><td>".$i_unit[0]."</td><td>".$i_rate[0]."</td></tr>";
                echo "<tr><td>".$i_code[1]."</td><td>".$i_name[1]."</td><td>".$i_unit[1]."</td><td>".$i_rate[1]."</td></tr>";
                echo "<tr><td>".$i_code[2]."</td><td>".$i_name[2]."</td><td>".$i_unit[2]."</td><td>".$i_rate[2]."</td></tr>";
                echo "<tr><td>".$i_code[3]."</td><td>".$i_name[3]."</td><td>".$i_unit[3]."</td><td>".$i_rate[3]."</td></tr>";
                echo "<tr><td>".$i_code[4]."</td><td>".$i_name[4]."</td><td>".$i_unit[4]."</td><td>".$i_rate[4]."</td></tr>";
                echo "<tr><th colspan=4></th></tr>";
                echo "<tr><th colspan=3>Total amount </th><td>".$t_amt."</td></tr>";
                echo "</table>";
?>






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()].


HTML File:

<html>
<form action='ass2a1.php' method='post'>
<pre>   <h3>OPERATIONS</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>

PHP Function:
NOTE: PHP function is saved as "ass2a1.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.




HTML File:

<html>

<body bgcolor="skyred">
<form action="a2c2.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>

PHP Function:
NOTE: PHP function is saved as "ass2a1.php"


<html>
<body bgcolor="gold">

<?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 ='a2c2.html'> RETURN</a>";
?>
</font>
</body>
</html>













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.




HTML File:
<html>
<head><title>A4A1</title></head>

<body bgcolor="GOLD" font type='San Sarif'>
<center><h2>Implementation of Interface and Class</h2></center>
<br/>
<hr>

<form action="a4a1.php" method='POST'>

<h3>Enter radius        :<input type='text' name='rad'><br/></h3>
<h3>Enter height        :<input type='text' name='hit'><br/></h3>
<br/>
<br/>
<pre><input type='submit' name='submit' value='Area.'>  <input type='reset' name='reset' value='reset'>
</form>
</body>
</html>

PHP Function:
NOTE: PHP function is saved as "a4a1.php"

<?php

$r=$_POST['rad'];
$h=$_POST['hit'];

define('PPI','3.1412');

Interface fun
{

        function area($r,$h);
        function volume($r,$h);
}


class cylinder implements fun
{

        function area($r,$h)
        {
         $area=(2*PPI*$r*($r+$h));
         echo "<center><h3>The area of cylinder is:     $area</font></center></h3>";
        }

        function volume($r,$h)
        {
         $v=(PPI*$r*$r*$h);
         echo "<center><h3>The volume of cylinder is:     $v</font></center></h3>";
        }
}


$o=new cylinder;
$o->area($r,$h);
$o->volume($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.





Post a Comment

0 Comments