0% found this document useful (0 votes)
6 views6 pages

PHP Basics: Features, Data Types, and More

Uploaded by

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

PHP Basics: Features, Data Types, and More

Uploaded by

asthagundawar01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. What is PHP? Briefly describe its purpose and features.

(8 Marks)

Definition:
PHP (Hypertext Preprocessor) is an open-source, server-side scripting language mainly used to
create dynamic and interactive web pages. It runs on the server and generates HTML output sent to
the client’s browser.

Purpose:
PHP is used for:

 Building dynamic websites.

 Handling forms and user inputs.

 Connecting to databases like MySQL.

 Creating login systems, e-commerce sites, and CMS.

Features:

1. Open Source: Free to download and use.

2. Easy Syntax: Similar to C and Java.

3. Cross-Platform: Works on Windows, Linux, macOS.

4. Database Support: Supports MySQL, PostgreSQL, Oracle, etc.

5. Fast Performance: Executes faster than many scripting languages.

6. Embedded in HTML: Can mix PHP and HTML easily.

7. Secure: Offers data encryption and session management.

8. Large Community: Huge online support and documentation.

2. List and explain three data types in PHP. (8 Marks)

PHP supports several data types. Here are three common ones:

1. String:
A sequence of characters enclosed in quotes.
Example:

2. $name = "Manuja";

3. echo $name;

Output: Manuja

4. Integer:
Whole numbers without decimal points.
Example:

5. $age = 22;
6. echo $age;

Output: 22

7. Array:
A collection of multiple values in a single variable.
Example:

8. $colors = array("Red", "Green", "Blue");

9. echo $colors[0];

Output: Red

3. Write a simple PHP script using a loop to print numbers from 1 to 5. (8 Marks)

Program:

<?php

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

echo $i . "<br>";

?>

Explanation:

 The loop starts at $i = 1 and runs while $i <= 5.

 In each iteration, the value of $i is printed, and then $i increases by 1.


Output:

4. Define a database. What is a table in a database? (8 Marks)

Database:
A database is an organized collection of data stored and managed electronically. It allows users to
store, retrieve, and manage data efficiently using a Database Management System (DBMS) such as
MySQL.

Example:
A student database may contain data about names, roll numbers, and marks.
Table:
A table is a structured format within a database that stores data in rows and columns.

 Each column represents a field (like name, age).

 Each row represents a record (like a single student’s details).

Example Table – Students:

ID Name Age

1 Manuja 22

2 Krish 17

5. Write an SQL command to create a table named Users with fields for id, name, and email. (8
Marks)

SQL Command:

CREATE TABLE Users (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(50),

email VARCHAR(100)

);

Explanation:

 CREATE TABLE – command to create a new table.

 id – integer field, auto-incremented, used as primary key.

 name – variable-length string up to 50 characters.

 email – string field for storing email addresses.

6. What is a cookie? How do you set one in PHP? (8 Marks)

Definition:
A cookie is a small file stored on the user’s computer by the browser. It is used to remember user
information between visits (like username, preferences, etc.).

Setting a Cookie:

<?php

setcookie("username", "Manuja", time() + 3600, "/");

echo "Cookie set successfully!";

?>
Explanation:

 setcookie(name, value, expire, path)

 "username" → name of the cookie.

 "Manuja" → value stored.

 time() + 3600 → expires after 1 hour.

 "/" → available across the whole website.

Accessing a Cookie:

echo $_COOKIE["username"];

7. Describe the steps for handling file uploads in PHP. (8 Marks)

Steps to Handle File Uploads:

1. Create HTML Form:

2. <form action="[Link]" method="post" enctype="multipart/form-data">

3. Select file: <input type="file" name="myfile">

4. <input type="submit" value="Upload">

5. </form>

6. Write PHP Script ([Link]):

7. <?php

8. $target = "uploads/" . basename($_FILES["myfile"]["name"]);

9. if(move_uploaded_file($_FILES["myfile"]["tmp_name"], $target)) {

10. echo "File uploaded successfully!";

11. } else {

12. echo "File upload failed.";

13. }

14. ?>

15. Explanation:

o enctype="multipart/form-data" → allows file uploads.

o $_FILES → superglobal array to access file details.

o move_uploaded_file() → moves file from temporary to target location.

8. What is a session in PHP? How do you start one? (8 Marks)


Definition:
A session is a way to store information (variables) across multiple pages. Unlike cookies, session data
is stored on the server, making it more secure.

Starting a Session:

<?php

session_start(); // Must be called before any output

$_SESSION["username"] = "Manuja";

echo "Session started successfully!";

?>

Accessing Session Data:

echo $_SESSION["username"];

Ending a Session:

session_destroy();

Advantages:

 Stores user data securely.

 Maintains login state.

 Automatically expires after inactivity.

9. What are user privileges in MySQL? Why are they important? (8 Marks)

Definition:
User privileges in MySQL define what actions a user can perform on the database — such as reading,
writing, deleting, or modifying data.

Types of Privileges:

1. SELECT – allows reading data.

2. INSERT – allows adding data.

3. UPDATE – allows modifying data.

4. DELETE – allows removing data.

5. CREATE / DROP – allows creating or deleting tables/databases.

6. GRANT / REVOKE – allows managing privileges.

Example:

GRANT SELECT, INSERT ON mydb.* TO 'manuja'@'localhost' IDENTIFIED BY '1234';

Importance:
 Security: Prevents unauthorized access.

 Control: Assigns specific rights to specific users.

 Data Protection: Avoids accidental deletion or modification.

 Efficient Management: Administrators can control user access levels easily.

Would you like me to format all these answers nicely into a Word file (for printing or submitting)?

You might also like