Introduction to PHP
PHP is a server-side scripting language used to create dynamic websites and web applications. It can process form submissions, work with databases, manage user sessions, and generate HTML content.
PHP code typically runs on a web server. Unlike a simple HTML file, a PHP file usually needs a PHP interpreter to execute its code. Introduction to PHP and Environment Setup Practice Question with Solutions to help you build concepts.
Question 1: Write Your First PHP Program
Problem Statement: Create a PHP program that displays a welcome message.
PHP Solution
<?php
echo "Welcome to PHP Programming!";
?>
Sample Output
Welcome to PHP Programming!
Explanation
<?phpstarts the PHP code.echodisplays text in the output.- The text inside double quotation marks is the message.
- The semicolon
;marks the end of the statement. ?>closes the PHP block. It is commonly omitted in PHP-only files.
Save the file as question1.php and open http://localhost/php_practice/question1.php in your browser.
Question 2: Display Your Name Using PHP
Problem Statement: Write a PHP program that displays a student’s name.
PHP Solution
<?php
echo "My name is Rahul.";
?>
Sample Output
My name is Rahul.
Explanation
The echo statement displays the supplied text. In this example, the program prints a student’s name. You can replace Rahul with any name you want.
Question 3: Display Multiple Lines of Text
Problem Statement: Create a PHP program that displays a student’s name, course and institute on separate lines in a browser.
PHP Solution
<?php
echo "Name: Rahul<br>";
echo "Course: PHP Programming<br>";
echo "Institute: VSIT Coding Institute";
?>
Sample Output
Name: Rahul
Course: PHP Programming
Institute: VSIT Coding Institute
Explanation
- Each
echostatement displays a line of text. - The HTML tag
<br>creates a line break in the browser. - PHP can generate HTML that the browser displays.
Question 4: Display a Heading Using PHP
Problem Statement: Write a PHP program that displays a large heading on a webpage.
PHP Solution
<?php
echo "<h1>Learn PHP from Zero to Hero</h1>";
?>
Sample Output
Learn PHP from Zero to Hero
Explanation
The echo statement sends an HTML heading to the browser. The <h1> tag represents the main heading of a webpage.
PHP can generate HTML elements, including headings, paragraphs, lists and tables.
Question 5: Create a PHP Welcome Page with HTML
Problem Statement: Create a webpage that displays a heading and a paragraph using both HTML and PHP.
PHP Solution
<!DOCTYPE html>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<?php
echo "<p>This webpage uses PHP.</p>";
?>
</body>
</html>
Sample Output
Welcome to My Website
This webpage uses PHP.
Explanation
This example combines HTML and PHP in one file.
- HTML provides the webpage structure.
- The PHP block executes on the server.
- The
echostatement generates a paragraph. - The browser displays the resulting HTML.
Save the complete code as question5.php.
Question 6: Display a Number Using PHP
Problem Statement: Write a PHP program that displays the number 100.
PHP Solution
<?php
echo 100;
?>
Sample Output
100
Explanation
The echo statement can display numbers directly, without quotation marks. Here, 100 is an integer value.
Compare these two statements:
echo 100;
echo "100";
Both display 100, but the first uses a number while the second uses a string.
Question 7: Check Whether PHP Is Working
Problem Statement: Write a PHP program that confirms the PHP interpreter is executing code.
PHP Solution
<?php
echo "PHP is working correctly!";
?>
Sample Output
PHP is working correctly!
Explanation
If the message appears when you visit the PHP file through http://localhost/, the PHP interpreter is processing your program.
If you see the PHP source code instead of the output, check that Apache is running and that you opened the file through localhost, rather than opening it directly from your computer.
Question 8: Display PHP and HTML Together
Problem Statement: Create a webpage with an HTML heading, a PHP-generated paragraph and another HTML paragraph.
PHP Solution
<!DOCTYPE html>
<html>
<body>
<h1>PHP Practice</h1>
<?php
echo "<p>Today I am learning PHP.</p>";
?>
<p>I will practice PHP every day.</p>
</body>
</html>
Sample Output
PHP Practice
Today I am learning PHP.
I will practice PHP every day.
Explanation
HTML and PHP can work together in the same file. HTML creates the first and last paragraphs, while PHP generates the middle paragraph.
This technique is widely used in PHP-powered websites.
Question 9: Write a PHP Program with Two PHP Blocks
Problem Statement: Write a program that uses two separate PHP blocks to display two messages.
PHP Solution
<?php
echo "First PHP block<br>";
?>
<p>This is an HTML paragraph.</p>
<?php
echo "Second PHP block";
?>
Sample Output
First PHP block
This is an HTML paragraph.
Second PHP block
Explanation
A PHP file can contain multiple PHP blocks separated by HTML. The server executes each PHP block and sends the resulting HTML to the browser.
This is useful when PHP-generated content needs to appear in different parts of a webpage.
Question 10: Display a Simple Student Profile
Problem Statement: Create a PHP webpage displaying a student’s name, course, learning level and institute.
PHP Solution
<!DOCTYPE html>
<html>
<head>
<title>Student Profile</title>
</head>
<body>
<h1>Student Profile</h1>
<?php
echo "<p>Name: Rahul</p>";
echo "<p>Course: PHP</p>";
echo "<p>Level: Beginner</p>";
echo "<p>Institute: VSIT Coding Institute</p>";
?>
</body>
</html>
Sample Output
Student Profile
Name: Rahul
Course: PHP
Level: Beginner
Institute: VSIT Coding Institute
Explanation
This example combines several concepts from the chapter:
- HTML creates the page structure.
- PHP executes on the server.
- Multiple
echostatements generate different paragraphs. - The browser displays the generated webpage.
You can change the profile details to create a different student profile.
Key Takeaways
- PHP is a server-side scripting language used to build dynamic websites.
- PHP code is commonly saved in files ending with
.php. - XAMPP provides a local environment for running PHP on Windows.
- The
echostatement displays text, numbers and HTML. - PHP can be combined with HTML in the same file.
- The browser receives the generated output rather than executing the PHP source itself.
- PHP files should be accessed through a configured PHP server, such as
localhost.
Frequently Asked Questions
1. What is PHP used for?
PHP is used to create dynamic websites, process forms, connect to databases, manage sessions and build web applications.
2. Is PHP easy for beginners?
Yes. Beginners can start with simple output statements and gradually learn variables, conditions, loops, functions and database operations.
3. Is PHP free to use?
Yes. PHP is open-source and can be used without paying a licensing fee.
4. Can I run PHP without XAMPP?
Yes. You can install PHP separately and use its built-in development server. XAMPP is simply a convenient option for beginners.
5. What is the difference between HTML and PHP?
HTML defines webpage structure, while PHP executes on the server and can generate dynamic HTML content.
6. Why is my PHP code showing instead of running?
You may be opening the file directly rather than through a PHP-enabled server, or your server configuration may be incorrect.
7. Which software is best for writing PHP code?
Visual Studio Code is a popular choice. Other editors can also work, provided you save your code in .php files.
8. Can I use PHP with MySQL?
Yes. PHP can connect to MySQL databases using extensions such as PDO and MySQLi.
9. Do I need to learn HTML before PHP?
Basic HTML knowledge is helpful because PHP is often used to generate webpage content, but you can begin learning PHP without mastering HTML first.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
