Showing posts with label php questions. Show all posts
Showing posts with label php questions. Show all posts

Wednesday, 30 March 2016

Store student details into XML file

Code:

<?php
$xml=new DomDocument("1.0","UTF-8");
$student=$xml->createElement("student");
$student=$xml->appendChild($student);
$dept=$xml->createElement("dept");
$dept=$student->appendChild($dept);
$ID=$xml->createElement("ID","001");
$ID=$dept->appendChild($ID);
$name=$xml->createElement("Name","Dominic Toretto");
$name=$dept->appendChild($name); 
$xml->FormatOutput=true;
$string_value=$xml->saveXML();
$xml->save("MyXml.xml");
?>
<meta http-equiv="refresh" content="1,url=MyXml.xml">

Output:

 

Sunday, 27 March 2016

PHP Questions

Some question of PHP programs are given below as  links. Click on that link which one you want to know more. The given links will display the program code and its output in a PDF. If you want to check the program code, just copy and paste into a text editor and then run it.

Questions are given below. Click on that to get answers, 
  1. Write a PHP program to print even numbers in a range?
  2. Write a PHP program to print prime numbers in a range?
  3. Write a PHP program to print Fibonacci numbers in a range?
  4. Write a PHP program to print n prime numbers?
  5. Write a PHP program to print n strange numbers?
  6. Write a PHP program to print n Fibonacci number?
  7. Write a PHP program to demonstrate switch control statement?
  8. Write a PHP program to concatenate two strings?
  9. Write a PHP program to display elements in an array in ascending order?
  10. Create a program to display whether a given number is a perfect, abundant or deficient? 
  11. Write a program to check a given number is prime or not? 
  12. Write a PHP program to display elements in an array using foreach loop?
  13. Write a PHP program to find largest in an array?
  14. Write a PHP program to demonstrate five string functions?
  15. Write a PHP program to read and display the contents of XML file?
  16. Create a program to populate content of XML file to dropdown list?
  17. Write a program to check a given number is strange or not? 
  18. Write a PHP program to demonstrate use of Cookie?
  19. Create a loginform and logout page?
  20. Create an HTML form and post its value to another page in PHP?
  21. Write a PHP program to demonstrate Class and Object? 
  22. Write a PHP program to demonstrate Inheritance?
  23. Write a PHP program to display root of quadratic equation by accepting coefficients?
  24. Write a PHP program to demonstrate Constructor and Destructor?
  25. Write a PHP program to create login form with session? 
  26. Write a PHP program to demonstrate function overriding? 
  27. Write a PHP program to populate dropdown list with data from MySQL table? 
  28. Write a PHP program to search an element in array using linear search? 
  29. Write a php program to populate content of student table to HTML? 
  30. Write a program to create XML file and store student details?

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: