Javascript required

Semester IV


Bsc.Physical Science Computer Science (CBCS) Programs


PHP Programs



Question 1 :  Create a PHP page using functions for comparing three integers and print the largest
              number.
                                
 PHP Code
                                         largest_of_three.php
                                    <?php
extract($_POST);
if(isset($submit))
{
    $maxValue=max($fnum,max($snum,$tnum));
}
?>
<html>
<head>
	<title>Greater of three</title>
</head>
<body>
<h3><center>PHP Program to find greater of three</center></h3>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" value="<?php echo @$maxValue;?>"/></td>
	</tr>
	<tr>
		<th>First number</th>
		<td><input type="text" name="fnum" value="<?php echo @$fnum;?>"/></td>
	</tr>	
	<tr>
		<th>Second number</th>
		<td><input type="text" name="snum" value="<?php echo @$snum;?>"/></td>
	</tr>
	<tr>
		<th>Third number</th>
		<td><input type="text" name="tnum" value="<?php echo @$tnum;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
Video Explanation

Question 2 : Write a function to calculate the factorial of a number (non-negative integer). The function
             accept the number as an argument.
                                
 PHP Code
                                         fact.php
                                    <?php  
extract($_POST);
if(isset($submit))
{
    function fact ($n)
    {
        if($n <= 1)
        {
            return 1;
        }
        else
        {
            return $n * fact($n - 1);
        }
    }
    $cal = fact($fnum);  
} 
?>
<html>
<head>
	<title>Factorial</title>
</head>
<body>
<h3><center>PHP Program to find factorial of a number by passing argument to the function</center></h3>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" value="<?php echo @$cal;?>"/></td>
	</tr>
	<tr>
		<th>Enter number</th>
		<td><input type="text" name="fnum" value="<?php echo @$fnum;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
                                

Question 3 :  WAP to check whether the given number is prime or not.
                                
 PHP Code
                                         prime.php
                                    <?php  
extract($_POST);
if(isset($submit))
{
    function Prime($n)
    {
        for($x=2; $x<$n; $x++)
        {
            if($n % $x ==0) { return false;}
        }
        return true;
    }
    $var = Prime($fnum);
} 
?>
<html>
<head>
	<title>Prime</title>
</head>
<body>
<h3><center>PHP Program to check whether the given number is prime or not</center></h3>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" value="<?php echo @$var?"Prime Number":"Not Prime"; ?>"/></td>
	</tr>
	<tr>
		<th>Enter number</th>
		<td><input type="text" name="fnum" value="<?php echo @$fnum;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
                                

Question 4 :  Create a PHP page which accepts string from user. After submission that page displays the
              reverse of provided string.
                                
 PHP Code
                                         reverse.php
                                    <?php  
extract($_POST);
if(isset($submit))
{
    $var = strrev($str);
} 
?>
<html>
<head>
	<title>Reverse</title>
</head>
<body>
<h3><center>PHP Program to reverse the given string</center></h3>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" value=""/></td>
	</tr>
	<tr>
		<th>Enter String</th>
		<td><input type="text" name="str" value=""/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
                                

Question 5 :  Write a PHP function that checks if a string is all lower case.
                                
 PHP Code
                                         check_lower.php
                                    <?php  
extract($_POST);
if(isset($submit))
{
    function lowercase($str)
    {
        for ($sc = 0; $sc < strlen($str); $sc++)
        {
            if (ord($str[$sc]) >= ord('A') && ord($str[$sc]) <= ord('Z'))
            {
                return false;
            }
        }
        return true;
    }
    $var = lowercase($str);
} 
?>
<html>
<head>
	<title>Check lowercase</title>
</head>
<body>
<h3><center>PHP Program to check the lowercase</center></h3>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" value="<?php echo @$var?"All are in lowercase":"Not in lowercase" ?>"/></td>
	</tr>
	<tr>
		<th>Enter String</th>
		<td><input type="text" name="str" value="<?php echo @$str;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
                                

Question 6 : Write a PHP script that checks whether a passed string is palindrome or not? ( A palindrome
             is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run)
                                
 PHP Code
                                         check_palindrome.php
                                    <?php  
extract($_POST);
if(isset($submit))
{
    $var = strrev($str);
    if(strcmp($var, $str)==0)
    {
        $var = "Palindrome";
    }
    else
        $var = "Not a palindrome";
} 
?>
<html>
<head>
	<title>Palindrome</title>
</head>
<body>
<h3><center>PHP Program to check whether the given string is palindrome or not</center></h3>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" value="<?php echo @$var ?>"/></td>
	</tr>
	<tr>
		<th>Enter String</th>
		<td><input type="text" name="str" value="<?php echo @$str;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
                                

Question 7 :  WAP to sort an array.
                                
 PHP Code
                                         sort.php
                                    
<html>
<head>
    <meta charset="utf-8">
    <title>Sorting</title>
    <style type="text/css" media="screen">
    .hidden {
                display: none;
            }
    </style>
</head>
<body>
<form name="form1" id="form1" method="post" action="<?php $_SERVER['PHP_SELF']?>">
<center><h3>PHP program to sort an array (Bubble sort)</h3>
<h4>Fill the values</h4>
<label>Value 1 <input type="text" name="bubble_values[]" id="bubble_values1" size="10" maxlength="10">
</label><br>
<label>Value 2 <input type="text" name="bubble_values[]" id="bubble_values2" size="10" maxlength="10">
</label><br>
<label>Value 3 <input type="text" name="bubble_values[]" id="bubble_values3" size="10" maxlength="10">
</label><br>
<label>Value 4 <input type="text" name="bubble_values[]" id="bubble_values4" size="10" maxlength="10">
</label><br>
<label>Value 5 <input type="text" name="bubble_values[]" id="bubble_values5" size="10" maxlength="10">
</label><br>
<input type="submit" value="Sort it!" id="bt-submit"></center>
</form>
<center>
<h3>Result</h3>
<?php
function bubbleSort(array $arr)
{
    $n = sizeof($arr);   
    for ($i = 1; $i < $n; $i++)
    {
        $flag = false;
        for ($j = $n - 1; $j >= $i; $j--)
        {
            if($arr[$j-1] > $arr[$j])
            {
                $tmp = $arr[$j - 1];
                $arr[$j - 1] = $arr[$j];
                $arr[$j] = $tmp;
                $flag = true;
            }
        }
        if (!$flag)
        {
            break;
        }
    }
    return $arr;
}
if (isset($_POST['bubble_values']) && !empty($_POST['bubble_values']))
{
    echo '<pre>';
    echo 'Before sort: ';
    print_r($_POST['bubble_values']);
    echo '<br>--------------------<br>';
    echo 'After sort:' ;
    print_r(bubbleSort($_POST['bubble_values'])); 
    echo '</pre>';
}
?>
</center>
</body>
                                

Question 8 :  Write a PHP script that removes the whitespaces from a string.
              Sample string : 'The quick " " brown fox'
              Expected Output : Thequick""brownfox
                                
 PHP Code
                                         rm_space.php
                                    <?php  
extract($_POST);
if(isset($submit))
{
    $var = preg_replace('/\s+/', '', $str);
} 
?>
<html>
<head>
	<title>Remove Whitespaces</title>
</head>
<body>
<h3><center>PHP Program to remove all whitespaces</center></h3>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" value="<?php echo @$var ?>"/></td>
	</tr>
	<tr>
		<th>Enter String</th>
		<td><input type="text" name="str" value="<?php echo @$str;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
                                

Question 9 :   Write a PHP script that finds out the sum of first n odd numbers.
                                
 PHP Code
                                         sum_of_odd_numbers.php
                                    <?php  
extract($_POST);
if(isset($submit))
{
    function oddSum($n)
    {
        $sum = 0; $curr = 1;
        for ($i = 0; $i < $n; $i++)
        {
            $sum += $curr;
            $curr += 2;
        }
        return $sum;
    }
    $var = oddSum($val);
} 
?>
<html>
<head>
	<title>Sum of n odd numbers</title>
</head>
<body>
<h3><center>PHP Program to calculate sum of 'n' odd numbers</center></h3>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" value="<?php echo @$var ?>"/></td>
	</tr>
	<tr>
		<th>Enter n </th>
		<td><input type="text" name="val" value="<?php echo @$val;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
                                

Question 10 :  Create a login page having user name and password. On clicking submit, a welcome
               message should be displayed if the user is already registered 
               (i.e.name is present in the database) otherwise error message should be displayed.
                                
 PHP Code
                                         login.php
                                    <?php  
extract($_POST);
if(isset($submit))
{
    $u = "utteralley";
    $p = "qwerty";
    if((strcmp($pass, $u)==0) and (strcmp($pass, $p)==0))
    {
        header("location: welcome.html");
    }
    else
        $error = "Your Login Name or Password is invalid";
} 
?>
<html>
<head>
      <title>Login Page</title>
      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }
         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }
         .box {
            border:#666666 solid 1px;
         }
      </style>
</head>
<body bgcolor = "#FFFFFF">
	 <div align = "center">
         <div style = "width:300px; border: solid 1px #333333; " align = "left">
            <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
				 <div style = "margin:30px">
                     <form method="post">
   	               <label>Username :</label>
	                <input type="text" name="user" value="<?php echo @$user;?>"/>
	                 <label>Password :</label>
                   	 <input type="text" name="pass" value="<?php echo @$pass;?>"/><br /><br />
	                 <input type="submit" value="submit" name="submit"/>
	                 <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo @$error;?
	                 ></div>
	                 </form>
	             </div>
	        </div>
	   </div>
 </body>
</html>
                                
 
                                         welcome.html
                                    <html>
<head>
    <title>Welcome </title>
</head>
<body>
<h1>Your are sucessfully logged in !!!</h1> 
</body>
</html>
                                

Question 11 :  Write a PHP script that checks if a string contains another string.
                                
 PHP Code
                                         check_substring.php
                                    <?php
extract($_POST);
if(isset($submit))
{
    if (strpos($fstr,$str) !== false)
    {
        $var = "The specific word is present.";
    }
    else
    {
        $var = "Word is not present.";
    }
}
?>
<html>
<head>
	<title>Check for another string</title>
</head>
<body>
<h3><center>PHP Program to check whether the string contains another string</center></h3>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" value="<?php echo @$var;?>"/></td>
	</tr>
	<tr>
		<th>Enter string</th>
		<td><input type="text" name="fstr" value="<?php echo @$fstr;?>"/></td>
	</tr>	
	<tr>
		<th>Word want to search</th>
		<td><input type="text" name="str" value="<?php echo @$str;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
                                

Question 12 : Create a simple 'birthday countdown' script, the script will count the number of days 
              between current and birth day.
                                
 PHP Code
                                         bday_countdown.php
                                    <?php
extract($_POST);
if(isset($submit))
{
    $birthdate = strtotime($fstr); //31-12-1993
    $d = date('d-m-Y h:i:s a', time());
    $today = strtotime($d);
    $fixedBirthdate = date_create(date("Y", $today) . "-" . date("m", $birthdate) . "-" . date("d", $birthdate));
    $diff = date_diff($fixedBirthdate, date_create(date("d-m-Y", $today)));
    $var = $diff->format("%R%a days");
}
?>
<html>
<head>
	<title>Birthday Countdown</title>
</head>
<body>
<h3><center>PHP program to count number of days between current and birth day</center></h3>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" value="<?php echo @$var;?>"/></td>
	</tr>
	<tr>
		<th>Enter your birthdate</th>
		<td><input type="text" name="fstr" value="<?php echo @$fstr;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
                                

Question 13 :  Create a script to construct the following pattern, using nested for loop.
                *
                * *
                * * *
                * * * *
                * * * * *
                                
 PHP Code
                                         pattern.php
                                    <html>
<head>
	<title>Pattern</title>
</head>
<body>
<h3><center>PHP program to print pattern (half triangle)</center></h3>
<form method="post">
 <table align="center" border="1">
	<tr>
		<th>Enter your birthdate</th>
		<td><input type="text" name="fstr" value="<?php echo @$fstr;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>	
</form>
</body>
</html>
<?php
extract($_POST);
if(isset($submit))
{
    for($i=0;$i<=$fstr;$i++)
    {
        for($j=0;$j<=$i;$j++)
        {
            echo "* ";
        }
        echo "<br>";
    }
}
?>
                                

Question 14 : Write a simple PHP program to check that emails are valid.
                                
 PHP Code
                                         E-mail Validation.php
                                    <?php
extract($_POST);
if(isset($submit))
{
    if (empty($email))
    {
        $emailErr = "Email is required";
    } 
    else 
    {
        if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^",$email)) 
        {
            $emailErr = "Invalid email format";
        }
        else
        {
            $emailErr = "Valid email format";
        }
    }
}
?>
<html>
<head>
	<title>Validate E-mail</title>
</head>
<body>
<h3><PHP program to check whether the email is valid or not</center></h3>
<form method="post">
 <table align="center" border="1">
	<tr>
		<th>Enter E-mail</th>
		<td><input type="text" name="email" value="<?php echo @$email;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>
	<center><div style = "font-size:30px; color:#cc0000; margin-top:10px"><?php echo @$emailErr;?>
	</div></center>	
</form>
</body>
</html>
                                

Question 15 :  WAP to print first n even numbers.
                                
 PHP Code
                                         neven.php
                                    <html>
<head>
	<title>N even numbers</title>
</head>
<body>
<h3><center>PHP program to print N even numbers</center></h3>
<form method="post">
 <table align="center" border="1">
	<tr>
		<th>Enter n</th>
		<td><input type="text" name="num" value="<?php echo @$num;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="submit" name="submit"/>
	</tr>
	</table>
	</div></center>	
</form>
</body>
</html>
<?php
extract($_POST);
if(isset($submit))
{
    $a = 2;
    $d = 2;
    for ($i = 0; $i < $num; $i++)
    {
        $var = $a + $i * $d;
        echo $var." ";
    }
}
?>
                                

Question 16 :  $color = array('white', 'green', 'red')
               Write a PHP script which will display the colors in the following way :
               Output :
               white, green, red,
               a. green
               b. red
               c. white
                                
 PHP Code
                                         disp_color.php
                                    <?php
$arr = array('white','green','red');
function bubbleSort(array $arr)
{
    $n = sizeof($arr);   
    for ($i = 1; $i < $n; $i++)
    {
        $flag = false;
        for ($j = $n - 1; $j >= $i; $j--)
        {
            if($arr[$j-1] > $arr[$j])
            {
                $tmp = $arr[$j - 1];
                $arr[$j - 1] = $arr[$j];
                $arr[$j] = $tmp;
                $flag = true;
            }
        }
        if (!$flag)
        {
            break;
        }
    }
}
for($i=0; $i<count($arr); $i++)
{  //end when $i is larger than amount of people
    echo "  <li>{$arr[$i]}</li>\n";
}
?>
                                

Question 17 : Using switch case and dropdown list display a “Hello” message depending on the language
              selected in drop down list.
                                
 PHP Code
                                         hello.php
                                    <html>
<head>
	<title>Print hello</title>
</head>
<body>
<h3><center>PHP program to hello message using switch-case</center></h3>
<form method="post">
	<center>
		<select name="languages">
        <option value="Germany" >Germany</option>
        <option value="English" >English</option>
        <option value="French" >French</option>
        </select>
		<input type="submit" value="submit" name="submit"/>
	</center>
</form>
</body>
</html>

                                

Question 18 :  Write a PHP program to print Fibonacci series using recursion.
                                
 PHP Code
                                         fibonacci.php
                                    <html>
<head>
    <title>Fibonacci</title>
</head>
<body>
<h3><center>PHP Program to calculate fibonacci series
</center></h3>
<form method="post">
 <table align="center" border="1">
    <tr>
        <th>Enter number</th>
        <td><input type="text" name="num" value="<?php echo @$num;?>"/></td>
    </tr>
    <tr>
        <td align="center" colspan="2">
        <input type="submit" value="submit" name="submit"/>
    </tr>
    </table> 
</form>
</body>
</html>
<?php
extract($_POST);
if(isset($submit))
{
    function Fibonacci($number)
    {
        if ($number == 0)
            return 0;
        else if ($number == 1)
            return 1;
        else
            return (Fibonacci($number-1) + Fibonacci($number-2)); 
     } 
     for ($i=0; $i < $num ; $i++)
     {
        echo Fibonacci($i)," ";
     }
 }  
?>
                            

Question 19 : Write a PHP script to replace the first 'the' of the following string with 'That'.
              Sample : 'the quick brown fox jumps over the lazy dog.'
              Expected Result : That quick brown fox jumps over the lazy dog.
                                
 PHP Code
                                         replace.php
                                    <html>
<head>
    <title>Replace</title>
</head>
<body>
<h3><center>PHP Program to replace 'the' with 'that'
</center></h3>
<form method="post">
 <table align="center" border="1">
    <tr>
        <th>Enter string</th>
        <td><input type="text" name="str" value="<?php echo @$str;?>"/></td>
    </tr>
    <tr>
        <td align="center" colspan="2">
        <input type="submit" value="submit" name="submit"/>
    </tr>
    </table> 
</form>
</body>
</html>
<?php
extract($_POST);
if(isset($submit))
{
    $Replacement = 'that';
    echo preg_replace('/the/', $Replacement, $str, 1);
}  
?>