PHP

 1. Largest of three numbers using if else ladder. 

<?php
    $a = 15;
    $b = 68;
    $c = 600;
    echo "<br>The given numbers are $a, $b and $c<br>";
    if($a>$b && $a>$c)
    {
        echo "$a is Maximum number";
    }
    elseif($b>$a && $b>$c)
    {
        echo "$b is Maximum number";
    }
    else
    {
        echo "$c is Maximum number";
    }
?>


2. check whether the given number is prime ornot usingforloop.

 
 <?php
    function primeCheck($number)
    {
        if($number ==1)
        return 0;
        for ($i = 2; $i <= $number/2; $i++)
        {
            if ($number % $i ==0)
             return 0;
        }
        return 1;
    }
    $number = 1;
    $flag = primeCheck($number);
    if ($flag == 1)
    echo "$number is prime number ";
    else
    echo"$number is Not a prime number ";
?>
 

3. To reverse a given number using while loop.

 <?php
    $num=1793;
    $rev=0;
    echo "Given number is $num";
    echo "<br>Reverse of a given number is ";
    while($num>=1)
    {
       $rem=$num%10;
       $rev=$rev*10+$rem;
       $num=$num/10;
    }
    echo $rev;
?>
 

4.To print the Fibonacci series using functions.

 
 <?php
    function fibonacci($n)
    {
        if($n==0)
        return 0;
        if($n==1)
        return 1;
        else
        {
            $fib=fibonacci($n-1)+fibonacci($n-2);
            return $fib;
        }
    }
    $num = 10;
    echo "The Given number is $num";
    echo "<br>The Fibonacci series of first $num is";
    for($i=0;$i<$num;$i++)
    {
        $res=fibonacci($i);
        echo "<br> $res ";
    }
?>
 

5.  To illustrate different string functions.

 <?php
    $str1="PHP SCRIPTING language";
    $str2=" It is partially case sensitive ";
    echo " The string1 is $str1";
    echo "<br>The length of the string1 is ".strlen($str1);
    echo "<br>The length of the string2 is ".strlen($str2);
    echo "<br>After trimming, string2 is:".strlen(trim($str2));
    echo "<br>Number of words in the string1 is ".str_word_count($str1);
    echo "<br>The reverse of the string1 is ".strrev($str1);
    echo"<br> String1 is in Lower case ".strtolower($str1);
    echo"<br> String1 is in Upper case ".strtoupper($str1);
    echo "<br>After replace php in to java is ".str_replace("PHP","Java",$str1);
    echo "<br> Sub string of string1 is ".substr($str1, 18,34);
    echo "<br> The lagnguge is present at ".strpos($str1,'guage') ." position";
?>

 

6. to implement calculator using switch case. 

 

<?php
    $a=7;
    $b=5;
    $opr= '+';
    switch($opr)
    {
        case '+':echo "<br> The Operator is $opr";
            echo "<br>Addition of $a and $b is ".($a+$b);
            break;
        case '-': echo "<br> The Operator is $opr";
            echo " <br>Substraction of $a and $b is ".($a-$b);
            break;
        case  '*' :  echo  "<br>  The  Operator  is $opr";
            echo " <br>Product of $a and $b is " . ($a*$b);
            break;
        case '/': echo "<br> The Operator is $opr";
            echo " <br>Quotient of $a and $b is " .($a/$b);
            break;
        case '%': echo "<br> The Operator is $opr";
            echo " <br>Reminder of $a and $b is".($a%$b);
            break;
        default : echo " <br>Inavalid operator or No Operator is selected";
    }
?>

 

7. To find factorial of a number using foreach loop. 

 

<?php
    $n=4;
    $fact=1;
    $a=array(1,2,3,4);
    foreach($a as $val)
    {
        $fact=$fact*$val;
    }
    echo " The Factorial of $n is $fact";
?>

 

8. To sort the given array. 

 

<?php
    $a=array(3,7,69,1,9,2);
    echo "The Given array is <br>";
    foreach ($a as $val)
    echo "$val<br>";
    for($i=0;$i<count($a);$i++)
    {
        for($j=0;$j<=count($a)-1;$j++)
        {
            if($a[$i]<$a[$j])
            {
                $temp=$a[$i];
                $a[$i]=$a[$j];
                $a[$j]=$temp;
            }
        }
    }
    echo "The sorted array is<br>";
    for($i=0;$i<count($a);$i++)
    echo $a[$i]. "<br>";
?>

 

9. To create and manage a database using SQLcommands.

  <?php
    $con=mysqli_connect("localhost","root","","mydb");
    $sql = "CREATE TABLE students(id INT(6), name VARCHAR(30))";
    if ($con->query($sql) === TRUE)
    {
       echo "Table STUDENTS created successfully";
    }
    $q1 = "INSERT INTO STUDENTS (id, name) VALUES ('101', 'Ramesh')";
    if ($con->query($q1) === TRUE)
    {
       echo "<br>New record created successfully<br>";
    }
    $q2 = "SELECT id, name FROM STUDENTS";
    $result = $con->query($q2);
    if ($result->num_rows> 0)
    {
       while($row = $result->fetch_assoc())
       {
          echo "id: " . $row["id"]. " -Name: " . $row["name"]."<br>";
       }
    }
    $q3 = "INSERT INTO STUDENTS (id, name) VALUES ('102','Sangitha')";
    $con->query($q3);
    $q4 = "DELETE FROM STUDENTS WHERE id=102";
    if ($con->query($q4) === TRUE)
    {
    }
    else
    {
       echo "Record deleted successfully";
       echo "Error deleting record: " . $con->error;
       $con->close();
       }
?>

 

10. To create and validate a email id.

 <?php
    $email = "thontadari@gmail.com";
    echo "The given mail is $email<br>";
    if (filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        echo " This is Valid mail Id";
    }
    else
    {
        echo "This is Invalid mail ID ";
    }
?>

 

11.Using PHP and SQL, create and validate a sample login form.

   

Login.php

<html>
<head>
    <body bgcolor="pink" alink="black" vlink="voilet">
    <div align="center">
    <h1>Login Form</h1>
    <form name="loginform" action="loginsession.php" method="post">
    <tr></tr>
    <label>User name <input type="text" name="username" style="background" required/></label><br/><br/>
    <label>Password <input type="password" name="password" maxlength="8"required/><br/>
    <input type="submit" value="Submit" name="ok"/>
    <input name="reset" type="reset" value="Reset"/>
    </form>
    </div>
</body>
</html>


Loginsession.php

<?php
    session_start();
    $l=mysqli_connect("localhost","root","");
    mysqli_select_db($l,'employ');
    $username=$_POST['username'];
    $password=$_POST['password'];
    $res=mysqli_query($l,"select * from `user_registration` where`username`='".$username."' and `password`='".$password."'");
    if(mysqli_num_rows($res)>0)
    {
        header("location:welcome.php");
        exit();
    }
    else
    ?>
    {
        <script>alert("Enter Valid User Credential");
        window.location="login.php";
        </script>
    <?
    }
?>


Welcome.php

<html>
<body>
    <font color=purple>
    <center>
    <pre>
        Welcome to PHP
    <em><font color="#7c0000">
    <a href=login.php>
    <font color="Red">Home</a>
    </font>
    </font>
</body>
</html>

3 Responses to "PHP"

  1. Yar guru ninu kalavida

    ReplyDelete
  2. I love u rangnath ❤❤❤❤❤big fan of u ����like u so much

    ReplyDelete
  3. This website helped us a lot in our exams:-)
    Love from siddaganga women's clg❤️

    ReplyDelete