8 TH

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

8th

<?php

$dayOfWeek = date('l');

$colors = [

'Monday' => 'red',

'Tuesday' => 'green',

'Wednesday' => 'blue',

'Thursday' => 'yellow',

'Friday' => 'orange',

'Saturday' => 'purple',

'Sunday' => 'black',

];

if (array_key_exists($dayOfWeek, $colors)) {

$bg = $colors[$dayOfWeek];

echo "<html><body style='background-color: $bg;'><h1> today's color </h1> </body></html>";


?>

9th

html>

<body>

<form>

enter the limit : <input type="text" name="prime"><br>

<input type="submit" >

</form>

</body>

<?php

$n = $_GET['prime'];

for($i=2;$i<=$n;$i++){

$b=0;

for($j=2;$j<$i;$j++){

if($i%$j==0){

$b=1;

break;

if($b==0){

echo "$i" ;

echo "<br>";
}

} ?>

</html>

B)

<html>

<body>

<form >

enter the limit : <input type="text" name = "fib"><br>

<input type="submit">

</form>

</body>

<?php

$n = $_GET['fib'];

$a=0;

$b=1;

echo $a;

echo "<br>";

echo $b ;

echo "<br>";

for($i=3;$i<=$n;$i++){

$c = $a+$b ;

echo $c ;
echo "<br>";

$a = $b;

$b=$c;

?>

</html>

10th)

<!DOCTYPE html>

<html>

<body>

<form method="post">

Enter the value with space: <input type="text" name="no">

<input type="submit" >

</form>

<?php

$myarray=$_POST["no"];

$mya=explode(" ",$myarray);

sort($mya);

print_r(array_unique($mya));

?>

</body>

</html>
11th)

<?php

for ($i = 5; $i >= 1; $i--) {

for ($j = 1; $j <= $i; $j++) {

echo "*";

echo "<br>";

?>

12th)

<?php

$a = [

['id'=> 1,'name'=>'a','age' => 20 ],

['id'=> 2,'name'=> 'b','age' => 25 ],

['id'=> 3,'name'=> 'c','age' =>30 ],

['id'=> 4,'name'=> 'd','age' => 35 ]

];

function search($data,$criteria,$value) {
$result=[];

foreach($data as $item)

if($item[$criteria]==$value){

$result[]=$item;

return $result ;

$x= search($a,'age','25');

if(count($x)>0){

foreach($x as $v){

echo $v['id'];

echo "<br>";

echo $v['name'];

echo "<br>";

echo $v['age'];

echo "<br>";

?>
15th)

<html>

<head>

<title> Read and Write a data into file </title>

</head>

<body>

<form method="post">

<textarea name="content" rows="5" cols="40">

</textarea>

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

</form>

<?php

$myfile=fopen("C:/xampp/htdocs/ab.txt","w") or die("file not found");

$x=$_POST['content'];

fwrite($myfile,$x);

fclose($myfile);

$myfile=fopen("C:/xampp/htdocs/ab.txt","r") or die("file not found");

echo "content of file are <br>";


while(!feof($myfile)) {

echo fgets($myfile) . "<br>";

fclose($myfile);

?>

</body>

</html>

17th)

<!DOCTYPE HTML>

<html>

<head>

</head>

<body>

<?php

$name = $email = $website ="";

$name = test_input($_POST["name"]);

$email = test_input($_POST["email"]);

$website = test_input($_POST["website"]);

function test_input($data) {
$data = trim($data); // trimes the spaces before and after the string

$data = stripslashes($data); // if the text containes forward slash that is \\\ it removes it

$data = htmlspecialchars($data); // gives the html tag as it is do not execute it in browser

return $data;

?>

<h2>PHP Form Validation Example</h2>

<form method="post">

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

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

Website: <input type="text" name="website">

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

</form>

<?php

echo "<h2>Your Input:</h2>";

echo $name;

echo "<br>";

echo $email;

echo "<br>";

echo $website;

?>
</body>

</html>

18th)

<?php

$cookie_name = "user";

$cookie_value = "John Doe";

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");

if(!isset($_COOKIE[$cookie_name])) {

echo "Cookie named '" . $cookie_name . "' is not set!";

} else {

echo "Cookie '" . $cookie_name . "' is set!<br>";

echo "Value is: " . $_COOKIE[$cookie_name];

?>

20 A)
<?php

try {

$a = 10;

$b = 0;

if ($b !== 0) {

$c = $a % $b;

echo $c;

} else {

throw new Exception("Division by zero is not allowed.");

} catch (Exception $e) {

echo $e->getMessage();

?>

B)

<?php

try {

$dateString = "2022-30-01"; // Replace with your date string


$date = new DateTime($dateString);

echo "Valid date format";

} catch (Exception $e) {

echo "Invalid date format";

?>

You might also like