Thursday, 21 November 2024

PHP Advanced-2

 Example-1


<?php

 echo "<h3>Date/Time functions</h3><br>";

 echo date("d/m/y")."<br/>";

 echo date("d M,Y")."<br/>";

 $date_array = getdate(); 

 foreach ( $date_array as $key => $val )

 {  

  print "$key = $val<br />"; 

 }

 echo var_dump(checkdate(2,29,2003))."<br>";

 echo var_dump(checkdate(2,29,2004))."<br>";

 echo time();

 echo(date("M-d-Y",mktime(0,0,0,12,36,2001))."<br>");

 echo(date("M-d-Y",mktime(0,0,0,1,1,99))."<br>");

 $formated_date = "Today's date: "; 

 $formated_date .= $date_array['mday'] . "/"; 

 $formated_date .= 

 $date_array['mon'] . "/";

 $formated_date .= $date_array['year']; 

 print $formated_date; 

 print date("m/d/y G.i:s<br>", time()); 

 print "Today is ";

 print date("j of F Y, \a\\t g.i a", time()); 

 echo "<br><h3>Math functions</h3>";

 echo abs(6.7)."<br>";

 echo abs(-6.7)."<br>";

 echo abs(-3)."<br>";

 echo abs(3);

 echo ceil(0.60)."<br>";

 echo ceil(0.40)."<br>";

 echo ceil(5)."<br>";

 echo ceil(5.1)."<br>";

 echo ceil(-5.1)."<br>";

 echo ceil(-5.9);

 echo exp(0)."<br>";

 echo exp(1)."<br>";

 echo exp(10)."<br>";

 echo exp(4.8);

 echo floor(0.60)."<br>";

 echo floor(0.40)."<br>";

 echo floor(5)."<br>";

 echo floor(5.1)."<br>";

 echo floor(-5.1)."<br>";

 echo floor(-5.9)."<br>";

 echo rand(10,100)."<br>";

 echo min(2,4,6,8,10)."<br>";

 echo max(2,4,6,8,10)."<br>";

 echo pow(2,4)."<br>";

 echo round(0.60)."<br>";

?>

 Example-2


<?php

         $string1="Hello Friend ";

         $string2="Welcome";

         echo "<br>";

        echo $string1 . " " . $string2;

        echo "<br>";

        echo strlen($string1);

        echo "<br>";

        echo strpos("Hello world!","world"); 

        echo "<br>";

         echo strtoupper($string1);

        echo "<br>";

        echo substr_replace($string1, "T",6,1);

        echo "<br>";

        echo substr($string1,0,5);

        echo "<br>";

        echo substr_count($string1,"e");

?> 


 Example-3

<?php     
$to_email = 'vainshbharatit@gmail.com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail function';
$headers = 'vainsh_bharat@yahoo.com;
mail($to_email,$subject,$message,$headers);
?>


 Example-4

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>

 Example-5

<?php
// Start the session
session_start();
?>

<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>

</body>
</html>


 Example-6

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>

</body>
</html>


Example-7

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// remove all session variables
session_unset();

// destroy the session
session_destroy();
?>

</body>
</html>

PHP Advanced-1

 Example-1

<?php

echo"First 10 Fibonacci no.";

$i=1;

$j=1;

echo"$i , $j ,";

for($k=2;$k<50;$k++)

{

$k=$i+$j;

$i=$j;

$j=$k;

echo" $k ,";

}

?>


 Example-2

<!DOCTYPE html>

<html>

<body>


<form name="f1" action="upload.php" method="post" enctype="multipart/form-data">

  Select image to upload:

  <input type="file" name="fileToUpload" id="fileToUpload">

  <input type="submit" value="Upload Image" name="submit">

</form>


</body>

</html>


 Example-3


<?php

$target_dir = "uploads/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));


// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) {

  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

  if($check !== false) {

    echo "File is an image - " . $check["mime"] . ".";

    $uploadOk = 1;

  } else {

    echo "File is not an image.";

    $uploadOk = 0;

  }

}


// Check if file already exists

if (file_exists($target_file)) {

  echo "Sorry, file already exists.";

  $uploadOk = 0;

}


// Check file size

if ($_FILES["fileToUpload"]["size"] > 500000) {

  echo "Sorry, your file is too large.";

  $uploadOk = 0;

}


// Allow certain file formats

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

  $uploadOk = 0;

}


// Check if $uploadOk is set to 0 by an error

if ($uploadOk == 0) {

  echo "Sorry, your file was not uploaded.";

// if everything is ok, try to upload file

} else {

  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

  } else {

    echo "Sorry, there was an error uploading your file.";

  }

}

?>


Example-4

<?php
function calcAverage()
{
    // initialize value to be used in calculation
    $total = 0;

    // find out how many arguments were given
    $arguments = func_num_args();     

    // loop to process each argument separately
    for ($i = 0; $i < $arguments; $i++) {
        // add the value in the current argument to the total
        $total += func_get_arg($i);
    }

    // after adding all arguments, calculate the average
    $average = $total / $arguments;

    // return the average
    return $average;
}

// invoke the function with 5 arguments

echo calcAverage(44, 55, 66, 77, 88);

// invoke the function with 8 arguments
echo calcAverage(12, 34, 56, 78, 90, 9, 8, 7);



Example-5

<html>

<body>

<?php

if(isset($_POST["submit"]))

{

$value = $_POST['val1'];

}

else

{

$value = "";

}

?>


<form name="f1" action="#" method="post">


Enter Age<input type="text" name="val1" required/>


<input type="submit" name="submit">

</form>



<?php

if($value)

{

if($value>=18)

    echo "You are Eligible for Vote"; }

else

    echo "You are not Eligible for Vote"; }

}

?>

</body>

</html>


Example-6

<html>
<body>

<form name="f1" action="verify.php" method="post">
<select name="book">
    <option></option>
    <option>ADA</option>
    <option>WT</option>
</select>

Quantity
<input type="text" name="q1">
<input type="submit">
</form>

</body>
</html>


Example-7

<html>
<body>
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("h:m:s")."<br>";
?>
<?php require();>
</body>
</html>


PHP Basic-2

 Example-1


<!DOCTYPE html>

<html>

  <head>

    <title></title>

  </head>

  <body bgcolor="white">

  <?php

if(isset($_POST['submit']))

{

        $num=$_POST['num1'];

        define('NUM',$num);


        for($i=1;$i<=10;$i++)

        {

            echo $i*NUM;

            echo '</br>';

        }

}

  ?>

<form name="f1" method="post" action="#">

Enter Number<input type="text" name="num1" size="25" required>

<input type="submit" name="submit"/>

</form>

</body>

</html>


 Example-2

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <?php

$x=8;
if($x>5)
{
    echo "$x is greater than 5</br>";
}
if($x<10)
{
    echo "$x is also less than 10</br>";

}

  ?>

 Example-3

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">
  <?php
      $a=80;
      echo "your grade is:";
      switch($a)
      {
          case $a>=90:
            echo "A</br>";
          break;

          case $a>=80:
            echo "B</br>";
          break;

          case $a>=70:
            echo "C</br>";
          break;

          default
          echo "you have fail";
         break;
      }
  ?>
</body>
</html>


 Example-4

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <?php

      $a=10;

      while($a>=0)
      {
          echo"no is".$a."\n";
          $a--;

      }
     

  ?>

  <?php
          echo "hello world".$a;
          print ""

  ?>


 Example-5

<?php
          if(isset($_POST['submit']))
          {
            
           $num1=$_POST['val1'];

           if($num1%2==0)
              {
                  echo "Even No.";
              }
              else
              {
                  echo "Odd No.";
              }  
            }
          
   ?>

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <form name="f1" action="#" method="post">

Enter No.<input type="text" name="val1" size="20">

<input type="submit" name="submit" value="check">
        </form>


   </body>
   </html>
   

 Example-6

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>

  <body bgcolor="white">

  <?php
$a=0;
$b=0;

for($i=0;$i<=5;$i++)
{
    $a+=10;
    $b+=5;
}

echo"at end of the loop a= $a and b=$b</br>";
 


$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value </br>";
}


function myname($name)
{
    echo "your name is".$name."</br>";    

}

echo "Hi!";
myname("james");


function mul($x,$y)
{
    $total=$x*$y;
    return $total;

}

echo "the value of=".mul(7,3);
echo "</br>";
function add_five(&$value) {
    $value += 5;
  }
  
  $num = 2;
  add_five($num);
  echo $num;
  
  echo "</br>";

  function hello()
  {

    echo "hi";

  }

  $check="hello";

  $check();

  $sub=array("wd","ADA");
  echo $sub[0]."and" .$sub[1]."are good subjects";


  $char=array(name=>"suresh",age=>22);
    echo $char[age];

    $char1=array(
          array(firstname=>"ravi",lastname=>"patel"),
          array(firstname=>"mayur",lastname=>"xyz")
        );

        //echo $char1[firstname];

        foreach($char1 as $val)
        {
            foreach($val as $key=>$final_val)
            {
                echo "$key:$final_val</br>";

            }
        }

        $cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
echo"</br>";

$check=array("free","program","apple");
sort($check);
echo $check;
//print($check);

foreach($check as $var)
{
    echo "$var</br>";

}

//array_slice()

$first=array("a","b","c","d","e");
$second=array_slice($first,1,2);
foreach($second as $var)
{
    echo "$var</br>";

}

//array_merge()

$first=array("a","b","c","d","e");
$second=array(1,2,3);
$third=array_merge($first,$second);
foreach($third as $var)
{
    echo "$var";

}

?>
</body>
</html>


 Example-6

<html>
<body>
<?php
if(isset($_POST["submit"]))
{
$value = $_POST['val_1'];
}
else
{
$value = "";
}
?>
<form method="POST" action="">
Enter your age <br>
<input type="text" name="val_1" value="<?php echo $value; ?>"> <br />
<input type="submit" name="submit" value= "click here">
</form>
<?php
if($value)
{
if($value>=18)
{ echo "You are Eligible for Vote"; }
else
{ echo "You are not Eligible for Vote"; }
}
?>
</body>
</html>


 Example-7

<html>
<head>
<title>STAR</title>
</head>
<body>
<h2>PYRAMIND</h2>
<?php
if(isset($_POST[“Submit”]))
{
$val = $_POST[“val”];
$symbol = $_POST[“symbol”];
}
else if(isset($_POST[“clear”]))
{
$val = “”;
$symbol = “”;
}
else
{
$val = “”;
$symbol = “”;
}?>
<form method=”POST”>
Enter the loop Number <input type=”text” name=”val” value="<?php echo”$val”; ?>" />
</br>
Enter the Symbol <input type=”text” name=”symbol” value="<?php echo”$symbol”; ?>"/>
</br>
<input type=”submit” name=”Submit”>
<input type=”submit” name=”clear” value=”Reset” >

</form>
<?php
if($val and $symbol)
{
if(is_numeric($val))
{ for($i=1;$i<=$val;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo “&nbsp “.$symbol;
}
echo “<br />”;
}
}

else
{
echo “Enter loop in Digits”;
}
}
?>
</body>
</html>


PHP Basic-1

 Example 1: 

 <!DOCTYPE html>

<html>

  <head>

    <title></title>

  </head>

  <body bgcolor="white">


  <?php

        echo "hello world</br>";

        print "hi!how are you?";

          ?>

</body>

</html>



 Example 2:

<html>

<head>

<script type="text/javascript">

$(document).ready(function(e){

 $('#btnSubmit').click(function(){ 

 var email = $('#txtEmail').val();

 if ($.trim(email).length == 0) {

 alert('Please Enter Valid Email Address');

 return false;

 }

 if (validateEmail(email)) {

 alert('Valid Email Address');

 return false;

 }

 else {

 alert('Invalid Email Address');

 return false;

 }});

});

</script>

</head> <body>

Email Address: <input type="text" id="txtEmail"/><br/>

<input type="submit" id="btnSubmit" Value="Submit" />

</body>

</html> 


 Example 3:

<html>

<body>

<form name="myform" action="welcome.php" method="post">

Name: <input type="text" name="name"><br>

E-mail: <input type="text" name="email"><br>

<input type="submit" name="submit">

</form>

</body>

</html>


 Example 4:

<?php

if(isset($_POST['submit']))

{

$num1=$_POST['num1'];

$num2=$_POST['num2'];


$result=$num1+$num2;


echo "Sum of " .$num1. " and ".$num2. " is " .$result;

}

?>

<!DOCTYPE html>

<html>

<head>

<title>Add two number</title>

</head>

<body>

<table>

<form name="add" action="#" method="post">

<tr>

<td>Number 1 :</td>

<td><input type="text" name="num1" required></td>

</tr>

<tr>

<td>Number 2 :</td>

<td><input type="text" name="num2" required></td>

</tr>

<tr>

<td></td>

<td><input type="submit" value="Add" name="submit" /></td>

</tr>

</form>

</table>

</body>

</html>


  Example 5:

<!DOCTYPE html>

<html>

  <head>

    <title></title>

  </head>

  <body bgcolor="white">

  <?php

        echo "hello world</br>";

        print "hi!how are you?";

        $text1="hello";

        $text2=10;

        echo $text1;

        echo $text2;

        echo $text1 ." " .$text2;

        echo $text1;

        print($text1);

        $bikes=array("yamaha","royal","honda","hero");

        var_dump($bikes);

        echo  "array element 1:$bikes[0] $bikes[1]$bikes[2] </br>";

        $x = "Hello world!";

        $x = null;

        var_dump($x);

echo "</br>";

      $x = 5985;

var_dump($x);

$cars = array("Volvo","BMW","Toyota");

var_dump($cars);

echo "</br>";

$text="hello world";

$text=null;

echo $text;

var_dump($text);

echo "</br>";

        $text2="xyz\"this is string\"pqr";

        echo $text2;

        echo "</br>";

        echo strlen("php programming");

        echo "</br>";

        echo str_word_count("php programming language"); 

        echo "</br>";

        echo strrev("JAMES"); 

        echo strpos("Hello world!", "world"); 

        echo strpos("developer news","news");

       $x = 5985;

        var_dump(is_int($x));

        $x = 1.9e411;

        var_dump($x);

        $x = acos(8);

        var_dump($x);

        echo "</br>";

// Cast float to int

$x = 23465.768;

$int_cast = (int)$x;

echo $int_cast;

echo "<br>";

// Cast string to int

$x = "23465.768";

$int_cast = (int)$x;

echo $int_cast;

echo "<br>";

echo(pi()); // returns 3.1415926535898

echo "<br>";

echo "<br>";

echo(rand(1000, 10000));

define("cars", [

  "Alfa Romeo",

  "BMW",

  "Toyota"

]);

echo cars[0];

    ?>

</body>

</html>


  Example 6:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <?php

    $a=45;
    $b=35;

    $temp=$a;
    $a=$b;
    $b=$temp;

    echo "after the swapping :</br></br>";
    echo "a=".$a. "b=".$b;

  ?>

  Example 7:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <?php


    $a=45;
    $b=35;
    $c=20;

     
    if($a>$b and $a>$c)
    {
        echo "Max No. is $a";
    }
    elseif($b>$c)
    {
        echo "Max No. is $b";

    }
    else
    {
      echo "Max No. is $c";

    }

   ?>
   </body>
   </html>