PHP PROGRAMMING
UNIT I
Introduction to PHP -Basic Knowledge of websites Introduction of Dynamic
Website -Introduction to PHP -Scope of PHP -XAMPP and WAMP Installation
UNIT II
PHP Programming Basics -Syntax of PHP -Embedding PHP in HTML -
Embedding HTML in PHP. Introduction to PHP Variable -Understanding Data
Types Using Operators -Using Conditional Statements -If(), else if() and else if
condition Statement.
UNIT III
Switch() Statements -Using the while() Loop -Using the for() Loop PHP
Functions. PHP Functions -Creating an Array -Modifying Array Elements -
Processing Arrays with Loops - Grouping Form Selections with Arrays -Using
Array Functions.
UNIT IV
PHP Advanced Concepts -Reading and Writing Files -Reading Data from a
File.
UNIT V
Managing Sessions and Using Session Variables -Destroying a Session -Storing
Data in Cookies -Setting Cookies.
UNIT I
Introduction to PHP
What is PHP?
PHP derived from
Personal Home Page
Tools, now stands for
HypertextPreprocessor.
It is an open-source, server-side scripting language.
PHP is widely used to develop dynamic and interactive web pages.
It is embedded in HTML and executed on the server, not the client’s
browser.
PHP was created by Rasmus Lerdorf in 1994.
PHP is an HTML-embedded Web scripting language. (means
PHP code can be inserted into the HTML of a Web page)
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code is executed on the server, and the result is returned to the
browser as
plain HTML
PHP files have extension ".php"
Example
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>
What Can PHP Do?
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can be used to control user-access
PHP can encrypt data
Why PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP supports a wide range of databases
PHP is free. Download it from the official PHP resource: www.php.net
PHP is easy to learn and runs efficiently on the server side
Features of PHP
Easy to Learn: Simple syntax, similar to C and Java.
Platform Independent: Can run on any OS (Windows, Linux, macOS).
Open Source: Free to use and customize.
Server-Side Execution: Runs on a server and outputs HTML.
Database Connectivity: Easily connects with databases like MySQL,
PostgreSQL, etc.
Supports HTML Embedding: Allows mixing PHP code with HTML
seamlessly.
Cross-platform compatibility: Works with most servers like Apache, IIS
Basic Knowledge of Websites
What is a Website?
A website is a collection of web pages linked together under a common
domain name.
Each web page contains content such as text, images, videos, and links.
Websites are hosted on servers and accessed via browsers.
Types of Websites
1. Static Websites:
oContent remains the same unless manually updated.
oBuilt using HTML and CSS.
oExample: Personal portfolios.
2. Dynamic Websites:
oContent changes dynamically based on user interactions.
oUses languages like PHP, JavaScript, Python, and databases.
oExample: Social media sites like Facebook, e-commerce sites languages
like PHP, ASP.NET, or JSP.
Introduction of Dynamic Website
What is a Dynamic Website?
A dynamic website generates web pages dynamically based on user
input or database data.
The content is not hardcoded; it is fetched from a database or external
sources.
Key Features of Dynamic Websites:
1. Interactive and user-friendly.
2. Content changes dynamically without reloading the entire page.
3. Uses server-side languages like PHP, ASP.NET, or JSP.
4. Integrates databases (e.g., MySQL, PostgreSQL).
Examples:
Social Networking (Facebook, Twitter).
Online Shopping (Amazon, Flipkart).
News Portals (BBC, CNN).
Scope of PHP
PHP is widely used in web development due to its flexibility and
simplicity.
1. Areas of Use
Web Applications: Create dynamic websites and web portals.
Content Management Systems (CMS): WordPress, Joomla, and Drupal
are built on PHP.
E-Commerce Platforms: Develop shopping sites using PHP frameworks
(Magento, OpenCart).
Web Services: APIs and RESTful services.
Server-Side Scripting: Backend for web applications.
2. Why PHP is Important
PHP is free and widely supported.
Millions of websites and apps rely on PHP.
Career opportunities in web development.
Integrates seamlessly with popular databases like MySQL
XAMPP and WAMP Installation
1. What are XAMPP and WAMP?
Both XAMPP and WAMP are local server environments that allow PHP and
MySQL development on your PC
2. Installing XAMPP
1. Download XAMPP from the official site
(https://www.apachefriends.org/).
2. Run the Installer: Follow the installation steps and choose components:
Apache, MySQL, and PHP.
3. Start Services:
Open the XAMPP Control Panel and start Apache and MySQL.
4. Access Local Server:
Open a browser and enter http://localhost or http://127.0.0.1.
5. Check PHP Setup:
Create a file test.php inside the htdocs folder.
Write:
<?php
echo "Hello, PHP!";
?
Access the file in the browser: http://localhost/test.php.
3. Installing WAMP
1. Download WAMP from the official site
(http://www.wampserver.com/).
2. Run the Installer: Follow the prompts to install.
3. Start Services: Launch WAMP and ensure the server icon in the system
tray turns green.
4. Access Local Server: Open http://localhost in your browser.
5. Test PHP: Create a test file test.php in the www folder.
Add the same PHP code and access it via http://localhost/test.php
4. Differences Between XAMPP and WAMP
Platform: XAMPP supports multiple OS, WAMP is Windows-specific.
Features: XAMPP includes Perl and FTP server, whereas WAMP is
minimalistic.
Performance: WAMP is lightweight compared to XAMPP.
5. Testing PHP Installation
Use the phpinfo() function to verify PHP setup:
<?php
phpinfo();
?>
Access the file and check PHP version, configuration, and extensions.
1 MARK
1. What does PHP stand for?
A) Private Home Page
B) Personal Hypertext Processor
C) PHP: Hypertext Preprocessor
D) Professional Home Page
Answer: C) PHP: Hypertext Preprocessor
2. Which symbol is used to start a PHP code block?
A) <?php
B) <php>
C) {php}
D) <?PHP?>
Answer: A) <?php
3. How do you end a PHP statement?
A) Period .
B) Semicolon ;
C) Colon :
D) Comma ,
Answer: B) Semicolon ;
4. How can you embed PHP code in HTML?
A) <php>echo "Hello";</php>
B) <?php echo "Hello"; ?>
C) {php echo "Hello"; }
D) echo "<php>Hello</php>";
Answer: B) <?php echo "Hello"; ?>
5. Which is the correct way to declare a variable in PHP?
A) $variableName
B) var variableName
C) @variableName
D) #variableName
Answer: A) $variableName
6. How do you write a comment in PHP?
A) // This is a comment
B) <!-- This is a comment -->
C) # This is a comment
D) Both A and C
Answer: D) Both A and C
7. Which of the following is not a valid PHP variable name?
A) $myVar
B) $_myVar
C) $My-Var
D) $my_var
Answer: C) $My-Var
8. PHP scripts are usually executed on which side?
A) Client-side
B) Server-side
C) Both client and server-side
D) None
Answer: B) Server-side
9. How can you output text in PHP?
A) write("Hello");
B) echo "Hello";
C) document.write("Hello");
D) response.write("Hello");
Answer: B) echo "Hello";
10. What is the correct way to include HTML inside a PHP script?
A) <?php echo "<h1>Hello</h1>"; ?>
B) <?php <h1>Hello</h1>; ?>
C) echo {<h1>Hello</h1>};
D) <h1><?php echo 'Hello'; ?></h1>
Answer: A) <?php echo "<h1>Hello</h1>"; ?>
11. Which of the following PHP tags is deprecated?
A) <?php ?>
B) <? ?>
C) <script language="php">
D) <?php include ?>
Answer: B) <? ?>
12. What is the correct way to start a session in PHP?
A) start_session()
B) session_start()
C) begin_session()
D) session_begin()
Answer: B) session_start()
13. How do you access elements in an associative array in PHP?
A) $array[0]
B) $array{"key"}
C) $array['key']
D) $array{key}
Answer: C) $array['key']
14. How do you create a PHP function?
A) def functionName() {}
B) function functionName() {}
C) fn functionName() {}
D) create functionName() {}
Answer: B) function functionName() {}
15. How can you include the content of one PHP file into another PHP file?
A) import 'filename.php';
B) include 'filename.php';
C) load 'filename.php';
D) require 'filename.php'; Answer: B) include 'filename.php';
16. What is the correct way to write an "if" statement in PHP?
A) if condition {}
B) if (condition) {}
C) if: condition {}
D) if -> condition {}
Answer: B) if (condition) {}
17. Which of the following is the correct way to write a PHP for loop?
A) for $i = 1; $i <= 10; $i++
B) for (int $i = 1; $i <= 10; $i++)
C) for ($i = 1; $i <= 10; $i++)
D) for i in range(1, 10)
Answer: C) for ($i = 1; $i <= 10; $i++)
18. How do you concatenate two strings in PHP?
A) +
B) &
C) ++
D) .
Answer: D) .
19. Which of the following is the correct way to open a file in PHP?
A) open(filename, mode)
B) fopen(filename, mode)
C) file_open(filename, mode)
D) open_file(filename, mode)
Answer: B) fopen(filename, mode)
20. How can you embed PHP variables into an HTML string?
A) echo "<p>$variable</p>";
B) echo "<p>{$variable}</p>";
C) Both A and B
D) None
Answer: C) Both A and B
21. What is the correct syntax to embed an HTML form inside PHP code?
A) echo '<form></form>';
B) echo "<form></form>";
C) Both A and B
D) You cannot embed an HTML form inside PHP code
Answer: C) Both A and B
22. Which is the correct way to define a constant in PHP?
A) constant("NAME", "value");
B) define("NAME", "value");
C) const NAME = "value";
D) Both B and C
Answer: D) Both B and C
23. How do you retrieve form data sent via POST in PHP?
A) $_POST['name']
B) $_GET['name']
C) $_FORM['name']
D) $_REQUEST['name']
Answer: A) $_POST['name']
24. What is the default file extension for PHP files?
A) .html
B) .php
C) .phtml
D) .htaccess
Answer: B) .php
25. Can PHP variables contain HTML tags?
A) Yes, they can.
B) No, they cannot.
C) Only when escaped.
D) Only if declared as global.
Answer: A) Yes, they can.
26.What does PHP stand for?
A) Personal Home Page
B) Hypertext Preprocessor
C) Preprocessor Hypertext Page
D) Pre Hypertext Processor
Answer: B) Hypertext Preprocessor
27.Which of the following is NOT a feature of dynamic websites?
A) User interaction
B) Static content
C) Database integration
D) Real-time data processing
Answer: B) Static content
28.What is PHP mainly used for?
A) Client-side scripting
B) Server-side scripting
C) Database management
D) Image editing
Answer: B) Server-side scripting
29.Which of the following is NOT an advantage of PHP?
A) Open-source
B) Cross-platform
C) High memory usage
D) Easy to learn
Answer: C) High memory usage
30.What is the primary difference between static and dynamic websites?
A) Dynamic websites do not require databases
B) Static websites have fixed content
C) Dynamic websites are easier to maintain
D) Static websites are more interactive
Answer: B) Static websites have fixed content
31.Which combination of software is used for PHP development on Windows?
A) XAMPP
B) WAMP
C) Both A and B
D) MAMP
Answer: C) Both A and B
32.What does WAMP stand for?
A) Windows Apache MySQL PHP
B) Web Access MySQL PHP
C) Web Apache Module PHP
D) Windows Application Module PHP
Answer: A) Windows Apache MySQL PHP
33.What is the default file extension for PHP files?
A) .html
B) .php
C) .txt
D) .js
Answer: B) .php
34.Which of the following can PHP NOT do?
A) Create and manipulate databases
B) Generate dynamic page content
C) Send emails
D) Control client-side behavior like JavaScript
Answer: D) Control client-side behavior like JavaScript
35.What is XAMPP?
A) An operating system
B) A web server software package
C) A scripting language
D) A database management system
Answer: B) A web server software package
36.Which one of the following is NOT part of XAMPP?
A) Apache
B) MySQL
C) PHP
D) Python
Answer: D) Python
37.What is the default server software used by XAMPP and WAMP?
A) Nginx
B) IIS
C) Apache
D) Tomcat
Answer: C) Apache
38.Which of the following PHP versions introduced the Zend Engine?
A) PHP 3
B) PHP 4
C) PHP 5
D) PHP 6
Answer: B) PHP 4
39.How do you declare a variable in PHP?
A) var $variable;
B) $variable;
C) declare $variable;
D) set $variable;
Answer: B) $variable;
40.What scope does a variable declared within a function in PHP have?
A) Global
B) Local
C) Static
D) Universal
Answer: B) Local
41.How can you embed PHP code in HTML?
A) <script></script>
B) <php></php>
C) <?php ?>
D) <code></code>
Answer: C) <?php ?>
42.Which function is used to connect to a MySQL database in PHP?
A) connect()
B) mysqli_connect()
C) mysql_connect()
D) db_connect()
Answer: B) mysqli_connect()
43.In XAMPP, which folder is used to store PHP files?
A) htdocs
B) public_html
C) www
D) var
Answer: A) htdocs
44.What is the main use of phpMyAdmin in XAMPP/WAMP?
A) To manage files
B) To manage databases
C) To manage web servers
D) To create dynamic pages
Answer: B) To manage databases
45.Which of the following is NOT a PHP data type?
A) String
B) Array
C) Float
D) Character
Answer: D) Character
46.How do you write a comment in PHP?
A) //comment
B) /* comment */
C) #comment
D) All of the above
Answer: D) All of the above
47.What is the output of this PHP code: echo 2 + 3 * 2;?
A) 10
B) 8
C) 12
D) 16
Answer: B) 8
48.Which version of PHP is the minimum recommended for new projects in
2024?
A) PHP 7
B) PHP 5.6
C) PHP 8
D) PHP 4
Answer: C) PHP 8
49.What is the command to start Apache in XAMPP?
A) Start Server
B) Run Apache
C) Start Apache
D) Click Start next to Apache
Answer: D) Click Start next to Apache
50.What is the scope of PHP variables declared outside any function?
A) Local
B) Static
C) Global
D) Universal
Answer: C) Global
5 MARK
1.Explain about introduction to php.
2.Explain about dynamic web pages.
3.Explain about scope of php.
4.Explain about advantages of php.
5.Explain about disadvantages of php
10 MARK
1.Explain about basics of web sites.
2.Explain about dynamic websites.
3.Explain about XAMPP installation.
4.Explain about WAMP Installation.
UNIT-II