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>