
Chapter 01: Your First PHP Script
Overview
With your development environment ready, it's time to write your first line of PHP code. This is a rite of passage in programming: the "Hello, World!" script. It's simple, but it's the perfect way to confirm that your setup is working and to learn the most basic syntax of the language.
In this chapter, you'll learn what PHP tags look like, how to output text, and the different ways you can run a PHP script.
Prerequisites
Before starting this chapter, you should have:
- PHP 8.4+ installed and confirmed working with
php --version - A code editor (VS Code recommended) installed
- A terminal or command prompt ready
- A project directory named
php-from-scratchfrom the previous chapter - Basic familiarity with navigating directories in the terminal
Estimated Time: ~15 minutes
What You'll Build
By the end of this chapter, you will have created:
hello.php— A simple script that outputs "Hello, World!" to the terminalhello-web.php— An HTML page with embedded PHP that displays dynamic contentconcatenation.php— A script demonstrating string concatenation and the.operator- Working knowledge of PHP syntax fundamentals (tags, echo, comments, concatenation)
- Understanding of when to use closing PHP tags and short echo syntax
- Experience running PHP scripts both from the command line and in a browser
Objectives
- Understand the purpose of PHP tags (
<?php ... ?>) and when to use closing tags. - Use the
echostatement and short echo syntax (<?=) to output strings. - Run a PHP script from both the command line and a web browser.
- Learn how to add comments to your code.
- Master string concatenation with the
.operator. - Distinguish between concatenation and string interpolation.
Step 1: Create a "Hello, World!" Script (~2 min)
Goal
Create your first PHP file and print a message to confirm your environment works.
Actions
- Navigate to your project directory: In your terminal, move into the
php-from-scratchfolder you created earlier. - Create a new file: Inside that directory, create a file named
hello.php. You can delete the previousindex.phpif you no longer need it. - Add the PHP code: Open
hello.phpin your editor and add the following:
# filename: hello.php
<?php
echo 'Hello, World!';Expected Result
You now have a hello.php file that outputs a classic greeting when executed.
Why It Works
<?phptells PHP where code execution begins.echooutputs the provided string to the terminal or browser.'Hello, World!'is a string literal containing the text to display.- The trailing semicolon (
;) ends the statement so PHP knows it’s complete.
Troubleshooting
- Error:
syntax error, unexpected '$name'— Make sure the file starts with<?php. - Error:
Undefined function 'echo'—echois a language construct, not a function; double-check you typed it correctly. - Blank file created — Ensure the code snippet was saved in
hello.phpbefore moving on.
Step 2: Run the Script from the Command Line (~1 min)
Goal
Execute a PHP file from the terminal to verify that the interpreter is installed correctly.
Actions
- Stay in your project directory: Confirm you’re still inside
php-from-scratch. - Run the script:
# Execute the PHP script
php hello.phpExpected Result
Your terminal prints:
Hello, World!Tip: If the prompt appears on the same line, append
PHP_EOLto your echo statement to add a newline.
Why It Works
- The
phpCLI executes the file using the installed PHP interpreter. - PHP reads
hello.php, runs the statements, and sends the output to STDOUT.
Troubleshooting
php: command not found— PHP isn’t installed or isn’t in your PATH. Verify withphp --versionand revisit Chapter 00 if needed.Could not open input file: hello.php— You’re in the wrong directory or the file is named differently. Usels/dirto confirm.- No output — Ensure your script matches the previous step and includes the
echostatement.
Step 3: Run the Script in the Browser (~2 min)
Goal
Serve the script through PHP’s built-in web server so you can view output in the browser.
Actions
- Start the development server:
# Start the built-in PHP development server
php -S localhost:8000You should see something like:
[Sat Oct 25 14:30:00 2025] PHP 8.4.0 Development Server (http://localhost:8000) started- Open the script in a browser: Visit
http://localhost:8000/hello.php.
Expected Result
The browser displays “Hello, World!” exactly as the terminal did, confirming PHP is executing on the server and returning HTML to the client.
Why It Works
php -Sspins up a lightweight web server ideal for local development.- When the browser requests
/hello.php, PHP executes the script and returns the rendered HTML response.
Troubleshooting
Failed to listen on localhost:8000— Another process is using the port. Tryphp -S localhost:8001and adjust the URL accordingly.- “This site can’t be reached” — Ensure the server is still running in your terminal and that the URL includes
http://. - Blank page — Check the terminal running the server for syntax errors or warnings; view the page source to confirm any HTML output.
Step 4: Embedding PHP in HTML (~3 min)
Goal
Mix PHP with HTML to render dynamic content in a traditional web page structure.
Actions
- Create a new file: Name it
hello-web.php. - Add the following HTML/PHP hybrid code:
# filename: hello-web.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First PHP Page</title>
</head>
<body>
<h1><?php echo 'Hello from the Web!'; ?></h1>
<p>This page was generated on <?php echo date('Y-m-d H:i:s'); ?>.</p>
</body>
</html>Note:
date('Y-m-d H:i:s')returns a formatted timestamp such as2025-10-25 14:30:45.
- View the file: With the dev server running, open
http://localhost:8000/hello-web.php.
Expected Result
The browser shows an HTML page titled “My First PHP Page” with a heading and a timestamp that reflects the current date and time.
Why It Works
- PHP executes on the server and outputs plain HTML for the browser to render.
- Embedding code inside
<?php ... ?>blocks lets you insert dynamic values anywhere in the markup.
Troubleshooting
- Raw PHP printed in the browser — Ensure the file has a
.phpextension and the server is serving it via PHP. - Date always the same — You’re likely viewing a cached version. Refresh the page or disable caching in your browser.
Step 5: Adding Comments (~1 min)
Goal
Learn how to document code using PHP’s comment syntax.
Actions
- Create an optional demo file with comment examples:
# filename: comments-demo.php
<?php
// This is a single-line comment. It's great for short notes.
/*
This is a multi-line comment block.
You can write as many lines as you want here.
Useful for longer explanations or documentation.
*/
# This is also a single-line comment, but it's less common.
# It's borrowed from shell scripting syntax.
echo 'Hello with comments!'; // Comments can also be placed at the end of a line.- Run the file if you’d like to confirm the comments are ignored when the script executes.
Expected Result
The script outputs Hello with comments! and nothing else, proving comments have no effect on runtime behavior.
Why It Works
//and#mark single-line comments that PHP skips when executing./* ... */wraps multi-line comments, perfect for longer explanations or temporarily disabling blocks of code.
Troubleshooting
- Unexpected output — Make sure echo statements aren’t inside comment blocks.
- Syntax error near
*/— Ensure multi-line comments start with/*and end with a matching*/.
Step 6: PHP Tags and String Operations (~3 min)
Goal
Understand PHP tag usage and practice essential string-output techniques.
Actions
- Review closing tag best practices:
- Omit
?>in pure PHP files to avoid stray whitespace issues. - Include
?>only when mixing PHP and HTML in the same file, as shown below:
- Omit
# filename: mixed-example.php
<?php
$greeting = 'Welcome';
?>
<!DOCTYPE html>
<html>
<head>
<title>Mixed PHP and HTML</title>
</head>
<body>
<h1><?php echo $greeting; ?></h1>
<p>This HTML comes after the closing PHP tag.</p>
</body>
</html>- Experiment with short echo tags to simplify templates:
# filename: short-echo-demo.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Short Echo Demo</title>
</head>
<body>
<?php $title = 'My Awesome Page'; ?>
<?php $author = 'Dale Hurley'; ?>
<!-- Long form -->
<h1><?php echo $title; ?></h1>
<!-- Short form (cleaner) -->
<h2><?= $author ?></h2>
<p>Current time: <?= date('H:i:s') ?></p>
</body>
</html>- Practice concatenation and interpolation:
# filename: concatenation.php
<?php
// Basic concatenation
echo 'Hello' . ' ' . 'World!';
echo PHP_EOL;
// Concatenating variables and strings
$firstName = 'Jane';
$lastName = 'Doe';
$fullName = $firstName . ' ' . $lastName;
echo 'Full name: ' . $fullName;
echo PHP_EOL;
// You can also use the .= operator to append to a string
$message = 'Hello';
$message .= ' World';
$message .= '!';
echo $message; // Outputs: Hello World!# filename: string-comparison.php
<?php
$name = 'Alice';
$age = 25;
// Concatenation
echo 'Name: ' . $name . ', Age: ' . $age . PHP_EOL;
// String interpolation (double quotes only)
echo "Name: $name, Age: $age" . PHP_EOL;
// Complex expressions often read better with concatenation
echo 'Next year, ' . $name . ' will be ' . ($age + 1) . ' years old.' . PHP_EOL;- Run the concatenation example:
php concatenation.phpExpected Result
Hello World!
Full name: Jane Doe
Hello World!Why It Works
- Omitting
?>prevents accidental whitespace that can break HTTP headers. - The short echo tag (
<?=) is shorthand for<?php echo ... ?>and is always enabled in PHP 8.4. - The dot operator (
.) concatenates strings;.=appends to an existing string variable. - Interpolation within double quotes offers concise syntax for embedding variables.
Troubleshooting
<?=causes a parse error — You’re likely executing the script via CLI without HTML context. Use standardechoin pure PHP scripts.- Unexpected concatenation results — Add explicit spaces (
'Hello' . ' ' . 'World') to avoid merged words. - Output shows literal
$variable— Interpolation requires double quotes. Use"for interpolation and'for literal strings.
Exercises
Test your understanding with these hands-on challenges:
Modify the Message: Change your
hello.phpscript to print "Hello, [Your Name]!" instead using string concatenation. Run it from the command line to verify.Display the Date: Create a new HTML page that uses PHP to display today's date in the format "Month Day, Year" (e.g., "October 25, 2025").
- Hint: Look up the date() function documentation on php.net and use the format string
'F j, Y'.
- Hint: Look up the date() function documentation on php.net and use the format string
Short Echo Practice: Create a new file called
profile.phpthat uses the short echo syntax (<?=) to display:- Your name in an
<h1>tag - Your favorite programming language in a
<p>tag - The current year in a footer
- Bonus: Add comments explaining what each section does
- Your name in an
Concatenation Challenge: Create a file called
greeting.phpthat:- Declares three variables:
$greeting,$name, and$punctuation - Uses the
.=operator to build a complete greeting message - Outputs the final message using
echo - Example: 'Hello' + ' ' + 'Alice' + '!' = 'Hello Alice!'
- Declares three variables:
Mixed Content: Update
hello-web.phpto display three different pieces of dynamic information using short echo syntax:- The current day of the week
- The current time (formatted as 'HH:MM AM/PM')
- A custom greeting message based on the time of day Use comments to explain what each PHP block does.
Experiment with Echo: In a new file, compare these three approaches:
// Method 1: Multiple arguments to echo
echo 'Hello', ' ', 'World!';
// Method 2: Concatenation
echo 'Hello' . ' ' . 'World!';
// Method 3: String interpolation
echo "Hello World!";Run the script. What differences do you notice? Which method do you prefer and why?
Wrap-up
Congratulations! You've successfully written and executed your first PHP scripts. Here's what you've accomplished:
- ✓ Created and ran a PHP script from the command line
- ✓ Started a local development server and viewed PHP output in a browser
- ✓ Embedded PHP within HTML to create dynamic web pages
- ✓ Learned the basic syntax: PHP tags, echo statement, and comments
- ✓ Understood when to use (and omit) closing PHP tags
- ✓ Mastered the short echo syntax (
<?=) for cleaner templates - ✓ Learned string concatenation with the
.and.=operators - ✓ Gained experience with troubleshooting common issues
What's Next
In the next chapter, we'll explore variables — the foundation for storing and manipulating data in your scripts. You'll learn about different data types, how to create and use variables, and how to work with constants.
Quick Recap
<?php
// Pure PHP files - no closing tag needed
echo 'Output text'; // Echo statement
echo 'Hello' . ' ' . 'World'; // Concatenation
// Comments
// Single line comment
/* Multi-line comment */In HTML templates:
<!DOCTYPE html>
<html>
<body>
<!-- Long form -->
<h1><?php echo $title; ?></h1>
<!-- Short echo syntax -->
<p><?= $description ?></p>
</body>
</html>Code Examples
Complete, runnable examples from this chapter are available in:
basic-syntax.php- Basic PHP syntax and echo examplesmixing-html.php- Mixing PHP with HTMLvariables-demo.php- Working with variables and concatenationsolutions/- Solutions to chapter exercises
Further Reading
To deepen your understanding of the topics covered in this chapter:
- PHP Tags — Official documentation on PHP opening and closing tags, including short echo syntax
- Echo Statement — Learn more about outputting data
- String Operators — Complete guide to concatenation and string manipulation
- PHP Comments — Best practices for commenting your code
- PSR-12: Extended Coding Style — Industry-standard PHP coding style guide (includes closing tag best practices)
- Built-in Web Server — Advanced options for the development server
- Date and Time Functions — Complete reference for working with dates
Knowledge Check
Test your understanding of PHP basics and syntax: