PHP Program Tutorial

Please leave a remark at the bottom of each page with your useful suggestion.


Table of Contents

  1. PHP Introduction
  2. PHP Program Collection 1
  3. PHP Program Collection 2
  4. PHP Program Collection 3

PHP Program Collection 2


  1. Program to find the sum of elements in an array.
  2. Program to split a string into an array elements based on delimiter
  3. Program to find the product of elements in an array.
  4. Program to combine the array elements into a string with given delimiter.
  5. Separate odd and even elements from array without using loop
  6. Program to create simple Login and Logout example using sessions.
  7. Program to Upload a file to the Server.
  8. Program to create a New Database using PHP and Mysql
  9. Program to connect to the server and selecting database
  10. Program to Insert records into the table in Database.
  11. Program to fetch records from the table in Database.
  12. Program to Store a image in Database
  13. Program to Read image from Database.
  14. Contact form using PHP
  15. Write a PHP script which will display the colors in the following way

  16. 16. Program to find the sum of elements in an array.
    <?php
     
          $arr = array(10,20,30,'a'); 
           echo array_sum($arr); 
    
     ?>
    
    17. Program to split a string into an array elements based on delimiter
    <?php 
    
    $str = "Welcome";
    $arr = explode('c',$str);          print_r($arr); 
    
    ?>
    
    18. Program to find the product of elements in an array.
    <?php
    
      $arr = array(10,20,30);
      echo array_product($arr); 
    
    ?>
    
    19. Program to combine the array elements into a string with given delimiter.
    <?php  
    
           $arr = array(9,1,2013);
           $str = implode('/',$arr);  
           echo $str;  
    
     ?>
    
    20. Separate odd and even elements from array without using loop
    <?php
    
    $input = array(4, 3, 6, 5, 8, 7, 2);
    
    function oddCmp($input)
    
    {
    
        return ($input & 1);
    
    }
    
    
    function evenCmp($input)
    
    {
    
        return !($input & 1);
    
    }
    
    $odd = array_filter($input, "oddCmp");
    
    $even = array_filter($input, "evenCmp");
    
    $odd = array_values(array_filter($odd));
    
    $even = array_values(array_filter($even));
    
    print"Odd array :\n";
    
    print_r($odd);
    
    print"\nEven array :\n";
    
    print_r($even);
    
     
    ?>
    
    21. Program to create simple Login and Logout example using sessions.
    *** login.php
    
     <html> 
     <head>
     <title>Login Form</title> 
     </head> 
     <body>
     <h2>Login Form</h2>
     <form method="post"   action="checklogin.php"> 
    User Id: <input type="text" name="uid">
    <br> 
    Password: <input type="password" name="pw">
    <br> 
    <input type="submit" value="Login"> 
    </form> 
    </body> 
    </html>
    
    *** checklogin.php
     <?php 
     $uid = $_POST['uid'];
     $pw = $_POST['pw']; 
    
    if($uid == 'arun' and $pw == 'arun123') 
    {  
    session_start(); 
    $_SESSION['sid']=session_id();              
    header("location:securepage.php");
     } 
    
    ?> 
    
    *** securepage.php
    
     <?php 
    session_start(); 
    
     if($_SESSION['sid']==session_id())  
    {   
           echo "Welcome to you<br>";  
         echo "<a href='logout.php'>Logout
           <a>"; 
    }  
    else 
    {   
    header("location:login.php"); 
    } 
    ?> 
    
    *** logout.php
    
    <?php  
    echo "Logged out scuccessfully"; session_start();  
    session_destroy();  setcookie(PHPSESSID,session_id(),time()-1); 
    ?>
    
    22. Program to Upload a file to the Server.
    *** form1.php
    
     <html>
     <head>
     </head> 
     <body> 
    <form method="post"   
    action="upload.php" enctype="multipart/form-data">
     Resume : <input type="file" name="f1"> <br>
    <input type="submit" value="Upload">
    </form> 
    </body> 
    </html>
    
    *** Upload.php
    
    <?php 
    
    if(is_uploaded_file($_FILES['f1']['tmp_name'])) 
    
    {  
    $fname = $_FILES['f1']['name'];            
    
    if(move_uploaded_file($_FILES['f1']['tmp_name'],"uploads/$fname"))
    
     {   
    echo "Uploaded successfully";
     } 
    else
     {  
    echo "Not uploaded";
     }
     } 
    ?>
    
    23. Program to create a New Database using PHP and Mysql
    <?php
     if(! mysql_connect('servername','username','password'))
     { 
        die('Connection failed!'.mysql_error()); 
     }
    $sql = "create database newdb"; if(mysql_query($sql))
    
    { 
     echo "Database created"; 
    }
    else
    { 
      echo mysql_error(); 
    } 
    ?> 
    
    24. Program to connect to the server and selecting database
    <?php 
    if(!mysql_connect('servername','username','password'))
     {
         die('Connection failed!'.mysql_error());
     }
          if(!mysql_select_db("dbname"))
     {  
    die('Database unavailable'.mysql_error()); 
     }
     ?> 
    
    25. Program to Insert records into the table in Database.
    <?php
    
     $conn =mysqli_connect('servername','username','password','databasename');
    
     if(!$conn) 
    {
     die('Connection failed!'.mysqli_error($conn));
     } 
    $sql = "INSERT INTO tablename('sno','name','pwd') VALUES('101','Surya','surya123')";
      
     if(mysqli_query($conn,$sql)) 
    {  
    echo "Record Inserted"; 
     } 
    else 
    {  
    echo mysql_error();
    } 
    ?> 
    ------------------------------------------------------------- 
    
    
    26. Program to fetch records from the table in Database.
    <?php 
    
    $conn = mysqli_connect('servername','username','password','databasename');
     
    if(!$conn) 
    {  
    die('Connection failed!'.mysqli_error($conn)); 
    } 
    $sql = "SELECT * FROM tablename";   
    $data = mysqli_query($conn,$sql);   
    while($rec = mysqli_fetch_row($data)) 
    { 
    echo "$rec[0]<br>"; 
    echo "$rec[1]<br>";
    echo "$rec[2]<br>"; 
    }
    ?>
    
    27. Program to Store a image in Database
     <?php
    
      if(isset($_POST['sub'])) 
       
    {   
     $cont =   file_get_contents($_FILES['f1']['tmp_name']);  
    $type = $_FILES['f1']['type'];         $cont=addslashes($cont);  
        mysql_connect("servername","username","password"); 
    mysql_select_db("dbname");   $sql="insert into tablename values ('','$cont','$type')";  
     
    if(mysql_query($sql))   
    echo "Inserted";   
    else  
    echo mysql_error();   
    }
    ?>
    
     <form method="post" action="" enctype="multipart/form-data">
    
     <input type="file" name="f1">
    
    Image : <input type="submit" value="Upload" name="sub"> 
    
    </form> 
    
    28. Program to Read image from Database.
     <?php
    
    header("content-type:image/jpeg");     mysql_connect("servername","username","password"); 
    
    mysql_select_db("databasename");  
    $sql = "select * from tablename";
    $data = mysql_query($sql); 
        while($rec=mysql_fetch_row($data))
       { 
         echo "$rec[1]<br>";
       }
    
     ?>
    
    29. Contact form using Php
    *** contactform.php
    
    <html> 
    <head>
    </head> 
    <body>
    <h4>Contact Form</h4>
    <form method="post "action="sendmail
    .php">
     Name : <input type="text"name="uname"><br> 
    Mobile No. : <input type="text" name="mobile"><br> 
    Email id : <input type="text" name="email"><br> 
    Message : <textarea name="message">
    </textarea><br> 
    <input type="submit" value="Submit">
    </form> 
    </body> 
    </html> 
    
    *** sendmail.php
    
    <?php 
    $uname = $_POST['uname']; 
    $mobile = $_POST['mobile'];
    $email = $_POST['email']; 
    $message = $_POST['message'];
    
    $to = "phpkish@gmail.com"; 
    $subject = "Contact"; 
    $message = $message."\n Name: ".$uname."\n Mobile: ".$mobile."\n Email: $email;
    if(mail($to,$subject,$message))
     { 
           echo "Thank you for contacting us";
     } 
    else
     {
             echo "Try again"; 
    } 
    ?>
    
    30. Write a PHP script which will display the colors in the following way
    <?php
    $color = array('white', 'green', 'red');
    foreach ($color as $c)
    {
    echo "$c, ";
    }
    sort($color);
    echo "<ul>";
    foreach ($color as $y)
    {
    echo "<li>$y</li>";
    }
    echo "</ul>";
    ?>