Tutorials Logic, IN info@tutorialslogic.com

First PHP Program: Run, Read, and Debug the Output

First Program Goal

Your first program should prove three things: PHP can execute the file, you can identify the generated output, and you know where to look when the result is wrong.

This lesson uses one small status page so syntax, HTML output, and the request path stay visible.

Create the File

Create hello.php inside the web root. In XAMPP that is commonly C:\xampp\htdocs. Start Apache and open the file through a URL such as http://localhost/hello.php.

The .php extension matters because the web server uses its PHP configuration to decide which files should be executed.

Hello PHP

A PHP statement normally ends with a semicolon. echo is a language construct that writes one or more values to the response.

A Small Status Page

A Small Status Page
<?php
$course = 'PHP';
$lesson = 1;

echo "<h1>{$course} Course</h1>" . PHP_EOL;
echo "<p>Lesson {$lesson} is running.</p>";
Output
<h1>PHP Course</h1>
<p>Lesson 1 is running.</p>

Double-quoted strings interpolate the two variables before echo writes the HTML response.

Echo and Print

Use echo for ordinary output. print also writes one value and returns 1, but that return value is rarely useful in beginner application code.

Neither construct automatically escapes HTML. Later form lessons use htmlspecialchars() before inserting untrusted text into a page.

Construct Values per statement Return value Typical choice
echo One or more None Normal response output
print One 1 Legacy or expression-specific code

Run from the CLI

The same file can run without Apache when it does not depend on an HTTP request. From its directory, execute php hello.php. HTML tags will appear as text because a terminal does not render HTML.

Terminal-Friendly Output

Terminal-Friendly Output
<?php
$topic = 'PHP syntax';
echo "Now learning: {$topic}" . PHP_EOL;
Output
Now learning: PHP syntax

First Run Failures

Symptom Likely cause Fix
Browser shows <?php File was served without PHP execution Use Apache/PHP and a localhost URL
404 Not Found URL does not match the web-root path Check the file name and directory
Parse error PHP syntax is incomplete Read the reported file and line, then inspect nearby punctuation
Blank page No output or errors are hidden Run php -l hello.php and inspect the error log

Read the First Run

0 of 2 checked

Q1. Why should hello.php be opened through localhost?

Q2. What does php -l hello.php check?

First Program Failure Boundary

  • Source code served as text

    If the browser displays PHP source, the request did not pass through a configured PHP handler. Stop and fix the web-server mapping before exposing files containing secrets.

Try this next

Change the Response

0 of 2 completed

  1. Add a $completed variable and print it in a second paragraph. Predict the exact HTML before refreshing.
  2. Remove one semicolon, run php -l, record the message, then restore the statement. The reported line can be after the actual missing punctuation.
Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.