Translate

PHP CODES


 BASIC SYNTAX 

(LEC -01)


<?php
    // ceate first php program
  echo "Today is my First Class ";
  echo "  <input type =\"text\" name=\"name\">";
//  Concatenation

  $date= 15;
  $day= "thursday";
  $year = 2022;

 echo 'Now today date is '.$date. ' '.$day. ' '.$year;

echo "  now today is $date $day $year";

// variables & Embedding inside HTML

$a=5;
$b=10;
$sum=$a+$b;
echo $sum;
?>
 

<h1>SUM : <?php  echo $sum ;  ?></h1>


OPERATORS

 (LEC -02)


<?php

//            OPERATORS

/*
1.Assignment Operator
2.Arithematic Operator
3.Rational Operator
4.logical operator
5.Triple Operator  
*/

// Assignment Operator
/*
  $a = 5;

 //$result = $a + 5;

   $a += 15;
  echo $a;
*/
// Arithematic Operator

/*
$a = 15;
$b=  25;

$addition= $a+$b;
$subtraction= $a-$b;
$multiplication= $a*$b;
$division= $a/$b;
$a++;
$b--;

echo "Addition : ".$addition."<br>";
echo "Subtraction : " .$subtraction."<br>";
echo "Multiplication : ".$multiplication."<br>";
echo "Division :  ".$division."<br>";
echo "After Increment :  ".$a."<br>";
echo "After Decrement : ".$b."<br>";

*/

// Rational Operator
/*
$a = 33;
$b = 95;

if($a <= $b )
    {  
         echo "PASS";
    }
else
    {
        echo "FAIL";
    }
*/
// logical Operator
/*
$age = 20;


if (( $age >= 15  )&&( $age <= 25  ))
    {  
       echo " Admission";
    }
else
    {
        echo "No Addmission";
    }
*/

// Triple Operator
/*
$a = '20';
$b = 20;

if($a===$b){

    echo "TRUE";
}
 else{
    echo "FALSE";

 }
 */
?>


CONTROL CONDITIONAL STRUCTURE (LEC -03)


<?php
 
//control conditional structure

// if - else structure

/*
$a=34;

if($a>=33)

     {
        echo "PASS";
     }

else{

    echo "FAIL";


   }

*/

// if - else - if  structure

/*
  $a=93;

 if($a>=100)

     {
        echo "A+ GARDE";
     }
     else if (($a >= 80) && ($a <= 89) )
     {
        echo " A GARDE  ";
     }
     else if (($a >= 70) && ($a <= 79) )
     {
        echo " B GARDE  ";
     }
      else if (($a >= 60) && ($a <= 69) )
     {
         echo " C GARDE  ";
     }
      else if (($a >= 50) && ($a <= 59) )
     {
        echo " B GARDE  ";
     }
      else
     {
       echo " FAIL ";
    }  
*/

//switch case
/*
$alphabet = "b";

switch ($alphabet) {
  case "a":
    echo "A FOR APPLE ! <br>";
    break;
  case "b":
    echo "B FOR BAT ! <br>";
    break;
  case "c":
    echo "C FOR CAT ! <br>";
    break;
  default:
    echo "NO MATCH ! <br>" ;
}

*/



?>


LOOP STRUCTURE 

(LEC -04)


<?php
echo " <br> FOR LOOP<br><br>";
for ($x = 0; $x <= 10; $x=$x+2)
{
    echo "The number is: $x <br>";
}


echo " <br>WHILE LOOP<br><br>";
$y = 0;
while($y <= 10) {
  echo "The number is: $y <br>";
  $y=$y+2;
}

echo " <br>DO - WHILE LOOP<br><br>";

$z = 1000;

do {
  echo "The number is: $z <br>";
  $z=$z+2;
} while ($z <= 10);


?>


ARRAY 

(LEC -05)


<?php
 // Array

 /*                     0          1      2        3        4        5        
 $array = array(' sir jahangir','rehan','ammar','musawir','ahsan','furqan');
 print_r($array);
*/


//Assosiative Array

/*
$students= array('sir jahangir'=>75 ,'rehan'=>76,'ammar'=>77,'musawir'=>99,
'ahsan'=>54,'furqan'=>98);
// print_r($students);
echo $students ['musawir'];
*/

//Multidemention Array
/*
$students= array('passed'=>array  ('rehan','ahsan'),
                 'Failed'=> array('ammar',''));


                 echo $students['passed'][0]."<br>";
                 echo $students['passed'][1]."<br>";
*/
?>


FORM HANDLING 
(LEC -06)

<?php
  $nameErr =" ";
  $emailErr ="";
 if(isset($_POST['submit'])){
     if(empty($_POST['name'])){
     $nameErr = "* Name cannot be empty. ";
     }
     if(empty($_POST['email'])){
        $emailErr = "* Email cannot be empty. ";
        }
 }
?>

<!DOCTYPE html>
<html>
<body>

<h2>LOGIN FORM</h2>
<form action=" " method="POST" >
    <label for="name">Enter your name: </label><br>
    <input type="text" name="name" id="name" ><?php echo $nameErr;?><br>
    <label for="email">Enter your email: </label><br>
    <input type="email" name="email" id="email"><?php echo $emailErr;?><br>
    <input type="submit" name="submit" value="Submit" >
 
</form>
</body>
</html>




WORKING WITH FUNCTIONS IN PHP (LEC-07)

<?php

  // Basic Function
  /*
 function sum (){
$a=200;
$b=500;
echo $sum=$a+$b;
 }
sum();
*/
// Function with Arrgument
/*
function sum ($a,$b){

    echo $sum=$a+$b."<br>";
     }
    sum (100,200);

*/
 
// Function with Return value  
/*
 
    function php($a){

     
       return  $php=$a;
         }
    echo 25 + php(15)."<br>";
    echo 25+ php(55)."<br>";
    echo 25 + php(36)."<br>";
    echo 25 + php(39)."<br>";
    echo 50 + php(5)."<br>";
*/


function force($mass1,$mass2,$radius,$gravity){

             $ex=$mass1*$mass2;
             $ex1=$ex/$radius;
    return  $force=$ex1*$gravity;
      }
 echo "THE VALUE OF FORCE ".force(25,26,26,25)."<br>";
?>



FILE HANDLING AND EXCEPTION HANDLING IN PHP (LEC-08)
<?php
// Parent and child
$num=0;
try{
    if($num==0){  
        throw new Exception("Please valid Number");
    }
    if($num==4){  
        throw new Numberis4Exception("Number is 4 is not allow");
    }
echo  2/$num;
}catch (Exception $e){
    echo $e->getMessage();
}
catch (Numberis4Exception $e){
    echo $e->getMessage();
}
?>
// With parent and child
<?php
$num=100;
class Numberis4Exception extends Exception{
        function errorMessage(){
        return $this->getMessage();
}
}
try{
    if($num==0){  
        throw new Exception("Please valid Number");
    }
    if($num==4){  
        throw new Numberis4Exception("Number is 4");
    }
   
echo  2/$num;
}catch (Exception $e){
    echo $e->getMessage();
}
catch (Numberis4Exception $e){
    echo $e->getMessage();
}


finally
{

     echo  ' Finally message !';
}

?>




COOKIES AND SESSIONS MANAGEMENT IN PHP (LEC-08)




DATABASE MANAGEMENT IN PHP 
(LEC -09)




ADVANCED FEATURE OF PHP 
(LEC -10)



No comments:

Post a Comment