1)What is a function How can you define and call a function
1)What is a function How can you define and call a function
Ans :- In PHP, a function is a block of code that can be defined and executed whenever it is called within
a program. Functions are useful for organizing code, making it more modular and reusable.
Here's a basic example of how you can define and call a function in PHP:
<?php
function sayHello() {
sayHello();
?>
<?php
return $sum;
?>
2) How can you retrieve form data? Write a PHP program to create a form data that will take
a Fahrenheit value as input and displays equivalent Celsius value on submit. The Celsius =
(5/9)*(Farenheit-32).
Ans= To retrieve form data in PHP, you can use the $_POST or $_GET
superglobal arrays, depending on the form's method attribute (POST or GET)
<!DOCTYPE html>
<html>
<head>
<title>Fahrenheit to Celsius Converter</title>
</head>
<body>
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the Fahrenheit value from the form
$fahrenheit = $_POST["fahrenheit"];
</body>
</html>
Ans:- Sometimes you need to change a variable from one data type into
another, and sometimes you want a variable to have a specific data type.
This can be done with casting.
$a = 5; // Integer
$b = 5.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = (string) $a;
$b = (string) $b;
$c = (string) $c;
$d = (string) $d;
$e = (string) $e;
For loop
<?php
// Output: 0 1 2 3 4
?>
Foreach
$colors = array("red", "green", "blue");
$array[] = $data;
fclose($open);
echo "<pre>";
var_dump($array);
echo "</pre>";
?>
Session
<?php
// Start the session
session_start();
In this example:
session_start() is used to initialize the session.
We check if the user is already logged in by checking if the 'username'
key is set in the $_SESSION array.
If the user is logged in, a welcome message is displayed. Otherwise, a
login form is shown.
If the form is submitted, the username is retrieved from the form, and
it is stored in the session with $_SESSION['username'].
The program then displays a welcome message with the username.
Note: This is a basic example, and in a real-world application, you would
likely use more secure methods for handling user authentication and store
sensitive information securely. Additionally, sessions can be configured with
various options to enhance security and control session behavior.