Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

1)What is a function How can you define and call a function

This is second file of digital sat vocubalary list.

Uploaded by

ProjectLA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

1)What is a function How can you define and call a function

This is second file of digital sat vocubalary list.

Uploaded by

ProjectLA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1)What is a function? How can you define and call a function in PHP?

Write a PHP program to


implement a function with parameters and return value

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 definition without parameters and return value

function sayHello() {

echo "Hello, World!";

// Call the function

sayHello();

?>

<?php

// Function definition with parameters and return value

function addNumbers($num1, $num2) {

$sum = $num1 + $num2;

return $sum;

// Call the function with arguments and store the result

$result = addNumbers(5, 7);

// Display the result

echo "The sum is: " . $result;

?>
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"];

// Calculate Celsius value


$celsius = (5/9) * ($fahrenheit - 32);

// Display the result


echo "<p>The equivalent Celsius value is: $celsius °C</p>";
}
?>

<!-- HTML Form -->


<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="fahrenheit">Enter Fahrenheit value:</label>
<input type="text" name="fahrenheit" required>
<input type="submit" value="Convert">
</form>

</body>
</html>

3) What are casting operators? Differentiate for from foreach statement.


Write PHP program to illustrate use of for and foreach.

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.

Change Data Type


Casting in PHP is done with these statements:

 (string) - Converts to data type String


 (int) - Converts to data type Integer
 (float) - Converts to data type Float
 (bool) - Converts to data type Boolean
 (array) - Converts to data type Array
 (object) - Converts to data type Object
 (unset) - Converts to data type NULL

$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

for ($i = 0; $i < 5; $i++) {

echo $i . " ";

// Output: 0 1 2 3 4

?>

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

foreach ($colors as $color)

{ echo $color . " "; }

4) Write a PHP program to parse a CSV file.


<?php

if (($open = fopen("Book1.csv", "r")) !== false) {

while (($data = fgetcsv($open, 1000, ",")) !== false) {

$array[] = $data;

fclose($open);

echo "<pre>";

// To display array data

var_dump($array);

echo "</pre>";

?>

Session

In web development, a session is a way to store information (or data) about


a user across multiple pages or visits to a website. Sessions are typically
used to maintain stateful information, such as user login status, shopping
cart contents, and other user-specific data. PHP provides built-in session
handling functions to make it easy for developers to work with sessions.
Here's a simple PHP program that demonstrates how to use sessions to
maintain user data:
phpCopy code

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

// Check if the user is already logged in


if (isset($_SESSION['username'])) {
$username = $_SESSION['username'];
echo "Welcome back, $username!";
} else {
// If the user is not logged in, show a login form
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Process the login form
$username = $_POST['username'];

// Store the username in the session


$_SESSION['username'] = $username;

echo "Hello, $username! You are now logged in.";


} else {
// Display the login form
echo '
<form method="post" action="">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<button type="submit">Log In</button>
</form>
';
}
}
?>

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.

You might also like