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 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.
A PHP statement normally ends with a semicolon. echo is a language construct that writes one or more values to the response.
<?php
$course = 'PHP';
$lesson = 1;
echo "<h1>{$course} Course</h1>" . PHP_EOL;
echo "<p>Lesson {$lesson} is running.</p>";
<h1>PHP Course</h1>
<p>Lesson 1 is running.</p>
Double-quoted strings interpolate the two variables before echo writes the HTML response.
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 |
| One | 1 | Legacy or expression-specific code |
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.
<?php
$topic = 'PHP syntax';
echo "Now learning: {$topic}" . PHP_EOL;
Now learning: PHP syntax
| 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 |
0 of 2 checked
Try this next
0 of 2 completed
Explore 500+ free tutorials across 20+ languages and frameworks.