Write and Run Your First PHP Hello World Page
Create a PHP Hello World page, open it through a web server, and check how a changed message appears inside HTML.
To make your first PHP page, save a file named hello.php containing an echo statement, put it where your PHP-capable web server serves pages, and open that page’s URL in a browser. You should see Hello, world! The important choice is to use the server’s URL rather than open the file directly from disk: PHP runs on the server, and the browser receives the result.
Create hello.php
Create a new file in your text editor and save it as hello.php. The .php extension is the default for PHP files, and <?php marks the start of the PHP script. Put this complete example in the file:
<?php
echo "Hello, world!";
The quoted text is the message you want to display. Here, echo outputs that text on the page. Save the file before trying to open it through the server. A short file is enough for this first check: you have one message to look for and one statement to change when it works.
Open the page through your web server
Put hello.php in a location from which your configured PHP-capable web server serves pages. Then open the URL corresponding to that file in your browser. For example, if your server is configured so that http://localhost/hello.php maps to the file you saved, open that URL. Your server may use a different address or serve files from a different location; use its configured URL and location rather than assuming the example address applies.
The page should display:
Hello, world!
Check the actual words, including the comma and exclamation mark. Seeing that message confirms the outcome this exercise asks you to check: the browser is displaying the text produced by your file. Opening hello.php directly from disk is not an equivalent check, because the intended path is for the PHP-capable server to execute the script and send its result to the browser. As the PHP syntax tutorial puts it, the script executes on the server and the plain HTML result is sent to the browser.
If the browser does not show the message, first compare the URL with the location where you saved the file. The two need to correspond according to your server’s configuration. Also check that you saved the file as hello.php and opened its server URL, not a local file address. This keeps the first check focused on whether you are viewing the page you just made.
Change the message, then add HTML
Now make one small edit to hello.php: replace Hello, world! between the quotes with Hello, PHP!. Save the file and reload the same browser URL. The displayed text should change to Hello, PHP!. Reloading matters here because you want to check the saved change, not the page you had open before the edit.
For the second change, replace the file’s contents with this complete example. It keeps your new message but places the PHP code inside an HTML paragraph:
<!doctype html>
<html lang="en">
<head>
<title>My first PHP page</title>
</head>
<body>
<p><?php echo "Hello, PHP!"; ?></p>
</body>
</html>
Save hello.php again and reload its server URL. The browser should display Hello, PHP! inside a paragraph. The PHP file contains both HTML and PHP code, but the browser receives the output, not the PHP statement. In this example, the paragraph in the returned HTML contains the text:
<p>Hello, PHP!</p>
You can repeat the message check with this HTML version: change Hello, PHP! between the quotes to a message of your choice, save the file, and reload the page. The words inside the paragraph should change with it. That is the basic loop for this first PHP page: edit the file, save it, request it through the server, and compare what the browser displays with the message you wrote.
