0% found this document useful (0 votes)
22 views9 pages

WEB2

Uploaded by

sgusain483
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views9 pages

WEB2

Uploaded by

sgusain483
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Q22.Write a program in PHP to swap two integer values.

CODE-
<!DOCTYPE html>
<html>
<head>
<title>Swap Two Variables</title>
</head>
<body>
<h2>Enter two numbers to swap</h2>
<form method="post" action="">
<label for="a">Enter first number (a):</label>
<input type="text" name="a" id="a" required>
<br><br>

<label for="b">Enter second number (b):</label>


<input type="text" name="b" id="b" required>
<br><br>
<input type="submit" value="Swap">
</form>

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$a = $_POST['a'];
$b = $_POST['b'];

echo "<h3>Before swap:</h3>";


echo "a = $a, b = $b<br><br>";

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

echo "<h3>After swap:</h3>";


echo "a = $a, b = $b<br>";
}
?>
</body>
</html>

OUTPUT:
Q23.Write a PHP program to :
a) Transform a string to all uppercase letters.
b) Transform a string to all lowercase letters.
c) Make a string’s first letter uppercase.
d) Make a string’s first letter lowercase and rest of the letters to
uppercase.

CODE-
<html>
<head><h1>STRING METHODS</h1>
</head>
<body>
<?php

$string = "Ajay Bhatt";


$uppercaseString = strtoupper($string);
echo $uppercaseString . "<br>";

$lowercaseString = strtolower($string);
echo $lowercaseString . "<br>";

$firstLetterUppercase = ucfirst($string);
echo $firstLetterUppercase . "<br>";

$firstLowerRestUpper = lcfirst(strtoupper($string));
echo $firstLowerRestUpper . "<br>";
?>
</body>
</html>

OUTPUT:
Q24.Write a PHP program for showing the use of all the operators in PHP.
CODE:
<?php
// Arithmetic Operators
$a = 10;
$b = 5;

echo "Sum: " . ($a + $b) . "<br>";


echo "Difference: " . ($a - $b) . "<br>";
echo "Product: " . ($a * $b) . "<br>";
echo "Division: " . ($a / $b) . "<br>";
echo "Modulus: " . ($a % $b) . "<br>";
echo "Exponent: " . ($a ** $b) . "<br>";

// Assignment Operators
$x = 10;
$x += 5; // $x = $x + 5
echo "x: " . $x . "<br>";

$y = 20;
$y -= 3; // $y = $y - 3
echo "y: " . $y . "<br>";

// Comparison Operators
echo "Is 10 equal to 10? " . ($a == $b) . "<br>";
echo "Is 10 identical to 10? " . ($a === $b) . "<br>";
echo "Is 10 not equal to 5? " . ($a != $b) . "<br>";
echo "Is 10 not identical to 5? " . ($a !== $b) . "<br>";
echo "Is 10 greater than 5? " . ($a > $b) . "<br>";
echo "Is 10 less than 5? " . ($a < $b) . "<br>";
echo "Is 10 greater than or equal to 5? " . ($a >= $b) . "<br>";
echo "Is 10 less than or equal to 5? " . ($a <= $b) . "<br>";

// Logical Operators
$p = true;
$q = false;

echo "p AND q: " . ($p && $q) . "<br>";


echo "p OR q: " . ($p || $q) . "<br>";
echo "NOT p: " . (!$p) . "<br>";
?>

OUTPUT:
Q25. Write a PHP program to implement Super global variable.
CODE-
1.$GLOBALS
CODE-
<?php
$x = 300;
$y = 200;

function multiplication(){
$GLOBALS['z'] = $GLOBALS['x'] * $GLOBALS['y'];
}

multiplication();
echo $z;
?>

OUTPUT:

2.$_SERVER
CODE:
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
echo "<br>"
?>
OUTPUT:

3.$Cookie
CODE-
<?php
$cookie_name = "user";
$cookie_value = "AJAY";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 =
1 day
?>
<html>
<body>

<?php
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];
}
?>

OUTPUT:

4.$_SESSION
CODE-
<?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.";
?>

OUTPUT:

You might also like