Writing Your First PHP Script
1️ Introduction
PHP is a server-side scripting
language that allows you to create dynamic web pages. Writing your first PHP
script is simple, and it’s the first step toward building interactive websites.
2️ Setup
Before you begin:
- Make sure Apache is running (via WAMP, XAMPP, or
MAMP).
- Place your PHP files in the correct folder:
- WAMP
→ C:\wamp64\www\
- XAMPP
→ C:\xampp\htdocs\
3️ Create Your
First PHP File
- Open a text editor (VS Code, Sublime Text, or Notepad).
- Create a new file named index.php.
- Add the following code:
<?php
//
This is a single-line comment
echo
"Hello, World!";
?>
4️ Understanding
the Code
- <?php ... ?>
→ Marks the start and end of PHP code.
- echo →
Outputs text to the browser.
- // →
Single-line comment.
5️ Run Your
Script
- Save the file in the correct folder (www or htdocs).
- Open a web browser and navigate to:
http://localhost/index.php
- You should see:
Hello,
World!
6️ Adding HTML
with PHP
You can mix HTML and PHP for dynamic
pages:
<!DOCTYPE
html>
<html>
<head>
<title>My First PHP
Page</title>
</head>
<body>
<h1>
<?php
echo "Welcome to My First PHP
Script!";
?>
</h1>
</body>
</html>
- PHP runs on the server and outputs HTML.
- The browser only sees the result, not the PHP
code.
Tips
for Beginners
✅ Always save files with .php extension.
✅ Start with simple scripts using echo.
✅ Use comments // or /* ... */ to document your code.
✅ Combine PHP with HTML to create dynamic web pages.