php assignment
php assignment
Setup Procedure:
1. Download notepad++ and install it
2. Get a browser ready (chrome)
3. Download XAMPP from apache.org and install it
4. Open XAMPP control panel
5. Click on start apache and MySQL
6. Navigate to pc c: drive xampp folder htdocs create your folder
C. Differentiate between an interpreter and a compiler, then state with reason which category
PHP programming language belongs
An interpreter translates high-level programming code into machine code line-by-line at runtime, executing
each command immediately. In contrast, a compiler translates the entire code into machine code before
execution, creating an executable file that can be run independently.
PHP is categorized as an interpreter language because it executes scripts on the server side using a PHP
interpreter, processing the code at runtime rather than compiling it beforehand. This allows for dynamic web
content generation and easier debugging during development.
Q2. Consider to have a HTML form containing some codes for input fields and a submit button. This
code creates a modal login form with two input fields for the username and password. The form will
send data to the “login.php” file using post method when the user clicks the login button.
A. Create the “login.php” script
<?php
If ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$username = $_POST[“username”];
$password = $_POST[“password”];
If (validate_credentials($username, $password)) {
Session_start();
$_SESSION[“username”] = $username;
Header(“Location: dashboard.php”);
} else {
Echo “Invalid username or password.”;
}
}
?>
B. Open a connection with a database use test_db.sql as your database
<?php
$servername = “localhost”;
$username = “your_username”;
$password = “your_password”;
$dbname = “test_db”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
If ($conn->connect_error) {
Die(“Connection failed: “ . $conn->connect_error);
}
?>
C. Describe the procedure of creating the database (test_db.sql)in PhpmyAdmin
* Log in to phpMyAdmin.
* Click the “Databases” tab.
* Click the “Create database” button.
* Enter the database name (e.g., “test_db”).
* Click the “Create” button.
Q3
A. What is PHP Session? Explain its Importance.
A PHP session is a mechanism for storing user data across multiple pages during a user's visit to a website.
Unlike cookies, which store data on the client-side, session data is stored on the server and is associated with
a unique session ID. This allows the server to maintain state and track user activity, making it essential for
functionalities like user authentication, shopping carts, and personalized experiences.
Importance of PHP Sessions
State Management: Sessions help maintain user state in stateless HTTP protocol.
Security: Sensitive information is stored on the server rather than on the client side.
Data Persistence: Users can navigate through different pages without losing their data.
B. Create a PHP Script for the Start of a Session
<?php
// Start the session
session_start();
// Set session variables
$_SESSION["username"] = "JohnDoe";
$_SESSION["role"] = "admin";
<?php
// Start the session
session_start();
// If it's desired to kill the session, also delete the session cookie.
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
Built-in Functions: These are pre-defined functions provided by PHP that perform specific tasks. Examples
include:
- `strlen()` for string length.
- `array_push()` for adding elements to an array.
- `date()` for formatting dates.
User-defined Functions: These are functions created by the programmer to perform specific tasks that are
not covered by built-in functions. They allow customization based on the application’s requirements.
<?php
function displayMsg() {
echo "Hello World!";
}
?>
B. ii. Call the Function in (bi) Above
<?php
// Call the function
displayMsg();
?>
Q5A. Explain the concept of Object-Oriented Programming (OOP) and state (5) of its advantages
over procedural programming
OOP is a programming paradigm that models real-world entities as objects, which have properties
(attributes) and behaviors (methods).
Advantages:
1. Modularity: Encapsulates data and behavior within objects.
2. Reusability: Can reuse code by creating classes and objects.
3. Maintainability: Easier to maintain and modify code.
4. Extensibility: Can easily add new features by creating new classes.
5. Flexibility: Can create complex and dynamic applications.
B. Create a PHP script which declare a class named “Fruit” consisting of two properties ($name and
$color) and two methods set_name() and get_name() for setting and getting the $name property
class Fruit {
public $name;
public $color;
Try {
$result = divide(5, 0);
Echo $result;
} catch (Exception $e) {
Echo “Error: “ . $e->getMessage();
}
Q7.
A. Show how PHP handles file by doing the following:
i. Create a File
<?php
$file = fopen(“example\.txt”, “w”);
?>
v. Append to a File
<?php
$file = fopen(“example\.txt”, “a”);
Fwrite($file, “Appending this text\.”);
Fclose($file);
?>
vi. Delete a File