Saturday 13 February 2016

Function Overriding

Code:

<?php
class Ice {
   function myFun() {
      return "Ice";
   }
}

class Cream extends Ice {
   function myFun() {
      return "Cream";
   }
}

$ice = new Ice;
$cream = new Cream;
echo "<center><h1>Function Overriding</h1>";
echo($ice->myFun());
echo($cream->myFun());
echo "</center>";
?>



Output:

Friday 12 February 2016

Content of student table to HTML

Code:

<?php
echo "<center><h1>Content of student table to HTML</h1>";
$con = mysqli_connect("localhost","root","","studentdb");
$que ="SELECT * FROM student";
$res=mysqli_query($con,$que);
      echo "<table border=1><tr><th>Name of student</th><th>Department of student</th><th>Year of student</th></tr>";
while($disp=mysqli_fetch_assoc($res))
  {
      echo  "<tr><td>".$disp['name']."</td>";
      echo "<td>".$disp['department']."</td>";
      echo "<td>".$disp['year']."</td>";   
      echo "</td></tr>";
  }
  echo "</table></center>";
?>



Output:


Linear search in array

Code:

<!DOCTYPE html>
<html>
<body>
<center>
<h1>Linear search in array</h1>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (isset($_POST['find']))
{
    $val=$_POST['srch'];
    if (in_array($val, $people))
      {
          echo "Match found";
      }
    else
      {
          echo "Match not found";
      }
}
?>
<form method="post" action="index.php">
    Search in array: <input type="text" name="srch">
    <input type="submit" value="Search" name="find">
</form>
</center>
</body>
</html>



Output:


Dropdown list with data from MySQL table

Code:

<!DOCTYPE html>
<html>
<body>
<center><h1>Dropdown list with data from MySQL table</h1>
    <select name="district">
      <option value="" hidden>District</option>
      <?php
        $conn=mysqli_connect('localhost','root','','bloodlist');
        $query="SELECT * FROM districts ORDER BY dis_name ASC";
        $result=mysqli_query($conn,$query);
        while ($distr=mysqli_fetch_assoc($result))
        {
          echo "<option value=".$distr['dis_id'].">".$distr['dis_name']."</option>";
        }
      ?>
    </select>
</center>
</body>
</html>



Output:

Login form with session

Code: 

index.php

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<center><h1>Login</h1>
<?php
if(isset($_POST['submit']))
{
    $conn=mysqli_connect("localhost","root","","login");
    $user=$_POST['username'];
    $pass=$_POST['password'];
    $qu="SELECT * FROM registerform WHERE username='{$user}' AND password='{$pass}'";
    $result=mysqli_query($conn,$qu);
    $count=mysqli_num_rows($result);
        if($count == 1)
        {
            $_SESSION['username']=$user;
            echo "<script>document.location.href='display.php'</script>";
        }
        else
        {
            echo "<script>alert('Invalid Username or Password')</script>";
        }
}
?>
<form name="signin" method="post" action="index.php">
Username: <input type="text" name="username"/>
Password: <input type="password" name="password"/>
<input type="submit" value="Login" name="submit"/>
</center>
</body>
</html>




display.php

<?php session_start(); ?>
<?php
    if(isset($_SESSION['username'])){
   
    }else{
   
        echo "<script>alert('Please Login')</script>";
        echo "<script>document.location.href='index.php'</script>";
    }
?>
<!DOCTYPE html>
<html>
<body>
<center>
<?php
    echo "<h1>Welecome to Home<br/></h1>";
    echo $_SESSION['username']." successfully logged in";   
?>
<center><br/>
<a href="logout.php">Logout</a>
</center>
</body>


Logout.php

<?php session_start(); ?>
<?php
        session_destroy();
        echo "<script>document.location.href='index.php'</script>";       
?>

Output:

Thursday 11 February 2016

Read and Display XML

Code:

a.xml 

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to> Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

index.php

<!DOCTYPE html>
<html>
<body>
<center>
<h1>Read and Display XML</h1>
<?php
    $xml=simplexml_load_file('a.xml');
    print_r($xml);
?>
</center>
</body>
</html>


Output:

 

Five String Functions in PHP

Code:

<!DOCTYPE html>
<html>
<body>
<center>
<h1>Five String Functions in PHP</h1>
<?php
    $name="Lionel Messi";
    echo "<h4>";
    echo "String : ".$name;
    echo "<br/>Length of the string : ".strlen($name);
    echo "<br/>Number of words in the string : ".str_word_count($name);
    echo "<br/>Reverse of the string : ".strrev($name);
    echo "<br/>Character position of Messi : ".strpos($name,"Messi");
    echo "<br/>Replace string : ".str_replace("Lionel", "Leo", $name);
    echo "</h4>";
?>
</center>
</body>
</html>


Output:

 

Largest in an array

Code:

<!DOCTYPE html>
<html>
<body>
<center>
<h1>Largest in an array</h1>
<?php
    $numbers = array(10,23,54,32,51,61,43,52,90,49);
    print_r($numbers);
    echo "<h3>Largest number is ";
    echo max($numbers);
    echo "</h3>";
?>
</center>
</body>
</html>


Output:

 

Elements in an array using foreach loop

Code:

<!DOCTYPE html>
<html>
<body>
<center>
<h1>Elements in an array using foreach loop</h1>
<?php
$players = array("Messi", "Neymar", "Ronaldo", "Oscar","Di Maria");
print_r($players);
foreach ($players as $value) {
    echo "<h4>$value <br/></h4>";
}
?>
</center>
</body>
</html>



Output:

 

Prime Number or Not

Code:

<!DOCTYPE html>
<html>
<body>
<center><h1>PRIME NUMBER OR NOT</h1>
<?php
if (isset($_POST['check']))
{
    $k=$_POST['num'];
    $flag=0;
    $x=0;
    for($i=2;$i<=$k;$i++)
    {
        for($j=2;$j<$i;$j++)
        {
            if($i%$j==0)
            {
                if ($i==$k) {
                    echo "$k is not a prime number";
                }
                $flag=1;
                break;
            }
        }
        if($flag==0)
            {
                if ($k==$i) {
                    echo "$k is a prime number";
                    break;
                }
            }
        $flag=0;
    }
}
?>
<form method="post" action="index.php">
    Enter Number: <input type="text" name="num">
    <input type="submit" name="check" value="Check">
</form>
</center>
</body>
</html>


Output:

 

Concatenate two Strings

Code:

<!DOCTYPE html>
<html>
<body>
<center><h1>CONCATENATE TWO STRINGS</h1>
<?php
if (isset($_POST['add']))
{
    $w1=$_POST['word1'];
    $w2=$_POST['word2'];
    $w3=$_POST['word3'];
    $w4=" is a ".$w3." year old person";
    $w5=$w1." ".$w2.$w4;
    echo "<h3>$w5</h3>";
}
?>
<form method="post" action="index.php">
    Firstname: <input type="text" name="word1">
    Lastname: <input type="text" name="word2">
    Age: <input type="text" name="word3">
    <input type="submit" value="Concatenate" name="add">
</form>
</center>
</body>
</html>


Output:

 

First N Fibonacci Numbers

Code:

<!DOCTYPE html>
<html>
<body>
<center><h1>First N Fibonacci Numbers</h1>
<?php
     $fib1=0;
     $fib2=1;
     $count=0;
if (isset($_POST['find']))
{
    $num=$_POST['range'];
    echo "$fib1 <br/>";
    echo "$fib2 <br/>";
    $count=2;
    while($count<$num)
    {
        $fib3=$fib1+$fib2;
        $count++;
        echo "$fib3 <br/>";
        $fib1=$fib2;
        $fib2=$fib3;
    }
}
?>
<form method="post" action="index.php">
    Enter limit: <input type="text" name="range">
    <input type="submit" value="Find" name="find">
</form>
</center>
</body>
</html>


Output:

 

First N Strange Numbers

 Code:

<!DOCTYPE html>
<html>
<body>
<center><h1>FIRST N STRANGE NUMBERS</h1>
<?php
    $count=0;
if (isset($_POST['find']))
{
    $a=$_POST['range'];
   
    $b=$a;

    for ($i=153;$i<=$i*$i;$i++)
    {
        $e=(string)$i;
        $d=strlen($e);

        $st=0;
        $reverse=0;
        $b=$i;
        while($b!=0)
        {
            $reverse=$reverse*10;
            $temp=$b%10;
            $c=$temp;
            $st=$st+pow($c,$d);
            $reverse=$reverse+$temp;
            $b=(int)$b/10;
        }
        if ($i==$st)
            {
                echo "$st <br/>";
                $count++;
            }
        if ($count==$a)
            {
                break;
            }
    }
}
?>
<form method="post" action="index.php">
    Enter limit: <input type="text" name="range">
    <input type="submit" value="Find" name="find">
</form>
</center>
</body>
</html>


Output:

 

First N Prime Numbers

Code:

<!DOCTYPE html>
<html>
<body>
<center>
<h1>First N Prime Numbers</h1>
<?php
if (isset($_POST['check']))
{
$status=1;
$num=3;
$n=$_POST['limit'];

if ($n>=1)
{
echo "2<br/>";
}
for ($i = 2; $i <= $n;)
{
for ($j = 2; $j <= sqrt($num) ; $j++)
{
if ($num%$j==0)
{
$status=0;
break;
}
}
if ($status!=0)
{
echo $num."<br/>";
$i++;
}
$status=1;
$num++;
}
}
?>
<form method="post" action="index.php">
    Enter limit: <input type="text" name="limit">
    <input type="submit" name="check" value="Check">  
</form>
</center>
</body>
</html>



Output:

 

Even Numbers in a Range

Code:

<!DOCTYPE html>
<html>
<body>
<center>
    <h1>EVEN NUMBERS IN RANGE</h1>
<?php
if(isset($_POST['find']))
{
$a=$_POST['lower'];
$b=$_POST['upper'];
for($a;$a<=$b;$a++)
    {
    if($a%2==0){
        echo "$a<br/>";
        }
    }
}
?>
<form method="post" action="index.php" name="evn">
    Lower limit: <input type="text" name="lower">
    Upper limit: <input type="text" name="upper">
    <input type="submit" name="find" value="Find">
</form>
</center>
</body>
</html>

Output:

Array Sorting in Ascending Order

Code:

<!DOCTYPE html>
<html>
<body>
<center>
<h1>ARRAY SORTING</h1>
<?php
$num = array(99, 37, 26, 62, 6, 50, 44);
print_r($num);
echo "<br/>";
sort($num);

$arrlength = count($num);
for($i = 0; $i <  $arrlength; $i++)
{
     echo "<br/>";
     echo $num[$i];
}
?>
</center>
</body>
</html>


Output:

 

Check Strange Number or Not

Code:

<!DOCTYPE html>
<html>
<body>
<center>
<h1>STRANGE NUMBER OR NOT</h1>
<?php
if(isset($_POST['check']))
{
$num=$_POST['num'];
$x=$num;
$sum=0;
$e=(string)$num;
$d=strlen($e);
while($x!=0)
{
$a=$x%10;
$sum=$sum+pow($a,$d);
$x=$x/10;
}
if($num==$sum)
{
echo "$num is a strange number";
}
else
{
echo "$num is not a strange number";
}
}

?>
<form method="post" action="index.php">
    Enter limit: <input type="text" name="num">
    <input type="submit" value="Find" name="check">
</form>
</center>
</body>
</html>


Output:


Content of XML file to Drop-Down

Code:

clg.xml

<?xml version="1.0" encoding="UTF-8"?>
<college>
    <dep>
        <id>001</id>
        <name>B.Voc</name>
    </dep>
    <dep>
        <id>002</id>
        <name>CS</name>
    </dep>
    <dep>
        <id>003</id>
        <name>Physics</name>
    </dep>
</college>

index.php

<!DOCTYPE html>
<html>
<body>
<center>
<h1>Content of XML file to Drop-Down</h1>
<?php
    $abc=simplexml_load_file('clg.xml');
    echo "<select name='dep'>";
    foreach ($abc->Children() as $d)
    {
        echo "<option value='".$d->id."'>".$d->name."</option>";
    }
    echo "</select>";
?>
</center>
</body>
</html>


Output:

 

 

Perfect, Abundant or Deficient

Code:

<!DOCTYPE html>
<html>
<body>
<center>
<h1>Perfect, Abundant or Deficient</h1>
<?php
  if (isset($_POST['check']))
  {
    $sum=0;
    $dsum=0;
    $a=$_POST['num'];
    for($i=1;$i<=$a;$i++)
    {
      if($a%$i==0)
      {
        $sum=$sum+$i;
      }
    }
    $dsum=2*$a;
    if($sum<$dsum)
    {
       echo "it is a deficient number";
    }
    else if($sum==$dsum)
    {
       echo "it is a perfect number"; 
    }
    else
    {
        echo "it is an abundant number";
    }
  }
?>
<form method="post" action="index.php">
  Enter a number: <input type="text" name="num">
    <input type="submit" name="check" value="check">
</form>
</center>
</body>
</html>


Output:

 

Prime Numbers in a Range

Code:

<!DOCTYPE html>
<html>
<body>
<center><h1>PRIME NUMBERS IN RANGE</h1>
<?php
if (isset($_POST['find']))
{
    $i=$_POST['lower'];
    $k=$_POST['upper'];
    $flag=0;
    for($i;$i<$k;$i++)
    {
        for($j=2;$j<$i;$j++)
        {
            if($i%$j==0)
            {
                $flag=1;
            }
        }
        if($flag==0)
            {
                echo "$i <br>";
            }
        $flag=0;
    }
}
?>
<form method="post" action="index.php">
    Lower limit: <input type="text" name="lower">
    Upper limit: <input type="text" name="upper">
    <input type="submit" name="find" value="Find">
</form>
</center>
</body>
</html>


Output:

 

Switch Control Statement

Code:

<!DOCTYPE html>
<html>
<body>
<center>
<h1>Switch Control Statement</h1>
<?php
$color = "white";

switch ($color) {
    case "white":
        echo "Your favorite color is white!";
        break;
    case "blue":
        echo "Your favorite color is blue!";
        break;
    case "green":
        echo "Your favorite color is green!";
        break;
    default:
        echo "Your favorite color is neither white, blue, nor green!";
}
?>
</center>
</body>
</html>


Output:

 

Fibonacci Numbers in a Range

Code:

<!DOCTYPE html>
<html>
<body>
<center><h1>Fibonacci Numbers in your Range</h1>
<?php
if (isset($_POST['find']))
{
    $fib1=0;
    $fib2=1;
    $fib3=0;
    $lower=$_POST['lower'];
    $upper=$_POST['upper'];
    for($i=$lower;$i<$upper;$i++)
    {
        if ($fib3<=$upper)
            {
                if ($fib3>=$lower)
                    {
                        echo "$fib3 <br/>";
                    }
                $fib1=$fib2;
                $fib2=$fib3;
                $fib3=$fib1+$fib2;
            }
    }
}
?>
<form method="post" action="index.php">
    Enter lower limit: <input type="text" name="lower">
    Enter upper limit: <input type="text" name="upper">
    <input type="submit" value="Find" name="find">
</form>
</center>
</body>
</html>


Output:

 

Monday 8 February 2016

Constructor and Destructor in PHP

Code:

<?php
echo "<center><h1>Constructor and Destructor</h1>";
class book
{
    function __construct($p,$t)
    {   
        $this->price = $p;
        $this->title = $t;
        echo "title: $t <br/>";
        echo "price: $p <br/>";
        echo "Constructor working!<br/><br/>";
    }
   
    function __destruct()
    {
        echo "object Destroyed</center>";
    }
}
$book=new book("450","optics");
?>



Output:


Root of quadratic equation by accepting coefficients

Code:

<!DOCTYPE html>
<html>
<body>
<center>
<h1>Root of a Quadratic equation by accepting coefficients</h1>
<?php
    if (isset($_POST['find']))
    {
        $a=$_POST['a'];
        $b=$_POST['b'];
        $c=$_POST['c'];
        $temp=sqrt($b*$b-4*$a*$c);
        $root1=(-$b+$temp)/(2*$a);
        $root2=(-$b-$temp)/(2*$a);
        echo "<h4>Roots of the Quadratic Equation are: ".$root1." and ".$root2."</h4>";
    }
?>
<form method="post" action="index.php">
    Enter value of a: <input type="number" name="a">
    Enter value of b: <input type="number" name="b">
    Enter value of c: <input type="number" name="c">
    <input type="submit" value="Calculate" name="find">
</form>
</center>
</body>
</html>


Output:

 


Inheritance in PHP

Code:

<?php
echo "<center><h1>Inheritance in PHP</h1>";
class Person
{
    public $name="Dominic Toretto";
}
class human extends Person
{
    public function Greet()
    {
        return "My name is " . $this->name;
    }
}
$person = new human();
echo $person->Greet();
echo "</center>";
?>



Output:


Class and Object in PHP

Code:

<?php
echo "<center>";
echo "<h1>Class and Object</h1>";
class human
{
    public $gender='Male';
}
    $Johnny = new human;
    echo 'Johnny is a '. $Johnny->gender.'.';
echo "</center>";
?>



Output:


Post value from a form to another page in PHP

Code:

index.php

<!DOCTYPE HTML>
<html>
<body>
<center>
<h1>Page to post</h1>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</center>
</body>
</html>



welcome.php 

<!DOCTYPE html>
<html>
<body>
<center>
<h1>Posted page</h1>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</center>
</body>
</html>

Output:


Login and Logout

Code:

index.php

<!DOCTYPE html>
<html>
<body>
<center><h1>Login Form</h1>
<?php
if(isset($_POST['submit']))
{
    $user=$_POST['username'];
    $pass=$_POST['password'];
   
    if($user=="Dominic" && $pass=="toretto")
    {
        echo "Now you are logged in <br/>";
        echo "<a href='logout.php'>logout</a>";   
    }
    else
    {
        echo "Invalid Username or Password";
    }
}
?>
<form method="post" action="index.php">
Username: <input type="text" name="username"/>
Pasword: <input type="password" name="password"/>
<input type="submit" value="Login" name="submit"/>

</form>
</center>
</body>
</html>


logout.php

 <!DOCTYPE html>
<html>
<body>
<center>
    <h1>Successfully logged out</h1>
</center>
</body>
</html>


Output:


The use of cookie in PHP

Code:

<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "Lionel Messi";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<center>
<h1>Cookie</h1>
<?php
if(!isset($_COOKIE[$cookie_name])) {
      echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
      echo "Cookie '" . $cookie_name . "' is set!<br>";
      echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

<p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>
</center>
</body>
</html>


Output: