QB UT2 Solution
QB UT2 Solution
9. What is super global variables? List it and write the use of it.
Superglobals are built-in PHP variables that are always accessible, regardless of scope.
List of Superglobal Variables:
1. $_GET – Retrieves data sent via GET method.
2. $_POST – Retrieves data sent via POST method.
3. $_REQUEST – Combines both $_GET and $_POST.
4. $_SESSION – Stores session variables.
5. $_COOKIE – Stores cookies.
6. $_SERVER – Provides server and execution environment information.
7. $_FILES – Handles file uploads.
8. $_ENV – Stores environment variables.
Example: Using $_SERVER to get server details
echo $_SERVER['PHP_SELF']; // Outputs the current script name
echo $_SERVER['SERVER_NAME']; // Outputs the server name
10. Write syntax to create a class and object in PHP.
A class is defined by using the class keyword, followed by the name of the class and a pair of curly
braces ({}). All its properties and methods go inside the braces. An object is an instance of a class.
Syntax:
class class_name
{
//Block of Code
}
$object = new class_name();
Example:
class Car {
public $brand;
public function setBrand($brand) {
$this->brand = $brand;
}
public function getBrand() {
return $this->brand;
}
}
$car1 = new Car();
$car1->setBrand("Toyota");
echo $car1->getBrand();
Once data is inserted into a database, we often need to modify or remove records. The UPDATE
and DELETE queries in SQL allow us to make these changes.
14. State and explain any four form controls to get users input in PHP.
Text Field: A text input field allows the user to enter a single line of text. Textbox field
enable the user to input text information to be used by the program.
Text Area: A text area field is similar to a text input field but it allows the user to enter
multiple line of text.
Radio Button (<input type="radio">) – Selects one option from multiple choices.
Dropdown (<select>) – Presents a list of options.
File Upload (<input type="file">) – Uploads files.
Example:
<form method="post" enctype="multipart/form-data">
Name: <input type="text" name="name"><br>
Gender: <input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female<br>
Country: <select name="country">
<option value="India">India</option>
<option value="USA">USA</option>
</select><br>
Upload Resume: <input type="file" name="resume"><br>
<input type="submit">
</form>
15. Describe i) Start session ii) Get session variables iii) Modify session variables iv)
Delete session
What is a Session in PHP?
A session in PHP is used to store user information (like login credentials, shopping cart
items, or preferences) across multiple pages.
Unlike cookies, session data is stored on the server, making it more secure.
Steps to Work with Sessions:
o Start a session → session_start();
o Store session variables → $_SESSION['key'] = 'value';
o Retrieve session variables → $_SESSION['key'];
o Modify session variables → Change the value of $_SESSION['key']
o Destroy a session → session_unset(); session_destroy();
Explanation:
$_SESSION["username"] is changed from "Abc" to "Xyz". Any subsequent page using this
session variable will now display "Xyz".
iv) Delete (Destroy) a Session
When a session is no longer needed, it can be cleared or completely destroyed.
1. Remove a Specific Session Variable
<?php
session_start();
unset($_SESSION["username"]); // Remove only "username" session variable
echo "Username session variable has been removed.";
?>
2. Destroy the Entire Session
<?php
session_start();
session_unset(); // Unset all session variables
session_destroy(); // Completely destroy the session
echo "Session has been destroyed.";
?>
PHP provides a built-in function called mail() to send emails directly from a server.
Additionally, external libraries like PHPMailer can be used for advanced features such as
attachments, authentication, and sending emails via SMTP (Simple Mail Transfer
Protocol).
The mail() function is the simplest way to send emails in PHP. It takes the following
parameters: mail(to, subject, message, headers, parameters);
Example: Sending a Basic Email
<?php
$to = "[email protected]"; // Recipient's email
$subject = "Welcome to PHP Mail!";
$message = "Hello, this is a test email sent from PHP.";
$headers = "From: [email protected]";
if(mail($to, $subject, $message, $headers)) {
echo "Email successfully sent to $to";
} else {
echo "Email sending failed.";
}
?>
$to → Recipient's email address.
$subject → The subject of the email.
$message → The body of the email.
$headers → The sender's email (From field).
mail() → Sends the email and returns true if successful, otherwise false.
Limitations of mail():
o Requires a proper mail server setup on the hosting server.
o No authentication (SMTP authentication is not supported).
o Emails may land in the spam folder.
To send an email with attachments, use addAttachment() in PHPMailer.
$mail->isHTML(true);
$mail->Subject = 'HTML Email Example';
$mail->Body = '<h1>Hello!</h1><p>This is an HTML email.</p>';
$mail->send();
Sending Emails with CC & BCC
o CC (Carbon Copy): Sends a copy of the email to additional recipients.
o BCC (Blind Carbon Copy): Sends a copy without showing the recipients.
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->send();
Note: Gmail and other providers require App Passwords instead of real passwords for security.
18. Explain what a cookie is. How can we create, modify, and destroy cookies?
A cookie is a small piece of data that a web server stores on the client’s browser.
It is used to remember user preferences, login information, or track user behavior.
Cookies in PHP are stored as key-value pairs and can persist even after the user closes
the browser (depending on expiration time).
Creating a Cookie: We use the setcookie() function to create a cookie in PHP.
Syntax: setcookie(name, value, expire, path, domain, secure, httponly);
Parameter Description
name Name of the cookie (string)
value Value to store in the cookie
expire Expiration time (timestamp in seconds)
path URL path where cookie is accessible (default: "/")
domain The domain where the cookie is available
secure true if cookie should be sent only over HTTPS
httponly true if cookie should be accessible only via HTTP (not JavaScript)
Retrieving (Reading) a Cookie: Once a cookie is set, it can be accessed using the
$_COOKIE superglobal array.
Modifying a Cookie: To modify a cookie, simply use setcookie() with the same name but
a new value.
Deleting (Destroying) a Cookie in PHP: To delete a cookie, set its expiration time to a past
date using setcookie().
Example:
<?php
// Step 1: Set a cookie
setcookie("user", "abc", time() + (86400 * 7), "/");
Output:
User: abc
Cookie updated!
Cookie deleted!