Swati PHP
Swati PHP
OF
         WEB APPLICATION
      DEVELOPMENT USING PHP
                                 (BCA-16-504)
<body>
       <h1> CELSIUS TO FAHRENHEIT</h1>
<?php
       $celsius=46;
       $fahrenheit=(($celsius+32)*9)/5;
       echo"temperature in fahrenheit: $fahrenheit";
?>
</body>
</html>
Output:-
                                                                 page2
   2. Write a PHP script to convert days into years.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <h1> CONVERT DAYS INTO YEARS</h1>
<?php
       $days=4446;
       $years=($days)/365;
       echo("number of years is:");
       echo($years);
?>
</body>
</html>
output:-
                                                                  page3
   3. Write a PHP script to find the smallest of three numbers.
<body>
       <h1> smallest of three numbers </h1>
<?php
       $a=40;
       $b=15;
       $c=30;
       if($a<$b && $a<$c)
               echo "a is smallest";
       elseif($b<$c && $b<$c)
               echo "b is smallest";
       else
               echo "c is smallest";
?>
</body>
</html>
Output:-
                                                                 page4
4.Write a PHP script to find the first 15 prime numbers
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <h1> FIRST 15 PRIME NUMBERS </h1>
<?php
       $count=0;
       $num=2;
while($count<15)
{
       $div_count=0;
       for($i=1;$i<=$num;$i++)
       {
               if(($num%$i)==0)
               {
                      $div_count++;
               }
       }
       if($div_count<3)
       {
               echo $num."<br>";
               $count=$count+1;
       }
       $num=$num+1;
}
?>
</body>
</html>
output:
                                                                      page5
<body>
<?php
       for($i=0;$i<=5;$i++)
       {
              for($j=1;$j<=$i;$j++)
              {
                     echo "*";
              }
              echo "<br>";
       }
?>
</body>
</html>
output:-
                                                                      page6
<body>
<?php
       for($i=0;$i<=5;$i++)
       {
              for($j=5-$i;$j>=1;$j--)
              {
                     echo "*";
              }
              echo"<br>";
       }
?>
</body>
</html>
Output:-
                                                                     page7
             7.Write a PHP script to create the following pattern.
       *
       **
      ***
      ****
     *****
<body>
<?php
       $n=5;
       for($i=0;$i<=$n;$i++)
       {
              for($j=0;$j<=$n-$i;$j++)
              {
                     echo" ";
              }
              for($k=1;$k<=$i;$k++)
              {
                     echo"*  ";
              }
              echo"<br>";
       }
?>
</body>
</html>
output:
                                                                     page8
<body>
<?php
       $a=1;
       for($i=1;$i<=4;$i++)
       {
              for($j=1;$j<=$i;$j++)
              {
                     echo"$a  ";
                     $a++;
              }
       echo"<br>";
       }
?>
</body>
</html>
Output:-
                                                                     page9
             9.Write a PHP script to create the following pattern.
54321
4321
321
21
1
 <body>
<?php
       for($i=1;$i<=5;$i++)
       {
              for($j=5-$i+1;$j>=1;$j--)
              {
                     echo"$j ";
              }
              echo"<br>";
       }
?>
</body>
</html>
Output:-
                                                                   page10
            10.Write a PHP script to print the factorial of a number.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<?php
       $num=5;
       $factorial=1;
       for($x=$num;$x>=1;$x--)
       {
               $factorial=$factorial*$x;
       }
       echo"factorial of $num is $factorial";
?>
</body>
</html>
Output:-
                                                                    page11
                 11.Write a PHP script to print fibonacci series.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<?php
       $n=8;
       $first_val=0;
       $second_val=1;
       echo "Fibonacci Series of first 8 numbers:<br>";
       echo "$first_val$second_val";
       for($i=2;$i<$8;$i++)
       {
               $third_val=$first_val+$second_val;
               echo"$third_val";
               $first_val=$second_val;
               $secocond_val=$third_val;
       }
?>
</body>
</html>
Output:-
                                                                page12
   12.Write a PHP script to print if a given number is palindrome or not.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <h1> PALINDROME </h1>
<?php
       $input= "GHOST";
       echo "<br> Input string" . $input;
       $reverse = strrev($input);
       echo "<br> Output string" . $reverse;
       if($input == $reverse)
       {
               echo "<br>". $input." Is Palindrome";
       }
       else
       {
               echo "<br>". $input." Is not Palindrome";
       }
?>
</body>
</html>
Output-
                                                                      page13
           13.Write a PHP script to print the first 20 odd numbers.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<?php
       echo"first 20 odd no:";
       echo"<br>";
       for($i=1;$i<40;$i=$i+2)
       {
              echo"$i";
              echo"<br>";
       }
       echo"<br>";
?>
</body>
</html>
Output:-
                                                                  page14
           14.Write a PHP script to print a table of a given number.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
 <?php
       $num=3;
       for($i=1;$i<=10;$i++)
       {
              $table=$i*$num;
              echo"$i*$num=$table<br>";
       }
?>
</body>
</html>
Output:-
                                                                  page15
15.Write a PHP script to show the working of switch statements.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<?php
      $d=date("D");
      switch($d)
      {
             case"Mon":
                    echo "Today is Monday";
                    break;
             case"Tue":
                    echo "Today is Tuesday";
                     break;
             case"Wed":
                     echo "Today is Wednesday";
                     break;
             case"Thu":
                     echo "Today is Thursday";
                     break;
             case"Fri":
                     echo "Today is Friday";
                     break;
             case"Sat":
                     echo "Today is Saturday";
                     Break;
             case"Sun":
                     echo "Today is Sunday";
                     break;
             default:
                     echo"Wonder which day is this!!";
      }
?>
</body>
</html>
Output:-
                                                                 page16
<body>
       <h1>NESTED IF </h1>
<?php
       $purchase_amount=2000;
       $payment_mode="credit card";
       if($purchase_amount>5000)
       {
               if($payment_mode=="credit card")
               {
                      $discount=$purchase_amount*0.15;
               }
               else
               {
                      $discount=$purchase_amount*0.10;
               }
       }
       else
       {
               if($payment_mode=="credit card")
               {
                      $discount=$purchase_amount*0.10;
               }
               else
               {
                      $discount=$purchase_amount*0.05;
               }
       }
       $final_bill=$purchase_amount-$discount;
       echo"<b>Purchase amount:</b> $purchase_amount<br>";
       echo"<b>Payment Mode:</b> $payment_mode<br>";
       echo"<b>Discount:</b> $discount<br>";
       echo"<b>Bill Payment:</b> $final_bill";
? >
</body>
</html>
          page17
output-
                                                                 page18
17.Write a PHP script to print swapping of two numbers using call by
value
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <h1> SWAPPING CALL BY VALUE </h1>
<?php
       $a=10;
       $b=20;
       function swap($a,$b)
       {
              $temp = $a;
              $a = $b;
              $b = $temp;
              echo"in swap() function,the value of variable a=$a<br>";
              echo"in swap() function,the value of variable b=$b<br>";
       }
       swap($a,$b);
       echo"outside swap()function,the value of a = $a<br>";
       echo"outside swap()function,the value of b = $b<br>";
?>
</body>
</html>
Output:-
                                                                          page19
   18.Write a PHP script to print swapping of two numbers using call by
                                 reference.
Output:-
                                                            page20
 19.Write a PHP script to show the factorial of a number using recursion.
<body>
       <h1> factorial using recursion </h1>
 <?php
       $num=5;
       echo"factorial of $num is",fact($num);
       function fact($num)
       {
              if($num<=0)
              {
                      return 1;
              }
              else
              {
                      return $num*fact($num-1);
              }
       }
?>
</body>
</html>
output:-
                                                                 page21
    20.Write a PHP script to show the working of dynamic function call.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <h1> DYNAMIC FUNCTION CALL </h1>
<?php
       function test()
       {
              echo"Dynamic function call executes.";
       }
       $my_func="test";
       $my_func();
?>
</body>
</html>
OUTPUT-
                                                                    page22
21.Write a PHP script to show working of local, global and static variables.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
      <h1> LOCAL VARIABLE </h1>
<?php
      function test_var()
      {
              $var=10;
              echo $var;
      }
      test_var();
?>
</body>
</html>
Output-
                                                                   page23
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <H1> GLOBAL VARIABLE </H1>
<?php
       $var1=5;
       $var2=10;
       function test_var()
       {
               global $var1,$var2;
               echo $var1+$var2;
       }
       test_var();
?>
</body>
</html>
Output-
                                                                 page24
<body>
       <H1> STACTIC VARIABLE </H1>
<?php
       function test()
       {
               $a=0;
               echo $a;
               echo "<br>";
               $a++;
       }
       test();
       test();
       test();
?>
</body>
</html>
Output-
                                                                 page25
22. Write a PHP script to show the working of all operators.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
      <h1> OPERATORS </h1>
<?php
      $x = 10;
      $y = 4;
      echo ($x + $y)."<br>";
      echo ($x * $y)."<br>";
      echo ($x - $y)."<br>";
      echo ($x / $y)."<br>";
      echo ($x % $y)."<br>";
?>
</body>
</html>
Output-
                                                                 page26
23. Write a PHP script to print the working of a transpose matrix.
<body>
       <h1> TRANSPOSE OF MATRIX</h1>
<?php
       $arr=array(
       array("1","2"),
       array("3","4"));
       for($i=0;$i<2;$i++)
       {
              for($j=0;$j<2;$j++)
              {
                       $a[$i][$j]=$arr[$j][$i];
              }
       }
       for($i=0;$i<2;$i++)
       {
              for($j=0;$j<2;$j++)
              {
                       echo $a[$i][$j]." ";
              }
              echo"<br>";
       }
?>
</body>
</html>
Output-
                                                                     page27
24. Write a PHP script to show the working of iterative functions.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <h1> ITERATIVE FUNCTION </h1>
<?php
       $colors = array("red","green","blue","cyan");
       echo current($colors);
?>
</body>
</html>
output-
                                                                     page28
25. Write a PHP script to show the working of sorting function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <h1> SORTING OF ARRAY </h1>
<?php
       $colors=array("red","blue","green","black","yellow");
       echo "the elements in array are:<br>";
       foreach($colors as $value)
       {
              echo"$value<br>";
       }
       sort($colors);
       echo"<br> the elements of array after sorting in ascending order;<br>";
       foreach($colors as $value)
       {
              echo"$value<br>";
       }
?>
</body>
</html>
Output-
                                                                                 page29
26. Write a PHP script to show the working of array functions.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <h1>ARRAY FUNCTIONS</h1>
<?php
       $colors=array("red","blue","green","black","yellow");
       echo"<pre>";
       print_r(array_chunk($colors,2));
       echo"</pre>";
?>
</body>
</html>
Output-
                                                               page30
27. Write a PHP script to show working of default arguments.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <h1> DEFAULT ARGUMENTS </h1>
<?php
       function area_circle($radius=5)
       {
              $area = 3.14159*$radius*$radius;
              echo"Area of circle with radius $radius is $area<br>";
       }
       area_circle(10);
       area_circle();
       area_circle(7);
?>
</body>
</html>
output-
                                                                       page31
28. Write a PHP script to show the working of string functions.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
        <h1> string function replace</h1>
<?php
        $str="hello! welcome to PHP Programming.";
        $new_str=str_replace("hello","hi",$str);
        echo "before replacement :$str<br>";
        echo "after replacement :$new_str";
?>
</body>
</html>
                                                                   page32
29. Write a PHP script to show the working of string formatting.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
       <h1> Program to show the Working of String Formatting. </h1>
<?php
       $number = 100;
       printf("Binary: %b<br>", $number);
       printf("ASCII: %c<br>", $number);
       printf("decimal: %d<br>", $number);
       $txt= sprintf("%f", $number);
       echo $txt;
?>
</body>
</html>
</body>
</html>
                                                                      page33
30. Write a PHP script to show use of super global variable ($get method)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
        <h1>$_POST Example</h1>
        <form method="post" action="super_post.php">
               Enter your name:
               <input type="text"name="name" /><br><br>
               Enter your Email:
               <input type="text" name="email"><br><br><br>
               <input type="submit"value="Submit" >
        </form>
</body>
</html>
PHP
</head>
<body>
        <h1>$_post Example</h1>
<?php>
        echo "Welcome";
        echo $_POST["name"];
        echo "<br>";
        echo "YOUR EMAIL ADDRESS IS :";
        echo $_POST["email"];
?>
</body>
</html>
Output
                                                                  page34
31. Write a PHP script to show use of super global variable($post method).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
</head>
<body>
       <h1>$_GET Example</h1>
       <form method="get"action="super_get.php">
             Enter your name:
             <input type="text"name="name" ><br><br />
             Enter your Email:
             <input type="text" namew="email" /><br /><br /><br />
             <input type="submit"value="submit" />
       </form>
</body>
</html>
PHP
        <h1>CREATING DATABASE</h1>
<?php
        $CONNECTION=mysqli_connect("localhost","root","","my_mansi03");
        if(!$CONNECTION)
        {
                die("Couldn't connect to server");
        }
        echo "Connected Successful<br><br>";
        //Create Table under my_db database
        $sql_query="create table employee(
        emp_id int primary key,
        emp_name varchar(20),
        emp_dept varchar(20),
        emp_city varchar(20))";
        mysqli_query($CONNECTION,$sql_query);
        echo "Table created successfully";
        mysqli_close($CONNECTION);
?>
</body>
</html>
page35
output:
                                                                             page36
33. Write a PHP script to delete the cookies.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>cookie</title>
<?php
        setcookie("message","php programming",time()-3600);
 ?>
</head>
<body>
        cookie deleted.
        check it at
        <a href="cookie_read.php">cookie_read.php</a>
</body>
</html>
Output:
                                                                             page37
34. Program to modify the cookies.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>cookie</title>
<?php
        $cookie_name="message";
        $cookie_value="hello world";
        setcookie($cookie_name,$cookie_value,time()+60*60*24*30);
?>
</head>
<body>
        <h1>modify cookie</h1>
<?php
        if(isset($_COOKIE[$cookie_name]))
        {
                 echo "Cookie",$cookie_name,"is set.<br>";
                 echo"Cookie value is $_COOKIE[$cookie_name]";
        }
        else
        {
                 echo"Cookie",$cookie_name,"is not set.<br>";
        }
?>
</body>
</html>
Output.
                                                                             page38
35. Program to register Session Variable
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
        session_start();
?>
<html>
<head>
       <title>session variables</title>
</head>
<body>
       <h1>register session variables</h1>
<?php
       $_SESSION["title"]="web application development using php";
       $_SESSION["author"]="tarsem singh";
       echo"session variable are set";
?>
</body>
</html>
Output:
                                                                     page39
36. Program of Working With Session Variable
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
        session_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title> Accessing session</title>
</head>
<body>
        <h1> Accessing Session Variable</h1>
<?php
       echo"Title:",$_SESSION["Title"],"<br>";
        echo"Author:",$_SESSION["Author"];
?>
</body>
</html>
Output:
                                                                              page40
37. Program of Encoding Session Variable.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<?php
session_start();
?>
</head>
<body>
        <h1>ENCODING SESSION VARIABLE</h1>
<?php
        $_SESSION["title"]="Web Application Development using PHP";
        $_SESSION["Author"]="Tarsem Singh";
        $session_encd=session_encode();
        echo"<br><b>Session Encoded Data :<br><br></b>";
        echo $session_encd;
?>
</body>
</html>
Output:
                                                                             page41
38. Program of Decoding Session Variable.
                                                                              page42
39. Program to delete the session variable
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
        session_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Session</title>
</head>
<body>
        <h1> DELETE SESSION VARIABLE</h1>
<?php
        if(isset($_SESSION["Title"]))
         {
             unset($_SESSION["Title"]);
         }
         echo"Variable deleted";
?>
</body>
</html>
Output:
                                                                              page43
40. Write a PHP script of file handling
<body>
<?php
$filename="notepad.txt";
if(file_exists($filename))
{
echo"<b> the file $filename exists </b><br />";
$file_handle=fopen("notepad.txt","r");
while(!feof($file_handle))
{
echo fgets($file_handle);
}
}
else
{
echo"the file $filename doesnot exist";
}
?>
<?php
$file=fopen("notepad.txt","w");
echo fwrite($file,"welcome to php programming");
?>
<?php
$file_handle=fopen("notepad.txt","a");
$text="here is more text append in notepad.txt";
if(fwrite($file_handle,$text)==false)
{
echo"cannot append the data";
}
else
                                                                             page44
{
echo"<br>data append</br>";
}
?>
<?php
$file="notepad.txt";
$copy="new.txt";
if(copy($file,$copy))
{
echo"copied ";
}
else
{
echo"couldnot copy $file";
}
fclose($file_handle);
?>
</body>
</html>
output: